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

lego()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
cc 5
c 9
b 1
f 1
dl 0
loc 20
rs 8.5454
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 legofy
10
import yaml
11
12
13
def handler(dp):
14
    dp.add_handler(MessageHandler(caption_filter("/lego"), lego))
15
    dp.add_handler(CommandHandler("lego", lego))
16
17
# import paths
18
with open('config.yml', 'r') as f:
19
    path = yaml.load(f)["path"]["lego"]
20
21
extensions = (".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp")
22
name = "legofied"
23
24
@run_async
25
def lego(bot, update):
26
    size = get_param(update)
27
    try:
28
        extension = get_image(bot, update, path)
29
    except:
30
        update.message.reply_text("Can't get the image! :(")
31
        return
32
    if extension not in extensions:
33
        update.message.reply_text("Unsupported file, onii-chan!")
34
        return False
35
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
36
    if extension == ".webp" or ".png":
37
        stick = "convert " + path + "original" + extension + " -background white -flatten " + path + "original" + extension
38
        subprocess.run(stick, shell=True)
39
    legofy.main(image_path=path + "original" + extension,
40
                output_path=path + name + extension,
41
                size=size, palette_mode=None, dither=False)
42
    send_image(update, path, name, extension)
43
    print(datetime.datetime.now(), ">>>", "Done legofying", ">>>", update.message.from_user.username)
44