1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ChatAction |
4
|
|
|
from telegram.ext import CommandHandler, MessageHandler, CallbackQueryHandler |
5
|
|
|
from modules.utils import caption_filter, get_image, send_image |
6
|
|
|
from modules.logging import log_command |
7
|
|
|
import modules.instagram_filters |
8
|
|
|
from datetime import datetime |
9
|
|
|
import inspect |
10
|
|
|
import os |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def module_init(gd): |
14
|
|
|
global path, extensions, filters |
15
|
|
|
path = gd.config["path"] |
16
|
|
|
extensions = gd.config["extensions"] |
17
|
|
|
commands = gd.config["commands"] |
18
|
|
|
for command in commands: |
19
|
|
|
gd.dp.add_handler(MessageHandler(caption_filter("/"+command), instagram)) |
20
|
|
|
gd.dp.add_handler(CommandHandler(command, instagram)) |
21
|
|
|
gd.dp.add_handler(CallbackQueryHandler(instagram_button, pattern="(filt_)\w+")) |
22
|
|
|
|
23
|
|
|
filters = [] |
24
|
|
|
all_funcs = inspect.getmembers(modules.instagram_filters, inspect.isfunction) |
25
|
|
|
for i in range(0, len(all_funcs)): |
26
|
|
|
if all_funcs[i][0].startswith("filt_"): |
27
|
|
|
filters.append(all_funcs[i][0]) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def instagram(bot, update): |
31
|
|
|
global filename |
32
|
|
|
filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
33
|
|
|
try: |
34
|
|
|
extension = get_image(bot, update, path, filename) |
|
|
|
|
35
|
|
|
except: |
36
|
|
|
update.message.reply_text("I can't get the image! :(") |
37
|
|
|
return |
38
|
|
|
if extension not in extensions: |
|
|
|
|
39
|
|
|
update.message.reply_text("Unsupported file, onii-chan!") |
40
|
|
|
return False |
41
|
|
|
instagram_key_list = [] |
42
|
|
|
for i in filters: |
|
|
|
|
43
|
|
|
inst_filter = i |
44
|
|
|
instagram_key = InlineKeyboardButton(str(i)[5:], callback_data=",".join([inst_filter, extension])) |
45
|
|
|
instagram_key_list.append(instagram_key) |
46
|
|
|
row_split = lambda list, size, acc=[]: (row_split(list[size:], size, acc + [list[:size]]) if list else acc) |
|
|
|
|
47
|
|
|
rows = row_split(instagram_key_list, 3) |
48
|
|
|
instagram_keyboard = rows |
49
|
|
|
instagram_reply_markup = InlineKeyboardMarkup(instagram_keyboard) |
50
|
|
|
update.message.reply_text("Available filters are:", reply_markup=instagram_reply_markup) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def instagram_button(bot, update): |
54
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
55
|
|
|
query = update.callback_query |
56
|
|
|
chosen_filter, extension = update.callback_query.data.split(",") |
57
|
|
|
filter_name = str(chosen_filter)[5:] |
58
|
|
|
user = query.from_user.username |
59
|
|
|
bot.editMessageText(text="%s selected: %s\nProcessing..." |
60
|
|
|
% (user, filter_name), |
61
|
|
|
chat_id=query.message.chat_id, |
62
|
|
|
message_id=query.message.message_id) |
63
|
|
|
query.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
64
|
|
|
try: |
65
|
|
|
getattr(modules.instagram_filters, chosen_filter)(path, filename, extension) |
|
|
|
|
66
|
|
|
except: |
67
|
|
|
raise Exception("Instagram error") |
68
|
|
|
send_image(query, path, filename+"-"+filter_name, extension) |
69
|
|
|
os.remove(path+filename+extension) |
70
|
|
|
os.remove(path+filename+"-"+filter_name+extension) |
71
|
|
|
print(current_time, ">", "/instagram", ">", user) |
72
|
|
|
log_command(bot, update, current_time, "instagram") |
73
|
|
|
|