Passed
Push — master ( bdfb99...7eca6c )
by Anas
01:47
created

modules.nyameme   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 11

2 Functions

Rating   Name   Duplication   Size   Complexity  
A module_init() 0 12 3
B nyameme() 0 40 8
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.utils import get_image
4
from modules.logging import logging_decorator
5
from telegram.ext import PrefixHandler
6
from telegram.ext.dispatcher import run_async
7
from modules.memegenerator import make_meme
8
from telegram import ChatAction
9
from datetime import datetime
10
import random
11
import shutil
12
import os
13
14
15
def module_init(gd):
16
    global path, extensions, fonts_dict, nyapath, files
17
    path = gd.config["path"]
18
    extensions = gd.config["extensions"]
19
    commands = gd.config["commands"]
20
    fonts_dict = {}
21
    nyapath = gd.config["nyapath"]
22
    files = os.listdir(nyapath)
23
    for i in gd.config["fonts"]:
24
        fonts_dict[gd.config["fonts"][i]["name"]] = gd.config["fonts"][i]["path"]
25
    for command in commands:
26
        gd.dp.add_handler(PrefixHandler("/", commands, nyameme))
27
28
29
@run_async
30
@logging_decorator("nyameme")
31
def nyameme(update, context):
32
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
33
    reply = update.message.reply_to_message
34
    if reply:
35
        if reply.caption:
36
            args = reply.caption
37
        elif reply.text:
38
            args = reply.text
39
        else:
40
            args = " ".join(context.args)
41
        args = args.split(" ")
42
    else:
43
        args = context.args
44
    if len(args) < 1:
45
        update.message.reply_text("Type in some text!")
46
        return
47
    if len(args) == 1:
48
        top_text = None
49
        bottom_text = args[0]
50
    else:
51
        split_spot = random.randint(1, len(args)-1)
52
        top_text = " ".join(args[:split_spot])
53
        bottom_text = " ".join(args[split_spot:])
54
    rand_font = random.choice(list(fonts_dict))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable fonts_dict does not seem to be defined.
Loading history...
55
    font = fonts_dict[rand_font]
56
    random_image = random.choice(files)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable files does not seem to be defined.
Loading history...
57
    filename = random_image.split(".")[0]
58
    extension = "."+random_image.split(".")[1]
59
    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...
60
        update.message.reply_text("Unexpected error")
61
        return
62
    shutil.copy(nyapath+random_image, path+random_image)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable nyapath does not seem to be defined.
Loading history...
63
    make_meme(top_text, bottom_text, filename, extension, path, font)
64
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
65
    with open(path + filename+"-meme" + extension, "rb") as f:
66
        update.message.reply_photo(f)
67
    os.remove(path+filename+extension)
68
    os.remove(path+filename+"-meme"+extension)
69