@@ -48,7 +48,7 @@ async def execute_item_download(
4848
4949 # Download item
5050 if item_download_link :
51- downloader = MediaDownloader (
51+ media_downloader = MediaDownloader (
5252 session_info = self .session_info ,
5353 download_info = DownloadInfo (
5454 download_link = item_download_link ,
@@ -58,52 +58,53 @@ async def execute_item_download(
5858 live_manager = self .live_manager ,
5959 )
6060
61- failed_download = await asyncio .to_thread (downloader .download )
61+ failed_download = await asyncio .to_thread (media_downloader .download )
6262 if failed_download :
6363 self .failed_downloads .append (failed_download )
6464
65- async def retry_failed_download (
65+ async def download_album (self , max_workers : int = MAX_WORKERS ) -> None :
66+ """Handle the album download."""
67+ num_tasks = len (self .album_info .item_pages )
68+ self .live_manager .add_overall_task (
69+ description = self .album_info .album_id ,
70+ num_tasks = num_tasks ,
71+ )
72+
73+ # Create tasks for downloading each item in the album
74+ semaphore = asyncio .Semaphore (max_workers )
75+ tasks = [
76+ self .execute_item_download (item_page , current_task , semaphore )
77+ for current_task , item_page in enumerate (self .album_info .item_pages )
78+ ]
79+ await asyncio .gather (* tasks )
80+
81+ # If there are failed downloads, process them after all downloads are complete
82+ if self .failed_downloads :
83+ await self ._process_failed_downloads ()
84+
85+ # Private methods
86+ async def _retry_failed_download (
6687 self ,
6788 task : int ,
6889 filename : str ,
6990 download_link : str ,
7091 ) -> None :
7192 """Handle failed downloads and retries them."""
72- downloader = MediaDownloader (
93+ media_downloader = MediaDownloader (
7394 session_info = self .session_info ,
7495 download_info = DownloadInfo (download_link , filename , task ),
7596 live_manager = self .live_manager ,
7697 retries = 1 , # Retry once for failed downloads
7798 )
7899 # Run the synchronous download function in a separate thread
79- await asyncio .to_thread (downloader .download )
100+ await asyncio .to_thread (media_downloader .download )
80101
81- async def process_failed_downloads (self ) -> None :
102+ async def _process_failed_downloads (self ) -> None :
82103 """Process any failed downloads after the initial attempt."""
83104 for data in self .failed_downloads :
84- await self .retry_failed_download (
105+ await self ._retry_failed_download (
85106 data ["id" ],
86107 data ["filename" ],
87108 data ["download_link" ],
88109 )
89110 self .failed_downloads .clear ()
90-
91- async def download_album (self , max_workers : int = MAX_WORKERS ) -> None :
92- """Handle the album download."""
93- num_tasks = len (self .album_info .item_pages )
94- self .live_manager .add_overall_task (
95- description = self .album_info .album_id ,
96- num_tasks = num_tasks ,
97- )
98-
99- # Create tasks for downloading each item in the album
100- semaphore = asyncio .Semaphore (max_workers )
101- tasks = [
102- self .execute_item_download (item_page , current_task , semaphore )
103- for current_task , item_page in enumerate (self .album_info .item_pages )
104- ]
105- await asyncio .gather (* tasks )
106-
107- # If there are failed downloads, process them after all downloads are complete
108- if self .failed_downloads :
109- await self .process_failed_downloads ()
0 commit comments