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

meme()   F

Complexity

Conditions 9

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
cc 9
c 8
b 0
f 1
dl 0
loc 46
rs 3.1578
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
    else:
60
        args = update.message.text.split(" ")
61
        
62
    args = args[1:]
63
64
    if len(args) < 1:
65
        update.message.reply_text("Type in some text!")
66
        return
67
68
    font = fonts_dict["impact"]
69
    for i in fonts_dict:
70
        if "-"+i in args[0] or "-"+i[0] in args[0]:
71
            font = fonts_dict[i]
72
            args = args[1:]
73
            break
74
            
75
    if len(args) < 1:
76
        update.message.reply_text("Type in some text!")
77
        return
78
79
    initial_text = " ".join(args)
80
    split_text = initial_text.split("@", maxsplit=1)
81
82
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
83
    try:
84
        extension = get_image(bot, update, path, filename)
85
    except:
86
        update.message.reply_text("Can't get the image! :(")
87
        return
88
    if extension not in extensions:
89
        update.message.reply_text("Unsupported file, onii-chan!")
90
        return
91
92
    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...
93
    make_meme(top_text, bottom_text, filename, extension, path, font)
94
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
95
    send_image(update, path, filename+"-meme", extension)
96
    print (current_time, ">", "/meme", ">", update.message.from_user.username)
97
    log_command(bot, update, current_time, "meme")
98