Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
Flatten code a little
Arkadiy Illarionov78ac85dc955a
diff --git a/cyberdrop_dl/base_functions/base_functions.py b/cyberdrop_dl/base_functions/base_functions.pyindex 4ab762741..85c698674 100644--- a/cyberdrop_dl/base_functions/base_functions.py+++ b/cyberdrop_dl/base_functions/base_functions.py@@ -61,16 +61,14 @@ async def get_db_path(url: URL, referer=None):"""Gets the URL path to be put into the DB and checked from the DB"""url_path = url.pathif 'anonfiles' in url.host or 'bayfiles' in url.host:- url_path = url.pathurl_path = url_path.split('/')url_path.pop(0)if len(url_path) > 1:url_path.pop(1)url_path = '/' + '/'.join(url_path)- if referer:- if "e-hentai" in referer:- url_path = url.path- url_path = url_path.split('keystamp')[0][:-1]++ if referer and "e-hentai" in referer:+ url_path = url_path.split('keystamp')[0][:-1]return url_pathdiff --git a/cyberdrop_dl/base_functions/data_classes.py b/cyberdrop_dl/base_functions/data_classes.pyindex 96bad3f8f..f6e9d9f1f 100644--- a/cyberdrop_dl/base_functions/data_classes.py+++ b/cyberdrop_dl/base_functions/data_classes.py@@ -144,11 +144,10 @@ class CascadeItem:domain.albums = new_albumsasync def extend(self, Cascade):- if Cascade:- if Cascade.domains:- for domain_str, domain in Cascade.domains.items():- for album_str, album in domain.albums.items():- await self.add_album(domain_str, album_str, album)+ if Cascade and Cascade.domains:+ for domain_str, domain in Cascade.domains.items():+ for album_str, album in domain.albums.items():+ await self.add_album(domain_str, album_str, album)async def dedupe(self):for _, domain in self.domains.items():diff --git a/cyberdrop_dl/base_functions/sql_helper.py b/cyberdrop_dl/base_functions/sql_helper.pyindex 79ebf8a26..480fca63d 100644--- a/cyberdrop_dl/base_functions/sql_helper.py+++ b/cyberdrop_dl/base_functions/sql_helper.py@@ -193,10 +193,7 @@ class SQLHelper:sql_file_check = self.curs.fetchone()if not sql_file_check:return False- elif sql_file_check[0] == 0:- return False- else:- return True+ return sql_file_check[0] != 0"""Downloader Operations"""diff --git a/cyberdrop_dl/crawlers/Bunkr_Spider.py b/cyberdrop_dl/crawlers/Bunkr_Spider.pyindex 289e4dc28..b21900c4c 100644--- a/cyberdrop_dl/crawlers/Bunkr_Spider.py+++ b/cyberdrop_dl/crawlers/Bunkr_Spider.py@@ -88,10 +88,9 @@ class BunkrCrawler:link = Nonefor script in scripts:- if script.text:- if "link.href" in script.text:- link = script.text.split('link.href = "')[-1].split('";')[0]- break+ if script.text and "link.href" in script.text:+ link = script.text.split('link.href = "')[-1].split('";')[0]+ breakif not link:raiselink = URL(link)diff --git a/cyberdrop_dl/crawlers/Coomeno_Spider.py b/cyberdrop_dl/crawlers/Coomeno_Spider.pyindex 794f3e6ef..b2a010815 100644--- a/cyberdrop_dl/crawlers/Coomeno_Spider.py+++ b/cyberdrop_dl/crawlers/Coomeno_Spider.py@@ -164,19 +164,15 @@ class CoomenoCrawler:soup = BeautifulSoup(text, 'html.parser')if self.separate_posts:- if self.include_id:- if title:- title = title + '/' + str(url.parts[-1]) + " - " + await make_title_safe(soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", ""))- else:- title = await make_title_safe(str(url.parts[-1]) + " - " + soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", ""))+ post_title = soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", "")+ prefix = f"{str(url.parts[-1])} - " if self.include_id else ""+ if title:+ title = title + '/' + await make_title_safe(prefix + post_title)else:- if title:- title = title + '/' + await make_title_safe(soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", ""))- else:- title = await make_title_safe(soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", ""))- else:- if not title:- title = await make_title_safe(soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", ""))+ title = await make_title_safe(prefix + post_title)+ elif not title:+ post_title = soup.select_one("h1[class=post__title]").text.replace('\n', '').replace("..", "")+ title = await make_title_safe(post_title)images = soup.select(images_selector)for image in images:diff --git a/cyberdrop_dl/crawlers/CyberFile_Spider.py b/cyberdrop_dl/crawlers/CyberFile_Spider.pyindex 836c2637f..6907e0277 100644--- a/cyberdrop_dl/crawlers/CyberFile_Spider.py+++ b/cyberdrop_dl/crawlers/CyberFile_Spider.py@@ -108,13 +108,12 @@ class CyberFileCrawler:script_funcs = soup.select('script')for script in script_funcs:script_text = script.text- if script_text:- if "showFileInformation" in script_text:- parta = script_text.split("showFileInformation(")- parta = [x for x in parta if x[0].isdigit()][0]- partb = parta.split(");")[0]- contentId = int(partb)- return contentId+ if script_text and "showFileInformation" in script_text:+ part_a = script_text.split("showFileInformation(")+ part_a = [x for x in part_a if x[0].isdigit()][0]+ part_b = part_a.split(");")[0]+ contentId = int(part_b)+ return contentIdreturn Noneexcept Exception as e:diff --git a/cyberdrop_dl/crawlers/Xenforo_Spider.py b/cyberdrop_dl/crawlers/Xenforo_Spider.pyindex 782f67028..cb876d9b4 100644--- a/cyberdrop_dl/crawlers/Xenforo_Spider.py+++ b/cyberdrop_dl/crawlers/Xenforo_Spider.py@@ -232,9 +232,8 @@ class XenforoCrawler:for post in posts:post_num_str = post.select_one("li[class=u-concealed] a").get('href').split('/')[-1]post_num_int = int(post_num_str.split('post-')[-1])- if post_number:- if post_number > post_num_int:- continue+ if post_number > post_num_int:+ continuetemp_title = title + "/" + post_num_str if self.separate_posts else title@@ -273,15 +272,12 @@ class XenforoCrawler:next_page = domain / next_page[1:]next_page = URL(next_page)await self.parse_simpcity(session, next_page, cascade, title, post_number)- else:- if self.output_last:- if 'page-' in url.raw_name:- last_post_url = url.parent / post_num_str- elif 'post-' in url.raw_name:- last_post_url = url.parent / post_num_str- else:- last_post_url = url / post_num_str- await write_last_post_file(self.output_last_file, str(last_post_url))+ elif self.output_last:+ if 'page-' in url.raw_name or 'post-' in url.raw_name:+ last_post_url = url.parent / post_num_str+ else:+ last_post_url = url / post_num_str+ await write_last_post_file(self.output_last_file, str(last_post_url))return titleasync def parse_socialmediagirls(self, session: ScrapeSession, url: URL, cascade: CascadeItem, title: str,@@ -308,9 +304,8 @@ class XenforoCrawler:for post in posts:post_num_str = post.select_one("li[class=u-concealed] a").get('href').split('/')[-1]post_num_int = int(post_num_str.split('post-')[-1])- if post_number:- if post_number > post_num_int:- continue+ if post_number > post_num_int:+ continuetemp_title = title + "/" + post_num_str if self.separate_posts else title@@ -348,15 +343,12 @@ class XenforoCrawler:next_page = domain / next_page[1:]next_page = URL(next_page)await self.parse_socialmediagirls(session, next_page, cascade, title, post_number)- else:- if self.output_last:- if 'page-' in url.raw_name:- last_post_url = url.parent / post_num_str- elif 'post-' in url.raw_name:- last_post_url = url.parent / post_num_str- else:- last_post_url = url / post_num_str- await write_last_post_file(self.output_last_file, str(last_post_url))+ elif self.output_last:+ if 'page-' in url.raw_name or 'post-' in url.raw_name:+ last_post_url = url.parent / post_num_str+ else:+ last_post_url = url / post_num_str+ await write_last_post_file(self.output_last_file, str(last_post_url))return titleasync def parse_xbunker(self, session: ScrapeSession, url: URL, cascade: CascadeItem, title: str,@@ -383,9 +375,8 @@ class XenforoCrawler:for post in posts:post_num_str = post.select_one("li[class=u-concealed] a").get('href').split('/')[-1]post_num_int = int(post_num_str.split('post-')[-1])- if post_number:- if post_number > post_num_int:- continue+ if post_number > post_num_int:+ continuetemp_title = title + "/" + post_num_str if self.separate_posts else title@@ -423,13 +414,10 @@ class XenforoCrawler:next_page = domain / next_page[1:]next_page = URL(next_page)await self.parse_xbunker(session, next_page, cascade, title, post_number)- else:- if self.output_last:- if 'page-' in url.raw_name:- last_post_url = url.parent / post_num_str- elif 'post-' in url.raw_name:- last_post_url = url.parent / post_num_str- else:- last_post_url = url / post_num_str- await write_last_post_file(self.output_last_file, str(last_post_url))+ elif self.output_last:+ if 'page-' in url.raw_name or 'post-' in url.raw_name:+ last_post_url = url.parent / post_num_str+ else:+ last_post_url = url / post_num_str+ await write_last_post_file(self.output_last_file, str(last_post_url))return titlediff --git a/cyberdrop_dl/downloader/downloader_utils.py b/cyberdrop_dl/downloader/downloader_utils.pyindex d7606d4a7..6c38841f5 100644--- a/cyberdrop_dl/downloader/downloader_utils.py+++ b/cyberdrop_dl/downloader/downloader_utils.py@@ -10,18 +10,14 @@ from cyberdrop_dl.base_functions.data_classes import MediaItemasync def allowed_filetype(media: MediaItem, block_images: bool, block_video: bool, block_audio: bool, block_other: bool):"""Checks whether the enclosed file is allowed to be downloaded"""ext = media.ext- if block_images:- if ext in FILE_FORMATS["Images"]:- return False- if block_video:- if ext in FILE_FORMATS["Videos"]:- return False- if block_audio:- if ext in FILE_FORMATS["Audio"]:- return False- if block_other:- if ext not in FILE_FORMATS["Images"] and ext not in FILE_FORMATS["Videos"] and ext not in FILE_FORMATS["Audio"]:- return False+ if block_images and ext in FILE_FORMATS["Images"]:+ return False+ if block_video and ext in FILE_FORMATS["Videos"]:+ return False+ if block_audio and ext in FILE_FORMATS["Audio"]:+ return False+ if block_other and ext not in (FILE_FORMATS["Images"] | FILE_FORMATS["Videos"] | FILE_FORMATS["Audio"]):+ return Falsereturn Truediff --git a/cyberdrop_dl/downloader/downloaders.py b/cyberdrop_dl/downloader/downloaders.pyindex c617de25f..63fd6abb8 100644--- a/cyberdrop_dl/downloader/downloaders.py+++ b/cyberdrop_dl/downloader/downloaders.py@@ -52,13 +52,12 @@ def retry(f):try:return await f(self, *args, **kwargs)except DownloadFailure as e:- if not self.disable_attempt_limit:- if self.current_attempt[args[3]] >= self.allowed_attempts - 1:- await self.output_failed(args[2], e)- logger.debug('Skipping %s...', args[2].url, exc_info=True)- overall_file_progress.advance(self.files.failed_files_task_id, 1)- self.files.failed_files += 1- return+ if not self.disable_attempt_limit and self.current_attempt[args[3]] >= self.allowed_attempts - 1:+ await self.output_failed(args[2], e)+ logger.debug('Skipping %s...', args[2].url, exc_info=True)+ overall_file_progress.advance(self.files.failed_files_task_id, 1)+ self.files.failed_files += 1+ returnlogger.debug(e.message)logger.debug(f'Retrying ({self.current_attempt[args[3]]}) {args[2].url}...')self.current_attempt[args[3]] += 1@@ -325,43 +324,37 @@ class Downloader:expected_size = Noneproceed = Truewhile True:- if complete_file.exists() or partial_file.exists():- if not expected_size:- expected_size = await self.download_session.get_filesize(media.url, str(media.referer),- current_throttle)- if complete_file.exists():+ if not complete_file.exists() and not partial_file.exists():+ break++ if not expected_size:+ expected_size = await self.download_session.get_filesize(media.url, str(media.referer),+ current_throttle)+ if complete_file.exists() and complete_file.stat().st_size == expected_size:+ proceed = False+ break++ downloaded_filename = await self.SQL_Helper.get_downloaded_filename(url_path, original_filename)+ if not downloaded_filename:+ complete_file, partial_file = await self.iterate_filename(complete_file, media, album)+ break++ if media.filename == downloaded_filename:+ if partial_file.exists():+ if partial_file.stat().st_size == expected_size:+ proceed = False+ partial_file.rename(complete_file)+ elif complete_file.exists():if complete_file.stat().st_size == expected_size:proceed = False- break- downloaded_filename = await self.SQL_Helper.get_downloaded_filename(url_path, original_filename)- if downloaded_filename:- if media.filename == downloaded_filename:- if partial_file.exists():- if partial_file.stat().st_size == expected_size:- proceed = False- partial_file.rename(complete_file)- break- else:- break- elif complete_file.exists():- if complete_file.stat().st_size == expected_size:- proceed = False- break- else:- complete_file, partial_file = await self.iterate_filename(complete_file, media, album)- break- else:- breakelse:- media.filename = downloaded_filename- complete_file = (self.download_dir / album / media.filename)- partial_file = complete_file.with_suffix(complete_file.suffix + '.part')- continue- else:- complete_file, partial_file = await self.iterate_filename(complete_file, media, album)- break- else:+ complete_file, partial_file = await self.iterate_filename(complete_file, media, album)break++ media.filename = downloaded_filename+ complete_file = (self.download_dir / album / media.filename)+ partial_file = complete_file.with_suffix(complete_file.suffix + '.part')+return complete_file, partial_file, proceedasync def iterate_filename(self, complete_file, media, album):@@ -370,12 +363,11 @@ class Downloader:filename = f"{complete_file.stem} ({iterations}){media.ext}"iterations += 1temp_complete_file = (self.download_dir / album / filename)- if not temp_complete_file.exists():- if not await self.SQL_Helper.check_filename(filename):- media.filename = filename- complete_file = (self.download_dir / album / media.filename)- partial_file = complete_file.with_suffix(complete_file.suffix + '.part')- break+ if not temp_complete_file.exists() and not await self.SQL_Helper.check_filename(filename):+ media.filename = filename+ complete_file = (self.download_dir / album / media.filename)+ partial_file = complete_file.with_suffix(complete_file.suffix + '.part')+ breakreturn complete_file, partial_filediff --git a/cyberdrop_dl/downloader/old_downloaders.py b/cyberdrop_dl/downloader/old_downloaders.pyindex d2d9fd9a6..0978770bb 100644--- a/cyberdrop_dl/downloader/old_downloaders.py+++ b/cyberdrop_dl/downloader/old_downloaders.py@@ -41,13 +41,12 @@ def retry(f):try:return await f(self, *args, **kwargs)except DownloadFailure as e:- if not self.disable_attempt_limit:- if self.current_attempt[args[2]] >= self.allowed_attempts - 1:- await self.output_failed(args[1], e)- logger.debug('Skipping %s...', args[1].url, exc_info=True)- self.files.failed_files += 1- self.progress.update(1)- return+ if not self.disable_attempt_limit and self.current_attempt[args[2]] >= self.allowed_attempts - 1:+ await self.output_failed(args[1], e)+ logger.debug('Skipping %s...', args[1].url, exc_info=True)+ self.files.failed_files += 1+ self.progress.update(1)+ returnlogger.debug(e.message)logger.debug(f'Retrying ({self.current_attempt[args[2]]}) {args[1].url}...')self.current_attempt[args[2]] += 1@@ -260,50 +259,42 @@ class Old_Downloader:expected_size = Noneproceed = Truewhile True:- if complete_file.exists() or partial_file.exists():- if not expected_size:- expected_size = await self.download_session.get_filesize(media.url, str(media.referer),- current_throttle)- if complete_file.exists():- if complete_file.stat().st_size == expected_size:- proceed = False- break- downloaded_filename = await self.SQL_Helper.get_downloaded_filename(url_path, original_filename)- if downloaded_filename:- if media.filename == downloaded_filename:- if complete_file.exists():- proceed = False- break- elif partial_file.exists():- if partial_file.stat().st_size == expected_size:- proceed = False- partial_file.rename(complete_file)- break- else:- break- else:- break- else:- media.filename = downloaded_filename+ if not complete_file.exists() and not partial_file.exists():+ break++ if not expected_size:+ expected_size = await self.download_session.get_filesize(media.url, str(media.referer),+ current_throttle)+ if complete_file.exists() and complete_file.stat().st_size == expected_size:+ proceed = False+ break++ downloaded_filename = await self.SQL_Helper.get_downloaded_filename(url_path, original_filename)+ if not downloaded_filename:+ iterations = 1+ while True:+ filename = f"{complete_file.stem} ({iterations}){media.ext}"+ iterations += 1+ temp_complete_file = (self.download_dir / album / filename)+ if not temp_complete_file.exists() and not await self.SQL_Helper.check_filename(filename):+ media.filename = filenamecomplete_file = (self.download_dir / album / media.filename)partial_file = complete_file.with_suffix(complete_file.suffix + '.part')- continue- else:-- iterations = 1- while True:- filename = f"{complete_file.stem} ({iterations}){media.ext}"- iterations += 1- temp_complete_file = (self.download_dir / album / filename)- if not temp_complete_file.exists():- if not await self.SQL_Helper.check_filename(filename):- media.filename = filename- complete_file = (self.download_dir / album / media.filename)- partial_file = complete_file.with_suffix(complete_file.suffix + '.part')- break- break- else:+ break+ break++ if media.filename == downloaded_filename:+ if complete_file.exists():+ proceed = False+ elif partial_file.exists() and partial_file.stat().st_size == expected_size:+ proceed = False+ partial_file.rename(complete_file)break++ media.filename = downloaded_filename+ complete_file = (self.download_dir / album / media.filename)+ partial_file = complete_file.with_suffix(complete_file.suffix + '.part')+return complete_file, partial_file, proceed