imseldrith commited on
Commit
8e0c2ab
Β·
1 Parent(s): 0e225df

Upload 4 files

Browse files
unzipper/helpers_nexa/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===================================================================== #
2
+ # Copyright (c) 2022 Itz-fork #
3
+ # #
4
+ # This program is distributed in the hope that it will be useful, #
5
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of #
6
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
7
+ # See the GNU General Public License for more details. #
8
+ # #
9
+ # You should have received a copy of the GNU General Public License #
10
+ # along with this program. If not, see <http://www.gnu.org/licenses/> #
11
+ # ===================================================================== #
unzipper/helpers_nexa/buttons.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===================================================================== #
2
+ # Copyright (c) 2022 Itz-fork #
3
+ # #
4
+ # This program is distributed in the hope that it will be useful, #
5
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of #
6
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
7
+ # See the GNU General Public License for more details. #
8
+ # #
9
+ # You should have received a copy of the GNU General Public License #
10
+ # along with this program. If not, see <http://www.gnu.org/licenses/> #
11
+ # ===================================================================== #
12
+
13
+ from os.path import basename
14
+ from pykeyboard import InlineKeyboard
15
+ from unzipper.client.caching import STRINGS
16
+ from unzipper.helpers_nexa.utils import read_json_sync
17
+ from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
18
+
19
+
20
+ class Unzipper_Buttons:
21
+ def __init__(self) -> None:
22
+ pass
23
+
24
+ async def make_button(self, text: str, *args, **kwargs):
25
+ """
26
+ Create pyrogram InlineKeyboardMarkup object with 1 button
27
+ """
28
+ return InlineKeyboardMarkup([
29
+ [InlineKeyboardButton(text, *args, **kwargs)]
30
+ ])
31
+
32
+ async def make_files_keyboard(self, files: list, user_id: int, chat_id: int, inlude_files: bool = True):
33
+ i_kbd = InlineKeyboard(row_width=2)
34
+ data = [InlineKeyboardButton(STRINGS["buttons"]["upload_all"], f"ext_a|{user_id}|{chat_id}"), InlineKeyboardButton(
35
+ STRINGS["buttons"]["cancel"], "cancel_dis")]
36
+ if inlude_files:
37
+ for num, file in enumerate(files):
38
+ # Stop iterating if file count is 90
39
+ if num >= 90:
40
+ break
41
+ data.append(
42
+ InlineKeyboardButton(f"{num} - {basename(file)}".encode(
43
+ "utf-8").decode("utf-8"), f"ext_f|{user_id}|{chat_id}|{num}")
44
+ )
45
+ i_kbd.add(*data)
46
+ return i_kbd
47
+
48
+ START = InlineKeyboardMarkup([[
49
+ InlineKeyboardButton(
50
+ STRINGS["buttons"]["help"], callback_data="helpcallback"),
51
+ InlineKeyboardButton(
52
+ STRINGS["buttons"]["about"], callback_data="aboutcallback")
53
+ ]])
54
+
55
+ HELP = InlineKeyboardMarkup([
56
+ [
57
+ InlineKeyboardButton(
58
+ STRINGS["buttons"]["help_extract"], callback_data="extracthelp"),
59
+ InlineKeyboardButton(
60
+ STRINGS["buttons"]["help_upload"], callback_data="upmodhelp")
61
+ ],
62
+ [
63
+ InlineKeyboardButton(
64
+ STRINGS["buttons"]["help_thumbnail"], callback_data="thumbhelp"),
65
+ InlineKeyboardButton(
66
+ STRINGS["buttons"]["help_backup"], callback_data="backuphelp")
67
+ ],
68
+ [
69
+ InlineKeyboardButton(
70
+ STRINGS["buttons"]["help_langs"], callback_data="langhelp")
71
+ ],
72
+ [
73
+ InlineKeyboardButton(
74
+ STRINGS["buttons"]["back"], callback_data="megoinhome")
75
+ ]
76
+ ])
77
+
78
+ HELP_BACK = InlineKeyboardMarkup(
79
+ [[InlineKeyboardButton(STRINGS["buttons"]["back_to_help_menu"], callback_data="helpcallback")]])
80
+
81
+ EXTRACT_FILE = InlineKeyboardMarkup([
82
+ [
83
+ InlineKeyboardButton(
84
+ STRINGS["buttons"]["extract_file"], callback_data="extract_file|tg_file|no_pass")
85
+ ],
86
+ [
87
+ InlineKeyboardButton(
88
+ STRINGS["buttons"]["extract_file_pass"], callback_data="extract_file|tg_file|with_pass")
89
+ ],
90
+ [
91
+ InlineKeyboardButton(
92
+ STRINGS["buttons"]["cancel"], callback_data="cancel_dis")
93
+ ]
94
+ ])
95
+
96
+ EXTRACT_URL = InlineKeyboardMarkup([
97
+ [
98
+ InlineKeyboardButton(
99
+ STRINGS["buttons"]["extract_url"], callback_data="extract_file|url|no_pass"),
100
+ ],
101
+ [
102
+ InlineKeyboardButton(
103
+ STRINGS["buttons"]["extract_url_pass"], callback_data="extract_file|url|with_pass")
104
+ ],
105
+ [
106
+ InlineKeyboardButton(
107
+ STRINGS["buttons"]["cancel"], callback_data="cancel_dis")
108
+ ]
109
+ ])
110
+
111
+ CLEAN = InlineKeyboardMarkup([
112
+ [
113
+ InlineKeyboardButton(
114
+ STRINGS["buttons"]["clean"], callback_data="cancel_dis")
115
+ ],
116
+ [
117
+ InlineKeyboardButton(
118
+ STRINGS["buttons"]["no_cancel"], callback_data="nobully")
119
+ ]
120
+ ])
121
+
122
+ BACKUP = InlineKeyboardMarkup(
123
+ [[InlineKeyboardButton("Gofile.io", callback_data="cloudbackup|gofile"), ]])
124
+
125
+ SETTINGS_GOFILE = InlineKeyboardMarkup([
126
+ [
127
+ InlineKeyboardButton(
128
+ STRINGS["buttons"]["gofile_set"], callback_data="gf_setting-set"),
129
+ InlineKeyboardButton(
130
+ STRINGS["buttons"]["gofile_del"], callback_data="gf_setting-del")
131
+ ],
132
+ [
133
+ InlineKeyboardButton(
134
+ STRINGS["buttons"]["gofile_get"], callback_data="gf_setting-get")
135
+ ]
136
+ ])
137
+
138
+ UPLOAD_MODE = InlineKeyboardMarkup([
139
+ [
140
+ InlineKeyboardButton(
141
+ STRINGS["buttons"]["as_doc"], callback_data="set_mode|doc")
142
+ ],
143
+ [
144
+ InlineKeyboardButton(
145
+ STRINGS["buttons"]["as_vid"], callback_data="set_mode|video")
146
+ ]
147
+ ])
148
+
149
+ LANGUAGES = InlineKeyboardMarkup([[InlineKeyboardButton(
150
+ v, f"set_lang|{k}")] for k, v in read_json_sync("unzipper/localization/languages.json", True)])
151
+
152
+ BACK = InlineKeyboardMarkup(
153
+ [[InlineKeyboardButton(STRINGS["buttons"]["back"], callback_data="megoinhome")]])
unzipper/helpers_nexa/checks.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===================================================================== #
2
+ # Copyright (c) 2022 Itz-fork #
3
+ # #
4
+ # This program is distributed in the hope that it will be useful, #
5
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of #
6
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
7
+ # See the GNU General Public License for more details. #
8
+ # #
9
+ # You should have received a copy of the GNU General Public License #
10
+ # along with this program. If not, see <http://www.gnu.org/licenses/> #
11
+ # ===================================================================== #
12
+
13
+
14
+ import logging
15
+ from pyrogram import enums
16
+ from unzipper import unzip_client
17
+ from config import Config
18
+
19
+
20
+ def check_log_channel():
21
+ try:
22
+ if Config.LOGS_CHANNEL:
23
+ c_info = unzip_client.get_chat(chat_id=Config.LOGS_CHANNEL)
24
+ if c_info.type != enums.ChatType.CHANNEL:
25
+ logging.warn("Chat is not a channel!")
26
+ exit()
27
+ elif c_info.username is not None:
28
+ logging.warn("Channel is not private!")
29
+ exit()
30
+ else:
31
+ unzip_client.send_message(
32
+ chat_id=Config.LOGS_CHANNEL, text="`Unzipper-Bot has Successfully Started!` \n\n**Powered by @NexaBotsUpdates**")
33
+ else:
34
+ logging.warn("No Log Channel ID is Given! Imma leaving Now!")
35
+ exit()
36
+ except Exception as e:
37
+ logging.warn("Error Happend while checking Log Channel! Make sure you're not dumb enough to provide a wrong Log Channel ID!")
38
+ logging.warn(f"Error: \n{e}")
unzipper/helpers_nexa/utils.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===================================================================== #
2
+ # Copyright (c) 2022 Itz-fork #
3
+ # #
4
+ # This program is distributed in the hope that it will be useful, #
5
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of #
6
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
7
+ # See the GNU General Public License for more details. #
8
+ # #
9
+ # You should have received a copy of the GNU General Public License #
10
+ # along with this program. If not, see <http://www.gnu.org/licenses/> #
11
+ # ===================================================================== #
12
+
13
+ # Credits: SpEcHiDe's AnyDL-Bot for progress_for_pyrogram, humanbytes and TimeFormatter
14
+
15
+ from re import sub
16
+ from time import time
17
+ from math import floor
18
+ from json import loads
19
+ from os import path, walk
20
+ from functools import partial
21
+ from subprocess import Popen, PIPE
22
+ from asyncio import get_running_loop
23
+
24
+
25
+ async def progress_for_pyrogram(current, total, ud_type, message, start):
26
+ now = time()
27
+ diff = now - start
28
+ speed = current / diff
29
+ if total:
30
+ if round(diff % 10.00) == 0 or current == total:
31
+ percentage = current * 100 / total
32
+ elapsed_time = round(diff) * 1000
33
+ estimated_total_time = elapsed_time + \
34
+ round((total - current) / speed) * 1000
35
+
36
+ elapsed_time = TimeFormatter(elapsed_time)
37
+ estimated_total_time = TimeFormatter(estimated_total_time)
38
+
39
+ progress = "[{0}{1}] \n**πŸ“Š Progress**: {2}%\n".format(
40
+ # Filled
41
+ ''.join(["β—‰" for i in range(floor(percentage / 5))]),
42
+ # Empty
43
+ ''.join(["β—Ž" for i in range(20 - floor(percentage / 5))]),
44
+ round(percentage, 2))
45
+
46
+ tmp = progress + "{0} of {1}\n**πŸƒ Speed:** {2}/s\n**⏰ ETA:** {3}\n".format(
47
+ humanbytes(current),
48
+ humanbytes(total),
49
+ humanbytes(speed),
50
+ estimated_total_time if estimated_total_time != '' else "0 s"
51
+ )
52
+ try:
53
+ await message.edit("{}\n {} \n\n**Powered by @NexaBotsUpdates**".format(ud_type, tmp))
54
+ except:
55
+ pass
56
+ else:
57
+ tmp = "**πŸ“Š Progress:** {0} of {1}\n**πŸƒ Speed:** {2}/s\n**⏰ ETA:** {3}\n".format(
58
+ humanbytes(current),
59
+ "?",
60
+ humanbytes(speed),
61
+ "unknown"
62
+ )
63
+ try:
64
+ await message.edit("{}\n {} \n\n**Powered by @NexaBotsUpdates**".format(ud_type, tmp))
65
+ except:
66
+ pass
67
+
68
+
69
+ def humanbytes(size: int):
70
+ if not size:
71
+ return "N/A"
72
+ power = 2**10
73
+ n = 0
74
+ Dic_powerN = {0: " ", 1: "Ki", 2: "Mi", 3: "Gi", 4: "Ti"}
75
+ while size > power:
76
+ size /= power
77
+ n += 1
78
+ return f"{round(size, 2)} {Dic_powerN[n]}B"
79
+
80
+
81
+ def TimeFormatter(milliseconds: int) -> str:
82
+ seconds, milliseconds = divmod(int(milliseconds), 1000)
83
+ minutes, seconds = divmod(seconds, 60)
84
+ hours, minutes = divmod(minutes, 60)
85
+ days, hours = divmod(hours, 24)
86
+ tmp = ((str(days) + "d, ") if days else "") + \
87
+ ((str(hours) + "h, ") if hours else "") + \
88
+ ((str(minutes) + "m, ") if minutes else "") + \
89
+ ((str(seconds) + "s, ") if seconds else "") + \
90
+ ((str(milliseconds) + "ms, ") if milliseconds else "")
91
+ return tmp[:-2] if tmp[:-2] else "0ms"
92
+
93
+
94
+ def run_shell_cmds(command):
95
+ """
96
+ Execute shell commands and returns the output
97
+ """
98
+ run = Popen(command, stdout=PIPE,
99
+ stderr=PIPE, shell=True)
100
+ shell_ouput = run.stdout.read()[:-1].decode("utf-8")
101
+ return shell_ouput
102
+
103
+
104
+ async def run_cmds_on_cr(func, *args, **kwargs):
105
+ """
106
+ Execute blocking functions asynchronously
107
+ """
108
+ loop = get_running_loop()
109
+ return await loop.run_in_executor(
110
+ None,
111
+ partial(func, *args, **kwargs)
112
+ )
113
+
114
+
115
+ async def get_files(fpath: str, filter_fn=None):
116
+ """
117
+ Returns files in a folder
118
+
119
+ Parameters:
120
+
121
+ - `fpath` - Path to the folder
122
+ - `filter_fn` - Function to filter elements in the array
123
+ """
124
+ path_list = [val for sublist in [
125
+ [path.join(i[0], j) for j in i[2]] for i in walk(fpath)] for val in sublist]
126
+ if filter_fn:
127
+ path_list = list(filter(filter_fn, path_list))
128
+ return sorted(path_list)
129
+
130
+
131
+ def read_json_sync(name: str, as_items: bool = False) -> dict:
132
+ """
133
+ Reads json file and returns a dict
134
+
135
+ Parameters:
136
+
137
+ - `name` - File path
138
+ - `as_items` - Pass "True" if you want to return items of the dict
139
+ """
140
+ with open(name) as fs:
141
+ return loads(fs.read()).items() if as_items else loads(fs.read())
142
+
143
+
144
+ async def rm_mark_chars(text: str):
145
+ """
146
+ Remove basic markdown characters
147
+
148
+ Parameters:
149
+
150
+ - `text` - Text
151
+ """
152
+ return sub("[*`_]", "", text)