Completed
Branch preserve_file_extensions (ac989e)
by Anas
01:06
created

lego()   F

Complexity

Conditions 10

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 1
Metric Value
cc 10
c 8
b 1
f 1
dl 0
loc 34
rs 3.1304

How to fix   Complexity   

Complexity

Complex classes like lego() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler, MessageHandler
4
from modules.custom_filters import caption_filter
5
from telegram.ext.dispatcher import run_async
6
from modules.send_image import send_image
7
from modules.get_image import get_image
8
from telegram import ChatAction
9
import subprocess
10
import datetime
11
import legofy
12
import yaml
13
14
15
def handler(dp):
16
    dp.add_handler(MessageHandler(caption_filter("/lego"), lego))
17
    dp.add_handler(CommandHandler("lego", lego))
18
19
# import paths
20
with open('config.yml', 'r') as f:
21
    path = yaml.load(f)["path"]["lego"]
22
23
extensions = (".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp")
24
name = "legofied"
25
26
@run_async
27
def lego(bot, update):
28
    if update.message.reply_to_message is not None:
29
        parts = update.message.text.split(" ", 1)
30
    else:
31
        parts = update.message.caption.split(" ", 1)
32
    if len(parts) == 1:
33
        size = 60
34
    else:
35
        try:
36
            size = int(parts[1])
37
        except:
38
            update.message.reply_text("Paremeter needs to be a number!")
39
            return
40
        if size > 100 or size < 1:
41
            update.message.reply_text("Baka, make it from 1 to 100!")
42
            return
43
    try:
44
        extension = get_image(bot, update, path)
45
    except:
46
        update.message.reply_text("Can't get the image! :(")
47
        return
48
    if extension not in extensions:
49
        update.message.reply_text("Unsupported file, onii-chan!")
50
        return False
51
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
52
    if extension == ".webp" or ".png":
53
        stick = "convert " + path + "original" + extension + " -background white -flatten " + path + "original" + extension
54
        subprocess.run(stick, shell=True)
55
    legofy.main(image_path=path + "original" + extension,
56
                output_path=path + name + extension,
57
                size=size, palette_mode=None, dither=False)
58
    send_image(bot, update, path, name, extension)
59
    print(datetime.datetime.now(), ">>>", "Done legofying", ">>>", update.message.from_user.username)