forked from ErichDanikenOfficial/TG-Rename-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunzip.py
More file actions
153 lines (145 loc) · 5.45 KB
/
Copy pathunzip.py
File metadata and controls
153 lines (145 loc) · 5.45 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/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 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.chat_base import TRChatBase
from helper_funcs.display_progress import progress_for_pyrogram, humanbytes
@pyrogram.Client.on_message(pyrogram.Filters.command(["unzip"]))
async def unzip(bot, update):
if update.from_user.id not in Config.AUTH_USERS:
await bot.delete_messages(
chat_id=update.chat.id,
message_ids=update.message_id,
revoke=True
)
return
TRChatBase(update.from_user.id, update.text, "unzip")
saved_file_path = Config.DOWNLOAD_LOCATION + \
"/" + str(update.from_user.id) + ".unzip.zip"
if os.path.exists(saved_file_path):
os.remove(saved_file_path)
reply_message = update.reply_to_message
if ((reply_message is not None) and
(reply_message.document is not None) and
(reply_message.document.file_name.endswith(Translation.UNZIP_SUPPORTED_EXTENSIONS))):
a = await bot.send_message(
chat_id=update.chat.id,
text=Translation.DOWNLOAD_START,
reply_to_message_id=update.message_id
)
c_time = time.time()
try:
await bot.download_media(
message=reply_message,
file_name=saved_file_path,
progress=progress_for_pyrogram,
progress_args=(
Translation.DOWNLOAD_START,
a,
c_time
)
)
except (ValueError) as e:
await bot.edit_message_text(
chat_id=update.chat.id,
text=str(e),
message_id=a.message_id
)
else:
await bot.edit_message_text(
chat_id=update.chat.id,
text=Translation.SAVED_RECVD_DOC_FILE,
message_id=a.message_id
)
extract_dir_path = Config.DOWNLOAD_LOCATION + \
"/" + str(update.from_user.id) + "zipped" + "/"
if not os.path.isdir(extract_dir_path):
os.makedirs(extract_dir_path)
await bot.edit_message_text(
chat_id=update.chat.id,
text=Translation.EXTRACT_ZIP_INTRO_THREE,
message_id=a.message_id
)
try:
command_to_exec = [
"7z",
"e",
"-o" + extract_dir_path,
saved_file_path
]
# https://stackoverflow.com/a/39629367/4723940
logger.info(command_to_exec)
t_response = subprocess.check_output(
command_to_exec, stderr=subprocess.STDOUT)
# https://stackoverflow.com/a/26178369/4723940
except:
try:
os.remove(saved_file_path)
shutil.rmtree(extract_dir_path)
except:
pass
await bot.edit_message_text(
chat_id=update.chat.id,
text=Translation.EXTRACT_ZIP_ERRS_OCCURED,
disable_web_page_preview=True,
parse_mode="html",
message_id=a.message_id
)
else:
os.remove(saved_file_path)
inline_keyboard = []
zip_file_contents = os.listdir(extract_dir_path)
i = 0
for current_file in zip_file_contents:
cb_string = "ZIP:{}:ZIP".format(str(i))
inline_keyboard.append([
pyrogram.InlineKeyboardButton(
current_file,
callback_data=cb_string.encode("UTF-8")
)
])
i = i + 1
cb_string = "ZIP:{}:ZIP".format("ALL")
inline_keyboard.append([
pyrogram.InlineKeyboardButton(
"Upload All Files",
callback_data=cb_string.encode("UTF-8")
)
])
cb_string = "ZIP:{}:ZIP".format("NONE")
inline_keyboard.append([
pyrogram.InlineKeyboardButton(
"Cancel",
callback_data=cb_string.encode("UTF-8")
)
])
reply_markup = pyrogram.InlineKeyboardMarkup(inline_keyboard)
await bot.edit_message_text(
chat_id=update.chat.id,
text=Translation.EXTRACT_ZIP_STEP_TWO,
message_id=a.message_id,
reply_markup=reply_markup,
)
else:
await bot.send_message(
chat_id=update.chat.id,
text=Translation.EXTRACT_ZIP_INTRO_ONE,
reply_to_message_id=update.message_id
)