-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtelegram.py
304 lines (243 loc) · 11.1 KB
/
telegram.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os
import uuid
from files import *
from config import *
from tqdm import tqdm
from db import Database
from termcolor import colored
from prettytable import PrettyTable
from telethon import TelegramClient, events, sync
VERBOSE = get_verbose()
DB_FILE = get_db_file()
PHONE = get_phone()
if VERBOSE:
import logging
logging.basicConfig(level=logging.DEBUG)
class Telegram:
def __init__(self):
self._api_id = get_telegram_api_id()
self._api_hash = get_telegram_api_hash()
script_dir = os.path.dirname(os.path.abspath(__file__))
session_path = os.path.join(script_dir, "anon.session")
if VERBOSE:
print(colored("[INFO] Initializing Telegram Client...", "magenta"))
self._client = TelegramClient(session=session_path, api_id=self._api_id, api_hash=self._api_hash)
if VERBOSE:
print(colored(f"[INFO] Connecting to Telegram via \"{PHONE}\""))
self._client.start(phone=PHONE)
if not DB_FILE:
self._db = Database("files.db")
else:
if VERBOSE:
print(colored(f"[INFO] Using \"{DB_FILE}\" as database location..."))
self._db = Database(DB_FILE)
@property
def api_id(self):
return self._api_id
@property
def api_hash(self):
return self._api_hash
@property
def client(self):
return self._client
@property
def db(self):
return self._db
def list_files(self):
# List all files
if VERBOSE:
print(colored("[INFO] Fetching files..."))
files = self.db.fetch()
if VERBOSE:
print(colored(f"[SUC] Found {len(list(files))} files."))
table = PrettyTable()
table.field_names = [
"ID 🌟",
"File Name 📁",
"File Path 📂",
"File Size 📌",
"Chunks 🔗",
"Type 📝",
"Uploaded 📅",
]
for file in files:
table.add_row(
[
file[0],
colored(file[1], "magenta"),
colored(file[2], "cyan"),
colored(parse_bytes(file[3]), "blue"),
colored(len(json.loads(file[4])), "green"),
colored(file[5], "yellow"),
colored(file[6], "magenta"),
]
)
print(table)
def download_file(self, file_query: str):
# Download a file
print(colored(f'[*] Searching for "{file_query}"...', "blue"))
file = self.db.find_file_by_name_or_path_or_id(file_query)
if file[0][-2] == "dir":
self.download_directory(file_query)
return
if not file:
print(colored(f"[-] File {file_query} not found.", "red"))
return
file = file[0]
chunks = json.loads(file[4])
file_name = file[1]
file_path = file[2]
print(colored(f"\n[+] Downloading {len(chunks)} chunk(s).", "magenta"))
# Download each chunk
with self.client.start() as client:
# Loop through each chunk
for chunk_num, chunk_path in enumerate(chunks):
# Build the caption, to search for the message
# that contains the chunk we need.
caption = f"{file[0]}:::::{file[2]}:::::{str(chunk_num)}:::::file"
messages = client.get_messages("me", search=caption)
# Loop through each message
for message in messages:
# Check if message contains the chunk we need
if message.message.split(":::::")[0] == file[0]:
# Download the chunk in temp directory
client.download_media(message, file=chunk_path)
# Read the chunk
with open(chunk_path, "rb") as chunk_file:
chunk = chunk_file.read()
# Append the chunk to the file
with open(file_path, "ab") as file:
file.write(chunk)
print(colored(f"[+] Downloaded file \"{file_name}\" to \"{file_path}\".", "green"))
def remove_file(self, file_query: str):
# Remove a file
print(colored(f"[+] Removing file {file_query}...", "green"))
file = self.db.find_file_by_name_or_path_or_id(file_query)
if not file:
print(colored(f"[-] File/Directory {file_query} not found.", "red"))
return
if file[0][-2] == "file":
with self.client.start() as client:
# Get all messages
messages = client.get_messages("me")
# Find the message with the absolute path in the caption
for message in messages:
if message.message:
if file[0][1]:
print(colored(f"[+] Deleting message {message.id}...", "magenta"))
client.delete_messages("me", message.id)
self.db.remove(file[0][0])
print(colored(f"[+] Removed file {file_query}.", "green"))
else:
with self.client.start() as client:
# Get all messages
messages = client.get_messages("me")
# Find the message with the absolute path in the caption
for message in messages:
if message.message:
if file[0][1]:
print(colored(f"[+] Deleting message {message.id}...", "magenta"))
client.delete_messages("me", message.id)
self.db.remove(file[0][0])
print(colored(f"[+] Removed directory {file_query}.", "green"))
def upload_file(
self,
current_dir: str,
file_path: str,
file_name: str,
) -> str:
print(colored(f"[*] Uploading {file_name} to Telegram...", "magenta"))
# Upload file to Telegram and get the file URL
absolute_path = os.path.abspath(os.path.join(current_dir, file_path))
# Split the file into chunks
chunks = split_file_into_chunks(absolute_path)
id = uuid.uuid4().__str__().replace("-", "")
print(colored(f"\n[+] Split {file_name} into {len(chunks)} chunk(s).", "cyan"))
# Upload each chunk to Telegram
with self.client.start() as client:
print()
progress = tqdm(total=len(chunks))
for chunk_num, chunk_path in enumerate(chunks):
caption = f"{id}::::::{absolute_path}::::::{str(chunk_num)}:::::file"
client.send_file(
"me",
chunk_path,
caption=caption,
progress_callback=lambda current, total: progress.update(1),
)
self.db.insert(
id, file_name, absolute_path, os.path.getsize(absolute_path), chunks, "file"
)
def upload_directory(self, current_dir: str, dir_path: str, dir_name: str):
print(colored(f"[*] Uploading {dir_path} to Telegram...", "magenta"))
# Upload directory to Telegram and get the file URL
absolute_path = os.path.abspath(os.path.join(current_dir, dir_path))
# List all files in the directory
files = os.listdir(absolute_path)
progress = tqdm(total=len(files))
for file in files:
file_path = os.path.join(absolute_path, file)
if os.path.isfile(file_path):
# Split the file into chunks
chunks = split_file_into_chunks(file_path)
id = uuid.uuid4().__str__().replace("-", "")
if VERBOSE:
print(colored(f"\n\t[+] Split {file} into {len(chunks)} chunk(s).", "cyan"))
# Upload each chunk to Telegram
with self.client.start() as client:
print()
for chunk_num, chunk_path in enumerate(chunks):
if VERBOSE:
print(colored(
f"\t[+] Uploading chunk {chunk_num} of {file} to Telegram...", "magenta"
))
caption = f"{id}::::::{file_path}::::::{str(chunk_num)}:::::dir"
client.send_file(
"me",
chunk_path,
caption=caption,
progress_callback=lambda current, total: progress.update(1 if current == total else 0),
)
self.db.insert(
id, file, file_path, os.path.getsize(file_path), chunks, "dir"
)
print(colored(f"\n[+] Uploaded {dir_name} to Telegram.", "green"))
def download_directory(self, dir_query: str):
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
# Download a directory
files = self.db.find_file_by_name_or_path_or_id("dir")
dir_path = os.path.dirname(files[0][2])
if not os.path.exists(dir_path):
os.makedirs(dir_path)
if not dir:
print(colored(f"[-] Directory {dir_query} not found.", "red"))
return
if VERBOSE:
print(f"[INFO] Downloading {len(files)} files from {dir_path}.")
# Loop through each file in the directory, and see if it's part of the directory
for file in files:
if VERBOSE:
print(f"[INFO] Downloading {file[1]}, with ID \"{file[0]}\".")
if dir_query in file[2]:
chunks = json.loads(file[4])
file_name = file[1]
file_path = file[2]
print(colored(f"[+] Downloading {len(chunks)} chunk(s) to \"{file_name}\".", "magenta"))
# Download each chunk
with self.client.start() as client:
for chunk_num, chunk_path in enumerate(chunks):
caption = f"{file[0]}:::::{file[2]}:::::{str(chunk_num)}:::::dir"
messages = client.get_messages("me", search=caption)
for message in messages:
if message.message.split(":::::")[0] == file[0]:
temp_chunk_path = os.path.join(ROOT_DIR, "temp", f"{file_name}.part{chunk_num}")
if VERBOSE:
print(colored(f"\t[INFO] Downloading chunk {chunk_num} to {temp_chunk_path}.", "yellow"))
client.download_media(message, file=temp_chunk_path)
with open(temp_chunk_path, "rb") as chunk_file:
chunk = chunk_file.read()
print(colored(f"\t[INFO] Appending chunk {chunk_num} to {file_path}.", "green"))
with open(file_path, "ab") as file:
# Append the chunk to the file
file.write(chunk)
print(colored(f"[+] Downloaded directory \"{dir_query}\"", "green"))