Skip to content

Commit 1280550

Browse files
authored
fix: download error (#88)
* fix: download error
1 parent d0d576f commit 1280550

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

media_downloader.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,11 @@ async def _get_media_meta(
261261

262262
async def add_download_task(message: pyrogram.types.Message, node: TaskNode):
263263
"""Add Download task"""
264+
if message.empty:
265+
return False
264266
await queue.put((message, node))
265267
node.total_task += 1
268+
return True
266269

267270

268271
async def download_task(
@@ -511,8 +514,8 @@ async def download_chat_task(
511514
)
512515

513516
for message in skipped_messages:
514-
await add_download_task(message, node)
515-
chat_download_config.total_task += 1
517+
if not await add_download_task(message, node):
518+
chat_download_config.downloaded_ids.append(message.id)
516519

517520
async for message in messages_iter: # type: ignore
518521
meta_data = MetaData()
@@ -525,13 +528,13 @@ async def download_chat_task(
525528
caption = app.get_caption_name(node.chat_id, message.media_group_id)
526529
set_meta_data(meta_data, message, caption)
527530

528-
if not app.need_skip_message(chat_download_config, message.id, meta_data):
529-
await add_download_task(message, node)
530-
chat_download_config.total_task += 1
531-
else:
531+
if not app.need_skip_message(
532+
chat_download_config, message.id, meta_data
533+
) and await add_download_task(message, node):
532534
chat_download_config.downloaded_ids.append(message.id)
533535

534536
chat_download_config.need_check = True
537+
chat_download_config.total_task = node.total_task
535538
node.is_running = True
536539

537540

module/app.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,14 @@ def update_config(self, immediate: bool = True):
576576
range(before_last_read_message_id, value.last_read_message_id + 1)
577577
)
578578
unfinished_ids -= set(value.downloaded_ids)
579+
unfinished_ids -= set({0})
579580

580581
self.chat_download_config[key].ids_to_retry = list(unfinished_ids)
581582

582583
if idx >= len(self.app_data["chat"]):
583584
self.app_data["chat"].append({})
584585

585-
if len(value.downloaded_ids) > 0:
586+
if value.finish_task:
586587
self.config["chat"][idx]["last_read_message_id"] = (
587588
value.last_read_message_id + 1
588589
)

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#https://github.com/tangyoha/pyrogram/archive/refs/tags/v2.0.69.zip
2-
#pyrogram=2.0.106
2+
#pyrogram==2.0.106
33
https://github.com/tangyoha/pyrogram/archive/refs/heads/master_v2.0.69_1.zip
44
PyYAML==6.0
55
rich==12.5.1

tests/module/test_app.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ def test_app(self):
3131
app.chat_download_config[123].last_read_message_id = 13
3232
app.chat_download_config[123].failed_ids.append(6)
3333
app.chat_download_config[123].ids_to_retry.append(7)
34+
# download success
3435
app.chat_download_config[123].downloaded_ids.append(8)
36+
app.chat_download_config[123].finish_task += 1
37+
# download success
3538
app.chat_download_config[123].downloaded_ids.append(10)
39+
app.chat_download_config[123].finish_task += 1
40+
# not exist message
3641
app.chat_download_config[123].downloaded_ids.append(13)
3742
app.config["chat"] = [{"chat_id": 123, "last_read_message_id": 5}]
3843

tests/test_common.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, **kwargs):
3131
self.media_group_id = kwargs.get("media_group_id", None)
3232
self.caption = kwargs.get("caption", None)
3333
self.text = None
34+
self.empty = kwargs.get("empty", False)
3435

3536
if kwargs.get("dis_chat") == None:
3637
self.chat = Chat(

tests/test_media_downloader.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
)
2626
from module.app import Application, DownloadStatus, TaskNode
2727
from module.cloud_drive import CloudDriveConfig
28-
from module.pyrogram_extension import record_download_status, reset_download_cache
28+
from module.pyrogram_extension import (
29+
get_extension,
30+
record_download_status,
31+
reset_download_cache,
32+
)
2933

3034
from .test_common import (
3135
Chat,
@@ -359,6 +363,7 @@ async def edit_message_text(self, *args, **kwargs):
359363

360364

361365
@mock.patch("media_downloader.get_extension", new=get_extension)
366+
@mock.patch("module.pyrogram_extension.get_extension", new=get_extension)
362367
@mock.patch("media_downloader.fetch_message", new=new_fetch_message)
363368
@mock.patch("media_downloader.get_chat_history_v2", new=get_chat_history)
364369
@mock.patch("media_downloader.RETRY_TIME_OUT", new=0)
@@ -972,22 +977,6 @@ def test_issues_311(self):
972977

973978
self.assertEqual(res, (DownloadStatus.SkipDownload, None))
974979

975-
@mock.patch(
976-
"media_downloader._exec_loop",
977-
new=raise_keyboard_interrupt,
978-
)
979-
@mock.patch("media_downloader.pyrogram.Client", new=MockClient)
980-
@mock.patch("media_downloader.RETRY_TIME_OUT", new=1)
981-
@mock.patch("media_downloader.logger")
982-
def test_main(self, mock_logger):
983-
rest_app(MOCK_CONF)
984-
985-
main()
986-
987-
mock_logger.success.assert_called_with(
988-
"Updated last read message_id to config file,total download 0, total upload file 0"
989-
)
990-
991980
@mock.patch("media_downloader.pyrogram.Client", new=MockClient)
992981
@mock.patch("media_downloader.RETRY_TIME_OUT", new=1)
993982
@mock.patch("media_downloader.logger")

0 commit comments

Comments
 (0)