forked from tangyoha/telegram_media_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_downloader.py
632 lines (525 loc) · 18.7 KB
/
media_downloader.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"""Downloads media from telegram."""
import asyncio
import logging
import os
import shutil
import threading
import time
from typing import List, Optional, Tuple, Union
import pyrogram
from loguru import logger
from pyrogram.types import Audio, Document, Photo, Video, VideoNote, Voice
from rich.logging import RichHandler
from module.app import Application, ChatDownloadConfig, DownloadStatus, TaskNode
from module.bot import start_download_bot, stop_download_bot
from module.download_stat import update_download_status
from module.get_chat_history_v2 import get_chat_history_v2
from module.language import _t
from module.pyrogram_extension import (
fetch_message,
get_extension,
record_download_status,
report_bot_download_status,
set_max_concurrent_transmissions,
set_meta_data,
upload_telegram_chat,
)
from module.web import get_flask_app
from utils.format import truncate_filename, validate_title
from utils.log import LogFilter
from utils.meta import print_meta
from utils.meta_data import MetaData
from utils.updates import check_for_updates
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler()],
)
CONFIG_NAME = "config.yaml"
DATA_FILE_NAME = "data.yaml"
APPLICATION_NAME = "media_downloader"
app = Application(CONFIG_NAME, DATA_FILE_NAME, APPLICATION_NAME)
logger.add(
os.path.join(app.log_file_path, "tdl.log"),
rotation="10 MB",
retention="10 days",
level="DEBUG",
)
queue: asyncio.Queue = asyncio.Queue()
RETRY_TIME_OUT = 3
logging.getLogger("pyrogram.session.session").addFilter(LogFilter())
logging.getLogger("pyrogram.client").addFilter(LogFilter())
logging.getLogger("pyrogram").setLevel(logging.WARNING)
def _check_download_finish(media_size: int, download_path: str, ui_file_name: str):
"""Check download task if finish
Parameters
----------
media_size: int
The size of the downloaded resource
download_path: str
Resource download hold path
ui_file_name: str
Really show file name
"""
download_size = os.path.getsize(download_path)
if media_size == download_size:
logger.success(f"{_t('Successfully downloaded')} - {ui_file_name}")
else:
logger.warning(
f"{_t('Media downloaded with wrong size')}: "
f"{download_size}, {_t('actual')}: "
f"{media_size}, {_t('file name')}: {ui_file_name}"
)
os.remove(download_path)
raise pyrogram.errors.exceptions.bad_request_400.BadRequest()
def _move_to_download_path(temp_download_path: str, download_path: str):
"""Move file to download path
Parameters
----------
temp_download_path: str
Temporary download path
download_path: str
Download path
"""
directory, _ = os.path.split(download_path)
os.makedirs(directory, exist_ok=True)
shutil.move(temp_download_path, download_path)
def _check_timeout(retry: int, _: int):
"""Check if message download timeout, then add message id into failed_ids
Parameters
----------
retry: int
Retry download message times
message_id: int
Try to download message 's id
"""
if retry == 2:
return True
return False
def _can_download(_type: str, file_formats: dict, file_format: Optional[str]) -> bool:
"""
Check if the given file format can be downloaded.
Parameters
----------
_type: str
Type of media object.
file_formats: dict
Dictionary containing the list of file_formats
to be downloaded for `audio`, `document` & `video`
media types
file_format: str
Format of the current file to be downloaded.
Returns
-------
bool
True if the file format can be downloaded else False.
"""
if _type in ["audio", "document", "video"]:
allowed_formats: list = file_formats[_type]
if not file_format in allowed_formats and allowed_formats[0] != "all":
return False
return True
def _is_exist(file_path: str) -> bool:
"""
Check if a file exists and it is not a directory.
Parameters
----------
file_path: str
Absolute path of the file to be checked.
Returns
-------
bool
True if the file exists else False.
"""
return not os.path.isdir(file_path) and os.path.exists(file_path)
# pylint: disable = R0912
async def _get_media_meta(
chat_id: Union[int, str],
message: pyrogram.types.Message,
media_obj: Union[Audio, Document, Photo, Video, VideoNote, Voice],
_type: str,
) -> Tuple[str, str, Optional[str]]:
"""Extract file name and file id from media object.
Parameters
----------
media_obj: Union[Audio, Document, Photo, Video, VideoNote, Voice]
Media object to be extracted.
_type: str
Type of media object.
Returns
-------
Tuple[str, str, Optional[str]]
file_name, file_format
"""
if _type in ["audio", "document", "video"]:
# pylint: disable = C0301
file_format: Optional[str] = media_obj.mime_type.split("/")[-1] # type: ignore
else:
file_format = None
file_name = None
temp_file_name = None
dirname = validate_title(f"{chat_id}")
if message.chat and message.chat.title:
dirname = validate_title(f"{message.chat.title}")
if message.date:
datetime_dir_name = message.date.strftime("%Y_%m")
else:
datetime_dir_name = "0"
if _type in ["voice", "video_note"]:
# pylint: disable = C0209
file_format = media_obj.mime_type.split("/")[-1] # type: ignore
file_save_path = app.get_file_save_path(_type, dirname, datetime_dir_name)
file_name = "{} - {}_{}.{}".format(
message.id,
_type,
media_obj.date.isoformat(), # type: ignore
file_format,
)
file_name = validate_title(file_name)
temp_file_name = os.path.join(app.temp_save_path, dirname, file_name)
file_name = os.path.join(file_save_path, file_name)
else:
file_name = getattr(media_obj, "file_name", None)
caption = getattr(message, "caption", None)
file_name_suffix = ".unknown"
if not file_name:
file_name_suffix = get_extension(
media_obj.file_id, getattr(media_obj, "mime_type", "")
)
else:
# file_name = file_name.split(".")[0]
_, file_name_without_suffix = os.path.split(os.path.normpath(file_name))
file_name, file_name_suffix = os.path.splitext(file_name_without_suffix)
if not file_name_suffix:
file_name_suffix = get_extension(
media_obj.file_id, getattr(media_obj, "mime_type", "")
)
if caption:
caption = validate_title(caption)
app.set_caption_name(chat_id, message.media_group_id, caption)
else:
caption = app.get_caption_name(chat_id, message.media_group_id)
if not file_name and message.photo:
file_name = f"{message.photo.file_unique_id}"
gen_file_name = (
app.get_file_name(message.id, file_name, caption) + file_name_suffix
)
file_save_path = app.get_file_save_path(_type, dirname, datetime_dir_name)
temp_file_name = os.path.join(app.temp_save_path, dirname, gen_file_name)
file_name = os.path.join(file_save_path, gen_file_name)
return truncate_filename(file_name), truncate_filename(temp_file_name), file_format
async def add_download_task(message: pyrogram.types.Message, node: TaskNode):
"""Add Download task"""
if message.empty:
return False
await queue.put((message, node))
node.total_task += 1
return True
async def download_task(
client: pyrogram.Client, message: pyrogram.types.Message, node: TaskNode
):
"""Download and Forward media"""
download_status, file_name = await download_media(
client, message, app.media_types, app.file_formats, node.chat_id, node.task_id
)
if not node.bot:
app.set_download_id(node.chat_id, message.id, download_status)
file_size = os.path.getsize(file_name) if file_name else 0
await upload_telegram_chat(
client,
node.upload_user if node.upload_user else client,
app,
node,
message,
file_name,
download_status,
)
# rclone upload
if (
not node.upload_telegram_chat_id
and download_status is DownloadStatus.SuccessDownload
):
if await app.upload_file(file_name):
node.upload_success_count += 1
await report_bot_download_status(
node.bot,
node,
download_status,
file_size,
)
# pylint: disable = R0915,R0914
@record_download_status
async def download_media(
client: pyrogram.client.Client,
message: pyrogram.types.Message,
media_types: List[str],
file_formats: dict,
chat_id: Union[int, str],
task_id: int = 0,
):
"""
Download media from Telegram.
Each of the files to download are retried 3 times with a
delay of 5 seconds each.
Parameters
----------
client: pyrogram.client.Client
Client to interact with Telegram APIs.
message: pyrogram.types.Message
Message object retrieved from telegram.
media_types: list
List of strings of media types to be downloaded.
Ex : `["audio", "photo"]`
Supported formats:
* audio
* document
* photo
* video
* voice
file_formats: dict
Dictionary containing the list of file_formats
to be downloaded for `audio`, `document` & `video`
media types.
Returns
-------
int
Current message id.
"""
# pylint: disable = R0912
file_name: str = ""
ui_file_name: str = ""
task_start_time: float = time.time()
media_size = 0
_media = None
message = await fetch_message(client, message)
try:
for _type in media_types:
_media = getattr(message, _type, None)
if _media is None:
continue
file_name, temp_file_name, file_format = await _get_media_meta(
chat_id, message, _media, _type
)
media_size = getattr(_media, "file_size", 0)
ui_file_name = file_name
if app.hide_file_name:
ui_file_name = f"****{os.path.splitext(file_name)[-1]}"
if _can_download(_type, file_formats, file_format):
if _is_exist(file_name):
file_size = os.path.getsize(file_name)
if file_size or file_size == media_size:
logger.info(
f"id={message.id} {ui_file_name} "
f"{_t('already download,download skipped')}.\n"
)
return DownloadStatus.SkipDownload, None
else:
return DownloadStatus.SkipDownload, None
break
except Exception as e:
logger.error(
f"Message[{message.id}]: "
f"{_t('could not be downloaded due to following exception')}:\n[{e}].",
exc_info=True,
)
return DownloadStatus.FailedDownload, None
if _media is None:
return DownloadStatus.SkipDownload, None
message_id = message.id
for retry in range(3):
try:
temp_download_path = await client.download_media(
message,
file_name=temp_file_name,
progress=lambda down_byte, total_byte: update_download_status(
chat_id,
message_id,
down_byte,
total_byte,
ui_file_name,
task_start_time,
task_id,
),
)
if temp_download_path and isinstance(temp_download_path, str):
_check_download_finish(media_size, temp_download_path, ui_file_name)
await asyncio.sleep(0.5)
_move_to_download_path(temp_download_path, file_name)
# TODO: if not exist file size or media
return DownloadStatus.SuccessDownload, file_name
except pyrogram.errors.exceptions.bad_request_400.BadRequest:
logger.warning(
f"Message[{message.id}]: {_t('file reference expired, refetching')}..."
)
await asyncio.sleep(RETRY_TIME_OUT)
message = await fetch_message(client, message)
if _check_timeout(retry, message.id):
# pylint: disable = C0301
logger.error(
f"Message[{message.id}]: "
f"{_t('file reference expired for 3 retries, download skipped.')}"
)
except pyrogram.errors.exceptions.flood_420.FloodWait as wait_err:
await asyncio.sleep(wait_err.value)
logger.warning("Message[{}]: FlowWait {}", message.id, wait_err.value)
_check_timeout(retry, message.id)
except TypeError:
# pylint: disable = C0301
logger.warning(
f"{_t('Timeout Error occurred when downloading Message')}[{message.id}], "
f"{_t('retrying after')} {RETRY_TIME_OUT} {_t('seconds')}"
)
await asyncio.sleep(RETRY_TIME_OUT)
if _check_timeout(retry, message.id):
logger.error(
f"Message[{message.id}]: {_t('Timing out after 3 reties, download skipped.')}"
)
except Exception as e:
# pylint: disable = C0301
logger.error(
f"Message[{message.id}]: "
f"{_t('could not be downloaded due to following exception')}:\n[{e}].",
exc_info=True,
)
break
return DownloadStatus.FailedDownload, None
def _load_config():
"""Load config"""
app.load_config()
def _check_config() -> bool:
"""Check config"""
print_meta(logger)
try:
_load_config()
except Exception as e:
logger.exception(f"load config error: {e}")
return False
return True
async def worker(client: pyrogram.client.Client):
"""Work for download task"""
while app.is_running:
try:
item = await queue.get()
message = item[0]
node: TaskNode = item[1]
if node.client:
await download_task(node.client, message, node)
else:
await download_task(client, message, node)
except Exception as e:
logger.exception(f"{e}")
async def download_chat_task(
client: pyrogram.Client,
chat_download_config: ChatDownloadConfig,
node: TaskNode,
):
"""Download all task"""
messages_iter = get_chat_history_v2(
client,
node.chat_id,
limit=node.limit,
offset_id=chat_download_config.last_read_message_id,
reverse=True,
)
if chat_download_config.ids_to_retry:
logger.info(f"{_t('Downloading files failed during last run')}...")
skipped_messages: list = await client.get_messages( # type: ignore
chat_id=node.chat_id, message_ids=chat_download_config.ids_to_retry
)
for message in skipped_messages:
if not await add_download_task(message, node):
chat_download_config.downloaded_ids.append(message.id)
async for message in messages_iter: # type: ignore
meta_data = MetaData()
caption = message.caption
if caption:
caption = validate_title(caption)
app.set_caption_name(node.chat_id, message.media_group_id, caption)
else:
caption = app.get_caption_name(node.chat_id, message.media_group_id)
set_meta_data(meta_data, message, caption)
if not app.need_skip_message(
chat_download_config, message.id, meta_data
) and await add_download_task(message, node):
chat_download_config.downloaded_ids.append(message.id)
chat_download_config.need_check = True
chat_download_config.total_task = node.total_task
node.is_running = True
async def download_all_chat(client: pyrogram.Client):
"""Download All chat"""
for key, value in app.chat_download_config.items():
node = TaskNode(chat_id=key)
try:
await download_chat_task(client, value, node)
except Exception as e:
logger.exception(f"{e}")
finally:
value.need_check = True
async def run_until_all_task_finish():
"""Normal download"""
while True:
finish: bool = True
for _, value in app.chat_download_config.items():
if not value.need_check or value.total_task != value.finish_task:
finish = False
if finish:
break
await asyncio.sleep(1)
def _exec_loop():
"""Exec loop"""
if app.bot_token:
app.loop.run_forever()
else:
app.loop.run_until_complete(run_until_all_task_finish())
def main():
"""Main function of the downloader."""
tasks = []
client = pyrogram.Client(
"media_downloader",
api_id=app.api_id,
api_hash=app.api_hash,
proxy=app.proxy,
workdir=app.session_file_path,
)
try:
app.pre_run()
threading.Thread(
target=get_flask_app().run, daemon=True, args=(app.web_host, app.web_port)
).start()
set_max_concurrent_transmissions(client, app.max_concurrent_transmissions)
client.start()
logger.success(_t("Successfully started (Press Ctrl+C to stop)"))
app.loop.create_task(download_all_chat(client))
for _ in range(app.max_download_task):
task = app.loop.create_task(worker(client))
tasks.append(task)
if app.bot_token:
app.loop.create_task(
start_download_bot(app, client, add_download_task, download_chat_task)
)
_exec_loop()
except KeyboardInterrupt:
logger.info(_t("KeyboardInterrupt"))
except Exception as e:
logger.exception("{}", e)
finally:
client.stop()
app.is_running = False
for task in tasks:
task.cancel()
logger.info(_t("Stopped!"))
check_for_updates()
logger.info(f"{_t('update config')}......")
app.update_config()
if app.bot_token:
stop_download_bot()
logger.success(
f"{_t('Updated last read message_id to config file')},"
f"{_t('total download')} {app.total_download_task}, "
f"{_t('total upload file')} "
f"{app.cloud_drive_config.total_upload_success_file_count}"
)
if __name__ == "__main__":
if _check_config():
main()