Completed
Pull Request — master (#3)
by Anas
01:01
created

liquid()   B

Complexity

Conditions 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.utils import caption_filter, get_image, send_image, get_param
4
from telegram.ext import CommandHandler, MessageHandler
5
from telegram.ext.dispatcher import run_async
6
from telegram import ChatAction
7
import subprocess
8
import datetime
9
import yaml
10
11
12
def handler(dp):
13
    dp.add_handler(MessageHandler(caption_filter("/liq"), liquid))
14
    dp.add_handler(CommandHandler("liq", liquid))
15
16
# import path
17
with open("config.yml", "r") as f:
18
    path = yaml.load(f)["path"]["liquid"]
19
20
21
# get image, then rescale
22
@run_async
23
def liquid(bot, update):
24
    power = get_param(update)
25
    try:
26
        extension = get_image(bot, update, path)
27
    except:
28
        update.message.reply_text("I can't get the image! :(")
29
        return
30
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
31
    identify = subprocess.Popen("identify " + path + "original" + extension, stdout=subprocess.PIPE).communicate()[0]
32
    res = str(identify.split()[2])[2:-1]
33
    size = str(100 - (power / 1.3))
34
    name = "liquid"
35
    x = "convert " + path + "original" + extension + " -liquid-rescale " + \
36
         size + "%x" + size + "% -resize " + res + "! " + path + name + extension
37
    subprocess.run(x, shell=True)
38
    if extension == ".mp4":
39
        mp4fix = "ffmpeg -loglevel panic -i " + path + name + extension + \
40
                  " -an -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 \
41
                  -pix_fmt yuv420p -c:v libx264 -profile:v high -level:v 2.0 " \
42
                  + path + name + "_mp4" + extension + " -y"
43
        subprocess.run(mp4fix, shell=True)
44
        name = name + "_mp4"
45
    send_image(update, path, name, extension)
46
    print(datetime.datetime.now(), ">>>", "Done liquid rescale", ">>>", update.message.from_user.username)
47