Completed
Push — master ( 2741fe...8da546 )
by Anas
29s
created

meme()   D

Complexity

Conditions 11

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
cc 11
c 7
b 0
f 1
dl 0
loc 53
rs 4.1538

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like meme() 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 modules.utils import caption_filter, get_image, send_image
4
from telegram.ext import CommandHandler, MessageHandler
5
from telegram.ext.dispatcher import run_async
6
from modules.memegenerator import make_meme
7
from modules.logging import log_command
8
from telegram import ChatAction
9
from datetime import datetime
10
11
12
def module_init(gd):
13
    global path, extensions, fonts_dict
14
    path = gd.config["path"]
15
    extensions = gd.config["extensions"]
16
    commands = gd.config["commands"]
17
    fonts_dict = {}
18
    for i in gd.config["fonts"]:
19
        fonts_dict[gd.config["fonts"][i]["name"]] = gd.config["fonts"][i]["path"]
20
    for command in commands:
21
        gd.dp.add_handler(MessageHandler(caption_filter("/"+command), meme))
22
        gd.dp.add_handler(CommandHandler(commands, meme))
23
24
25
def text_format(update, split_text):
26
    if len(split_text) == 1 and split_text[0] == "":
27
        update.message.reply_text("Type in some text!")
28
        return
29
    elif len(split_text) > 1 and split_text[0] == "" and split_text[1] == "":
30
        update.message.reply_text("Type in some text!")
31
        return
32
    elif len(split_text) == 1:
33
        top_text = None
34
        bottom_text = split_text[0]
35
        bottom_text.rstrip()
36
    elif len(split_text) > 1 and split_text[0] == "":
37
        top_text = None
38
        bottom_text = split_text[1]
39
        bottom_text.lstrip()
40
    elif len(split_text) > 1 and split_text[1] == "":
41
        top_text = split_text[0]
42
        top_text.rstrip()
43
        bottom_text = None
44
    else:
45
        top_text = split_text[0].rstrip()
46
        top_text.rstrip()
47
        bottom_text = split_text[1]
48
        bottom_text.lstrip()
49
    return top_text, bottom_text
50
51
52
@run_async
53
def meme(bot, update):
54
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
55
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
56
    
57
    if len(update.message.photo) > 0:
58
        args = update.message.caption.split(" ")
59
    elif update.message.reply_to_message is not None:
60
        if len(update.message.reply_to_message.photo) > 0:
61
            args = update.message.text.split(" ")
62
        else:
63
            update.message.reply_text("You need an image for this!")
64
            return
65
    else:
66
        update.message.reply_text("You need an image for this!")
67
        return
68
        
69
    args = args[1:]
70
71
    if len(args) < 1:
72
        update.message.reply_text("Type in some text!")
73
        return
74
75
    font = fonts_dict["impact"]
76
    for i in fonts_dict:
77
        if "-"+i in args[0] or "-"+i[0] in args[0]:
78
            font = fonts_dict[i]
79
            args = args[1:]
80
            break
81
            
82
    if len(args) < 1:
83
        update.message.reply_text("Type in some text!")
84
        return
85
86
    initial_text = " ".join(args)
87
    split_text = initial_text.split("@", maxsplit=1)
88
89
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
90
    try:
91
        extension = get_image(bot, update, path, filename)
92
    except:
93
        update.message.reply_text("Can't get the image! :(")
94
        return
95
    if extension not in extensions:
96
        update.message.reply_text("Unsupported file, onii-chan!")
97
        return
98
99
    top_text, bottom_text = text_format(update, split_text)
0 ignored issues
show
Bug Best Practice introduced by
Attempting to unpack a non-sequence
Loading history...
100
    make_meme(top_text, bottom_text, filename, extension, path, font)
101
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
102
    send_image(update, path, filename+"-meme", extension)
103
    print (current_time, ">", "/meme", ">", update.message.from_user.username)
104
    log_command(bot, update, current_time, "meme")
105