1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ChatAction |
4
|
|
|
from telegram.ext import CommandHandler, CallbackQueryHandler |
5
|
|
|
from telegram.ext.dispatcher import run_async |
6
|
|
|
from modules.logging import log_command |
7
|
|
|
from datetime import datetime |
8
|
|
|
from random import randint |
9
|
|
|
import os |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def module_init(gd): |
13
|
|
|
global path |
14
|
|
|
path = gd.config["path"] |
15
|
|
|
commands = gd.config["commands"] |
16
|
|
|
for command in commands: |
17
|
|
|
gd.dp.add_handler(CommandHandler(command, gif, pass_args=True)) |
18
|
|
|
gd.dp.add_handler(CallbackQueryHandler(gif_button, pattern="(gif_)\w+")) |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@run_async |
22
|
|
|
def gif(bot, update, args): |
23
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
24
|
|
|
folders = os.walk(path) |
|
|
|
|
25
|
|
|
args = "gif_" + str(args)[2:-2] |
26
|
|
|
avail_folders = next(folders)[1] |
27
|
|
|
if args == "gif_help" or args == "gif_?": |
28
|
|
|
reply_markup = make_keyboard(avail_folders) |
29
|
|
|
update.message.reply_text("Available folders for /gif are:", reply_markup=reply_markup) |
30
|
|
|
elif args in avail_folders: |
31
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_DOCUMENT) |
32
|
|
|
gifs_dir = path + args |
33
|
|
|
result = getgifs(update, gifs_dir) |
34
|
|
|
with open(gifs_dir + "/" + str(result), "rb") as f: |
35
|
|
|
update.message.reply_document(f) |
36
|
|
|
log_command(bot, update, current_time, "gif") |
37
|
|
|
elif args == "gif_": |
38
|
|
|
gifs_dir = path |
39
|
|
|
result = getgifs(update, gifs_dir) |
40
|
|
|
with open(gifs_dir + "/" + str(result), "rb") as f: |
41
|
|
|
update.message.reply_document(f) |
42
|
|
|
log_command(bot, update, current_time, "gif") |
43
|
|
|
else: |
44
|
|
|
update.message.reply_text("No such folder, try /gif help") |
45
|
|
|
print(current_time, "> /gif >", update.message.from_user.username, ">", args) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def gif_button(bot, update): |
49
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
50
|
|
|
query = update.callback_query |
51
|
|
|
user = query.from_user.username |
52
|
|
|
display_data = str(query.data)[4:] |
53
|
|
|
bot.editMessageText(text="%s selected: %s\nUploading can take a while!" |
54
|
|
|
% (user, display_data), |
55
|
|
|
chat_id=query.message.chat_id, |
56
|
|
|
message_id=query.message.message_id) |
57
|
|
|
query.message.chat.send_action(ChatAction.UPLOAD_DOCUMENT) |
58
|
|
|
if display_data == "unsorted": |
59
|
|
|
gifs_dir = path |
|
|
|
|
60
|
|
|
else: |
61
|
|
|
gifs_dir = path + query.data |
62
|
|
|
result = getgifs(update, gifs_dir) |
63
|
|
|
with open(gifs_dir + "/" + str(result), "rb") as f: |
64
|
|
|
bot.sendDocument(query.message.chat_id, f) |
65
|
|
|
print(current_time, "> /gif >", query.message.from_user.username, ">", display_data) |
66
|
|
|
log_command(bot, update, current_time, "gif") |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def make_keyboard(folders): |
70
|
|
|
key_list = [] |
71
|
|
|
for i in folders: |
72
|
|
|
key = InlineKeyboardButton(str(i)[4:], callback_data=i) |
73
|
|
|
key_list.append(key) |
74
|
|
|
row_split = lambda list, size, acc=[]: (row_split(list[size:], size, acc + [list[:size]]) if list else acc) |
|
|
|
|
75
|
|
|
rows = row_split(key_list, 4) |
76
|
|
|
root_btn = [InlineKeyboardButton("[unsorted]", callback_data="gif_unsorted")] |
77
|
|
|
rows.insert(0, root_btn) |
78
|
|
|
keyboard = rows |
79
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard) |
80
|
|
|
return reply_markup |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def getgifs(update, gifs_dir): |
84
|
|
|
gifs = [f for f in os.listdir(gifs_dir) |
85
|
|
|
if os.path.isfile(os.path.join(gifs_dir, f)) |
86
|
|
|
and f.endswith((".mp4", ".gif"))] |
87
|
|
|
filecount = len(gifs) |
88
|
|
|
rand = randint(0, filecount - 1) |
89
|
|
|
result = list(gifs)[rand] |
90
|
|
|
return result |
91
|
|
|
|