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

modules.meme.meme()   C

Complexity

Conditions 9

Size

Total Lines 48
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

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