Completed
Branch master (8d6fa8)
by Anas
01:00
created

gif()   D

Complexity

Conditions 9

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 2 Features 0
Metric Value
cc 9
c 8
b 2
f 0
dl 0
loc 22
rs 4.7088
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 random import randint
7
import datetime
8
import yaml
9
import os
10
11
12
def handler(dp):
13
    dp.add_handler(CommandHandler("gif", gif, pass_args=True))
14
    dp.add_handler(CallbackQueryHandler(gif_button, pattern="([A-z0-9\\\])"))
15
16
with open("config.yml", "r") as f:
17
    gif_folder = yaml.load(f)["path"]["gifs"]
18
19
20
@run_async
21
def gif(bot, update, args):
0 ignored issues
show
Unused Code introduced by
The argument bot seems to be unused.
Loading history...
22
    folders = os.walk(gif_folder)
23
    args = str(args)[2:-2]
24
    avail_folders = next(folders)[1]
25
    if args == "help" or args == "?":
26
        reply_markup = make_keyboard(avail_folders)
27
        update.message.reply_text("Available folders for /gif are:", reply_markup=reply_markup)
28
    elif args in avail_folders or args == "":
29
        update.message.chat.send_action(ChatAction.UPLOAD_DOCUMENT)
30
        gifs_dir = gif_folder + args
31
        gifs = [f for f in os.listdir(gifs_dir)
32
                if os.path.isfile(os.path.join(gifs_dir, f))
33
                and f.endswith((".mp4", ".gif"))]
34
        filecount = len(gifs)
35
        rand = randint(0, filecount - 1)
36
        result = list(gifs)[rand]
37
        with open(gifs_dir + "/" + str(result), "rb") as f:
38
            update.message.reply_document(f)
39
        print(datetime.datetime.now(), ">>> Sent gif >>>", update.message.from_user.username, ">", result)
40
    else:
41
        update.message.reply_text("No such folder, try /gif help")
42
43
44
def gif_button(bot, update):
45
    query = update.callback_query
46
    user = query.from_user.username
47
    bot.editMessageText(text="%s selected: %s\nUploading can take a while!"
48
                        % (user, query.data), 
49
                        chat_id=query.message.chat_id,
50
                        message_id=query.message.message_id)
51
    query.message.chat.send_action(ChatAction.UPLOAD_DOCUMENT)
52
    gifs_dir = gif_folder + query.data
53
    gifs = [f for f in os.listdir(gifs_dir)
54
            if os.path.isfile(os.path.join(gifs_dir, f))
55
            and f.endswith((".mp4", ".gif"))]
56
    filecount = len(gifs)
57
    rand = randint(0, filecount - 1)
58
    result = list(gifs)[rand]
59
    with open(gifs_dir + "/" + str(result), "rb") as f:
60
        bot.sendDocument(query.message.chat_id, f)
61
    print(datetime.datetime.now(), ">>> Sent gif >>>", query.message.from_user.username, ">", result)
62
63
64
def make_keyboard(folders):
65
    key_list = []
66
    for i in folders:
67
        key = InlineKeyboardButton(i, callback_data=i)
68
        key_list.append(key)
69
    row_split = lambda list, size, acc=[]: (row_split(list[size:], size, acc + [list[:size]]) if list else acc)
70
    rows = row_split(key_list, 4)
71
    root_btn = [InlineKeyboardButton("[unsorted]", callback_data="\\")]
72
    rows.insert(0, root_btn)
73
    keyboard = rows
74
    reply_markup = InlineKeyboardMarkup(keyboard)
75
    return reply_markup