File size: 4,836 Bytes
013b9a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) Shrimadhav U K

# the logging things
import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

import json
import math
import os
import shutil
import subprocess
import time

# the secret configuration specific things
if bool(os.environ.get("WEBHOOK", False)):
    from sample_config import Config
else:
    from config import Config

# the Strings used for this "thing"
from translation import Translation

import pyrogram
logging.getLogger("pyrogram").setLevel(logging.WARNING)

from helper_funcs.display_progress import progress_for_pyrogram, humanbytes
from plugins.youtube_dl_button import youtube_dl_call_back
from plugins.dl_button import ddl_call_back
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
# https://stackoverflow.com/a/37631799/4723940
from PIL import Image


@pyrogram.Client.on_callback_query()
async def button(bot, update):
    if update.from_user.id in Config.AUTH_USERS:
        # logger.info(update)
        cb_data = update.data
        if ":" in cb_data:
            # unzip formats
            extract_dir_path = Config.DOWNLOAD_LOCATION + \
                "/" + str(update.from_user.id) + "zipped" + "/"
            if not os.path.isdir(extract_dir_path):
                await bot.delete_messages(
                    chat_id=update.message.chat.id,
                    message_ids=update.message.message_id,
                    revoke=True
                )
                return False
            zip_file_contents = os.listdir(extract_dir_path)
            type_of_extract, index_extractor, undefined_tcartxe = cb_data.split(":")
            if index_extractor == "NONE":
                try:
                    shutil.rmtree(extract_dir_path)
                except:
                    pass
                await bot.edit_message_text(
                    chat_id=update.message.chat.id,
                    text=Translation.CANCEL_STR,
                    message_id=update.message.message_id
                )
            elif index_extractor == "ALL":
                i = 0
                for file_content in zip_file_contents:
                    current_file_name = os.path.join(extract_dir_path, file_content)
                    start_time = time.time()
                    await bot.send_document(
                        chat_id=update.message.chat.id,
                        document=current_file_name,
                        # thumb=thumb_image_path,
                        caption=file_content,
                        # reply_markup=reply_markup,
                        reply_to_message_id=update.message.message_id,
                        progress=progress_for_pyrogram,
                        progress_args=(
                            Translation.UPLOAD_START,
                            update.message,
                            start_time
                        )
                    )
                    i = i + 1
                    os.remove(current_file_name)
                try:
                    shutil.rmtree(extract_dir_path)
                except:
                    pass
                await bot.edit_message_text(
                    chat_id=update.message.chat.id,
                    text=Translation.ZIP_UPLOADED_STR.format(i, "0"),
                    message_id=update.message.message_id
                )
            else:
                file_content = zip_file_contents[int(index_extractor)]
                current_file_name = os.path.join(extract_dir_path, file_content)
                start_time = time.time()
                await bot.send_document(
                    chat_id=update.message.chat.id,
                    document=current_file_name,
                    # thumb=thumb_image_path,
                    caption=file_content,
                    # reply_markup=reply_markup,
                    reply_to_message_id=update.message.message_id,
                    progress=progress_for_pyrogram,
                    progress_args=(
                        Translation.UPLOAD_START,
                        update.message,
                        start_time
                    )
                )
                try:
                    shutil.rmtree(extract_dir_path)
                except:
                    pass
                await bot.edit_message_text(
                    chat_id=update.message.chat.id,
                    text=Translation.ZIP_UPLOADED_STR.format("1", "0"),
                    message_id=update.message.message_id
                )
        elif "|" in cb_data:
            await youtube_dl_call_back(bot, update)
        elif "=" in cb_data:
            await ddl_call_back(bot, update)