Completed
Push — master ( 7dbca0...394d77 )
by Anas
01:03
created

kek()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
cc 5
c 7
b 1
f 1
dl 0
loc 19
rs 8.5454
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 import ChatAction
6
import subprocess
7
import datetime
8
import yaml
9
10
11
def handler(dp):
12
    dp.add_handler(MessageHandler(caption_filter("/kek"), kek))
13
    dp.add_handler(CommandHandler("kek", kek))
14
15
# import path
16
with open("config.yml", "r") as f:
17
    path = yaml.load(f)["path"]["kek"]
18
19
extensions = (".jpg", ".jpeg", ".png", ".bmp", ".webp")
20
21
22
# get image, pass parameter
23
def kek(bot, update):
24
    if update.message.reply_to_message is not None:
25
        kek_param = "".join(update.message.text[5:7])
26
    elif update.message.caption is not None:
27
        kek_param = "".join(update.message.caption[5:7])
28
    else:
29
        update.message.reply_text("You need an image for that!")
30
    try:
31
        extension = get_image(bot, update, path)
32
    except:
33
        update.message.reply_text("Can't get the image! :(")
34
        return
35
    if extension not in extensions:
36
        update.message.reply_text("Unsupported file, onii-chan!")
37
        return False
38
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
39
    result = kekify(update, kek_param, extension)
40
    send_image(update, path, result, extension)
41
    print(datetime.datetime.now(), ">>>", "Done kek", ">>>", update.message.from_user.username)
42
43
44
# kek process + send
45
def kekify(update, kek_param, extension):
46
    try:
47
        if kek_param == "-l" or kek_param == "":
48
            crop = "50%x100% "
49
            piece_one = "result-0" + extension
50
            piece_two = "result-1" + extension
51
            flip = "-flop "
52
            order = path + piece_one + " " + path + piece_two
53
            append = "+append "
54
            result = "kek-left"
55
        elif kek_param == "-r":
56
            crop = "50%x100% "
57
            piece_one = "result-1" + extension
58
            piece_two = "result-0" + extension
59
            flip = "-flop "
60
            order = path + piece_two + " " + path + piece_one
61
            append = "+append "
62
            result = "kek-right"
63
        elif kek_param == "-t":
64
            crop = "100%x50% "
65
            piece_one = "result-0" + extension
66
            piece_two = "result-1" + extension
67
            flip = "-flip "
68
            order = path + piece_one + " " + path + piece_two
69
            append = "-append "
70
            result = "kek-top"
71
        elif kek_param == "-b":
72
            crop = "100%x50% "
73
            piece_one = "result-1" + extension
74
            piece_two = "result-0" + extension
75
            flip = "-flip "
76
            order = path + piece_two + " " + path + piece_one
77
            append = "-append "
78
            result = "kek-bot"
79
        elif kek_param == "-m":
80
            result = multikek(update, extension)
81
            return result
82
        cut = "convert " + path + "original" + extension + " -crop " + crop + path + "result" + extension
83
        subprocess.run(cut, shell=True)
84
        mirror = "convert " + path + piece_one + " " + flip + " " + path + piece_two
85
        subprocess.run(mirror, shell=True)
86
        append = "convert " + order + " " + append + path + result + extension
87
        subprocess.run(append, shell=True)
88
        return result
89
    except:
90
        update.message.reply_text("Unknown kek parameter.\nUse -l, -r, -t, -b or -m")
91
        return
92
93
94
def multikek(update, extension):
95
    kekify(update, "-l", extension)
96
    kekify(update, "-r", extension)
97
    kekify(update, "-t", extension)
98
    kekify(update, "-b", extension)
99
    append_lr = "convert " + path + "kek-left" + extension + " " + path + "kek-right" + extension + " +append " + path + "kek-lr-temp" + extension
100
    subprocess.run(append_lr, shell=True)
101
    append_tb = "convert " + path + "kek-top" + extension + " " + path + "kek-bot" + extension + " +append " + path + "kek-tb-temp" + extension
102
    subprocess.run(append_tb, shell=True)
103
    append_all = "convert " + path + "kek-lr-temp" + extension + " " + path + "kek-tb-temp" + extension + " -append " + path + "multikek" + extension
104
    subprocess.run(append_all, shell=True)
105
    result = "multikek"
106
    return result