Passed
Push — master ( 9bfda5...dc2491 )
by Anas
02:12
created

modules.lego.lego()   B

Complexity

Conditions 6

Size

Total Lines 27
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 27
nop 2
dl 0
loc 27
rs 8.2986
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
import legofy
11
import os
12
13
14
def module_init(gd):
15
    global path, extensions
16
    path = gd.config["path"]
17
    extensions = gd.config["extensions"]
18
    commands = gd.config["commands"]
19
    for command in commands:
20
        gd.dp.add_handler(MessageHandler(caption_filter("/"+command), lego))
21
        gd.dp.add_handler(CommandHandler(command, lego))
22
23
24
@run_async
25
def lego(bot, update):
26
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
27
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
28
    size = get_param(update, 50, 1, 100)
29
    if size is None:
30
        return
31
    try:
32
        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...
33
    except:
34
        update.message.reply_text("Can't get the image! :(")
35
        return
36
    if extension not in extensions:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable extensions does not seem to be defined.
Loading history...
37
        update.message.reply_text("Unsupported file, onii-chan!")
38
        return False
39
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
40
    if extension == ".webp" or ".png":
41
        stick = "convert " + path + filename + extension + " -background white -flatten " + path + filename + extension
42
        subprocess.run(stick, shell=True)
43
    legofy.main(image_path=path + filename + extension,
44
                output_path=path + filename + "-lego" + extension,
45
                size=size, palette_mode=None, dither=False)
46
    send_image(update, path, filename+"-lego", extension)
47
    os.remove(path+filename+extension)
48
    os.remove(path+filename+"-lego"+extension)
49
    print(current_time, ">", "/lego", ">", update.message.from_user.username)
50
    log_command(bot, update, current_time, "lego")
51