Passed
Branch master (dc2491)
by Anas
03:44 queued 02:00
created

modules.liquid.liquid()   A

Complexity

Conditions 4

Size

Total Lines 32
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 31
nop 2
dl 0
loc 32
rs 9.1359
c 0
b 0
f 0
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 modules.logging import log_command
7
from telegram import ChatAction
8
from datetime import datetime
9
import subprocess
10
11
12
def module_init(gd):
13
    global path
14
    path = gd.config["path"]
15
    commands = gd.config["commands"]
16
    for command in commands:
17
        gd.dp.add_handler(MessageHandler(caption_filter("/"+command), liquid))
18
        gd.dp.add_handler(CommandHandler(command, liquid))
19
20
21
@run_async
22
def liquid(bot, update):
23
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
24
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
25
    power = get_param(update, 60, 1, 100)
26
    if power is None:
27
        return
28
    try:
29
        extension = get_image(bot, update, path, filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
30
    except:
31
        update.message.reply_text("I can't get the image! :(")
32
        return
33
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
34
    identify = subprocess.Popen("identify " + path + filename + extension, stdout=subprocess.PIPE).communicate()[0]
35
    res = str(identify.split()[2])[2:-1]
36
    size = str(100 - (power / 1.3))
37
    name = filename + "-liquid"
38
    x = "convert " + path + filename + extension + " -liquid-rescale " + \
39
         size + "%x" + size + "% -resize " + res + "! " + path + name + extension
40
    subprocess.run(x, shell=True)
41
    if extension == ".mp4":
42
        mp4fix = "ffmpeg -loglevel panic -i " + path + name + extension + \
43
                  " -an -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 \
44
                  -pix_fmt yuv420p -c:v libx264 -profile:v high -level:v 2.0 " \
45
                  + path + name + "_mp4" + extension + " -y"
46
        subprocess.run(mp4fix, shell=True)
47
        name = name + "_mp4"
48
    send_image(update, path, name, extension)
49
    os.remove(path+filename+extension)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'os'
Loading history...
Comprehensibility Best Practice introduced by
The variable os does not seem to be defined.
Loading history...
50
    os.remove(path+name+extension)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'os'
Loading history...
51
    print(current_time, ">", "/liquid", ">", update.message.from_user.username)
52
    log_command(bot, update, current_time, "liquid")
53