Skip to content

Bulk asynchronous downloader for multiple file hosts

Add file lock, and fix attachment folder

Jules-WinnfieldX01ff1b8055e2
diff --git a/cyberdrop_dl/utils/crawlers/Thotsbay_Spider.py b/cyberdrop_dl/utils/crawlers/Thotsbay_Spider.py
index aad4fbb37..a9a4396e2 100644
--- a/cyberdrop_dl/utils/crawlers/Thotsbay_Spider.py
+++ b/cyberdrop_dl/utils/crawlers/Thotsbay_Spider.py
@@ -159,13 +159,12 @@ class ThotsbayCrawler():
link = link[:-1]
if link.startswith('/'):
link = domain / link[1:]
- in_prog_title = temp_title + "/Attachments" if self.separate_posts else "Attachments"
+ in_prog_title = temp_title + "/Attachments"
await Cascade.add_to_album(url.host, in_prog_title, URL(link), url)
# Gyfcat / Redgifs / Imgur
# NOTE IMGUR SUPPORT IS NOT WORKING AS OF NOW
- links = post_content.select(
- "span[data-s9e-mediaembed-iframe]")
+ links = post_content.select("span[data-s9e-mediaembed-iframe]")
for link in links:
embed_data = link.get("data-s9e-mediaembed-iframe")
# Replace gyfcat/redgifs/imgur embed url escape chars
@@ -185,7 +184,7 @@ class ThotsbayCrawler():
for link_title_bundle in forum_direct_urls:
link = link_title_bundle[0]
temp_title = link_title_bundle[1]
- in_prog_title = temp_title + "/Attachments" if self.separate_posts else "Attachments"
+ in_prog_title = temp_title + "/Attachments"
if str(link).endswith("/"):
link = URL(str(link)[:-1])
if 'attachments' in link.parts:
diff --git a/cyberdrop_dl/utils/data_classes.py b/cyberdrop_dl/utils/data_classes.py
index d75224a4e..eaa704ea7 100644
--- a/cyberdrop_dl/utils/data_classes.py
+++ b/cyberdrop_dl/utils/data_classes.py
@@ -19,6 +19,22 @@ class AlbumItem:
self.title = new_title
+@dataclass
+class FileLock:
+ locked_files = []
+
+ async def check_lock(self, filename):
+ if filename in self.locked_files:
+ return True
+ return False
+
+ async def add_lock(self, filename):
+ self.locked_files.append(filename)
+
+ async def remove_lock(self, filename):
+ self.locked_files.remove(filename)
+
+
@dataclass
class DomainItem:
domain: str
diff --git a/cyberdrop_dl/utils/downloaders.py b/cyberdrop_dl/utils/downloaders.py
index 39545fafe..dfab60c49 100644
--- a/cyberdrop_dl/utils/downloaders.py
+++ b/cyberdrop_dl/utils/downloaders.py
@@ -16,7 +16,7 @@ from yarl import URL
from .base_functions import FILE_FORMATS, MAX_FILENAME_LENGTH, log, logger, sanitize, ssl_context, user_agent
from .sql_helper import SQLHelper
-from .data_classes import AlbumItem, CascadeItem
+from .data_classes import AlbumItem, CascadeItem, FileLock
class FailureException(Exception):
@@ -78,6 +78,7 @@ class Downloader:
self.title = title
self.SQL_helper = SQL_helper
+ self.FileLocker = FileLock()
self.attempts = attempts
self.current_attempt = 0
@@ -121,6 +122,10 @@ class Downloader:
if (self.folder / self.title / filename).exists():
return
+ while await self.FileLocker.check_lock(filename):
+ await asyncio.sleep(2)
+ await self.FileLocker.add_lock(filename)
+
# Skip based on CLI arg.
if self.exclude_videos:
if ext in FILE_FORMATS['Videos']:
@@ -176,6 +181,7 @@ class Downloader:
progress.update(len(chunk))
resp.close()
await self.rename_file(filename)
+ await self.FileLocker.remove_lock(filename)
except (aiohttp.client_exceptions.ClientPayloadError, aiohttp.client_exceptions.ClientOSError,
aiohttp.client_exceptions.ServerDisconnectedError, asyncio.TimeoutError,
aiohttp.client_exceptions.ClientResponseError, FailureException) as e: