1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from modules.anime_search import get_image, yandere_request_link |
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 modules.meme import fonts_dict, text_format |
9
|
|
|
from modules.nya import files |
10
|
|
|
from telegram import ChatAction |
11
|
|
|
from datetime import datetime |
12
|
|
|
import requests |
13
|
|
|
import random |
14
|
|
|
import shutil |
15
|
|
|
import os |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def module_init(gd): |
19
|
|
|
global path, extensions, nyapath, files |
20
|
|
|
path = gd.config["path"] |
21
|
|
|
extensions = gd.config["extensions"] |
22
|
|
|
commands_nyameme = gd.config["commands_nyameme"] |
23
|
|
|
commands_animeme = gd.config["commands_animeme"] |
24
|
|
|
nyapath = gd.config["nyapath"] |
25
|
|
|
for command in commands_nyameme: |
26
|
|
|
gd.dp.add_handler(PrefixHandler("/", command, nyameme)) |
27
|
|
|
for command in commands_animeme: |
28
|
|
|
gd.dp.add_handler(PrefixHandler("/", command, animeme)) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@run_async |
32
|
|
|
@logging_decorator("nyameme") |
33
|
|
|
def nyameme(update, context): |
34
|
|
|
filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
35
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
36
|
|
|
font, args = get_font(context.args) |
37
|
|
|
meme_text = get_text(update, args) |
38
|
|
|
top_text, bottom_text = text_split(meme_text) |
39
|
|
|
random_image = random.choice(files) |
40
|
|
|
filename = random_image.split(".")[0] |
41
|
|
|
extension = "."+random_image.split(".")[1] |
42
|
|
|
if extension not in extensions: |
|
|
|
|
43
|
|
|
update.message.reply_text("Unexpected error") |
44
|
|
|
return |
45
|
|
|
shutil.copy(nyapath+random_image, path+random_image) |
|
|
|
|
46
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
47
|
|
|
make_meme(top_text, bottom_text, filename, extension, path, font) |
48
|
|
|
with open(path + filename+"-meme" + extension, "rb") as f: |
49
|
|
|
update.message.reply_photo(f) |
50
|
|
|
os.remove(path+filename+extension) |
51
|
|
|
os.remove(path+filename+"-meme"+extension) |
52
|
|
|
return |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
@run_async |
56
|
|
|
@logging_decorator("animeme") |
57
|
|
|
def animeme(update, context): |
58
|
|
|
filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
59
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
60
|
|
|
font, args = get_font(context.args) |
61
|
|
|
meme_text = get_text(update, args) |
62
|
|
|
top_text, bottom_text = text_split(meme_text) |
63
|
|
|
_, _, sample_link = get_image("rating:safe", yandere_request_link, "") |
64
|
|
|
extension = "."+sample_link.split(".")[-1] |
65
|
|
|
if extension not in extensions: |
|
|
|
|
66
|
|
|
update.message.reply_text("Unexpected error") |
67
|
|
|
return |
68
|
|
|
response = requests.get(sample_link) |
69
|
|
|
with open(path+filename+extension, "wb") as img: |
|
|
|
|
70
|
|
|
img.write(response.content) |
71
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
72
|
|
|
make_meme(top_text, bottom_text, filename, extension, path, font) |
73
|
|
|
with open(path + filename+"-meme" + extension, "rb") as f: |
74
|
|
|
update.message.reply_photo(f) |
75
|
|
|
os.remove(path+filename+extension) |
76
|
|
|
os.remove(path+filename+"-meme"+extension) |
77
|
|
|
return |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def get_text(update, args): |
81
|
|
|
reply = update.message.reply_to_message |
82
|
|
|
if reply: |
83
|
|
|
if reply.caption: |
84
|
|
|
args = reply.caption |
85
|
|
|
elif reply.text: |
86
|
|
|
args = reply.text |
87
|
|
|
else: |
88
|
|
|
args = " ".join(args) |
89
|
|
|
args = args.split(" ") |
90
|
|
|
else: |
91
|
|
|
pass |
92
|
|
|
if len(args) < 1: |
93
|
|
|
update.message.reply_text("Type in some text!") |
94
|
|
|
return None |
95
|
|
|
return args |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def text_split(text_list): |
99
|
|
|
if len(text_list) == 1: |
100
|
|
|
top_text = None |
101
|
|
|
bottom_text = text_list[0] |
102
|
|
|
elif "@" in text_list: |
103
|
|
|
split_text = " ".join(text_list).split("@", maxsplit=1) |
104
|
|
|
top_text, bottom_text = text_format(split_text) |
105
|
|
|
else: |
106
|
|
|
split_spot = random.randint(1, len(text_list)-1) |
107
|
|
|
top_text = " ".join(text_list[:split_spot]) |
108
|
|
|
bottom_text = " ".join(text_list[split_spot:]) |
109
|
|
|
return top_text, bottom_text |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def get_font(args): |
113
|
|
|
rand_font = random.choice(list(fonts_dict)) |
114
|
|
|
font = fonts_dict[rand_font] |
115
|
|
|
if len(args) < 1: |
116
|
|
|
return font, args |
117
|
|
|
else: |
118
|
|
|
for i in fonts_dict: |
119
|
|
|
if "-"+i in args[0] or "-"+i[0] in args[0]: |
120
|
|
|
font = fonts_dict[i] |
121
|
|
|
args = args[1:] |
122
|
|
|
break |
123
|
|
|
return font, args |