Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Move all file lock transactions to run instead of sporatically throug…
Browse files Browse the repository at this point in the history
…h entire downloader and wrapper.
  • Loading branch information
Jules-WinnfieldX committed Feb 28, 2024
1 parent e7236f2 commit 5715a79
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cyberdrop_dl/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "5.1.76"
__version__ = "5.1.77"
18 changes: 6 additions & 12 deletions cyberdrop_dl/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ async def wrapper(self, *args, **kwargs):

except InvalidContentTypeFailure as e:
media_item = args[0]
await self._file_lock.release_lock(media_item.file_lock_reference_name)
if not isinstance(media_item.download_task_id, Field):
await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id)
await log(f"Download Failed: {media_item.url} received Invalid Content", 40)
Expand All @@ -79,7 +78,6 @@ async def wrapper(self, *args, **kwargs):

except Exception as e:
media_item = args[0]
await self._file_lock.release_lock(media_item.file_lock_reference_name)
await log(f"Download Failed: {media_item.url} with error {e}", 40)
if not isinstance(media_item.download_task_id, Field):
await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id)
Expand Down Expand Up @@ -130,6 +128,10 @@ async def run(self, media_item: MediaItem) -> None:
await log(f"Download Starting: {media_item.url}", 20)
async with self.manager.client_manager.download_session_limit:
try:
if isinstance(media_item.file_lock_reference_name, Field):
media_item.file_lock_reference_name = media_item.filename
await self._file_lock.check_lock(media_item.file_lock_reference_name)

await self.download(media_item)
except Exception as e:
await log(f"Download Failed: {media_item.url} with error {e}", 40)
Expand All @@ -138,6 +140,8 @@ async def run(self, media_item: MediaItem) -> None:
await self.manager.progress_manager.download_progress.add_failed()
else:
await log(f"Download Finished: {media_item.url}", 20)
finally:
await self._file_lock.release_lock(media_item.file_lock_reference_name)
self._semaphore.release()

"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
Expand Down Expand Up @@ -311,12 +315,8 @@ async def download(self, media_item: MediaItem) -> None:

partial_file = None
complete_file = None
if isinstance(media_item.file_lock_reference_name, Field):
media_item.file_lock_reference_name = media_item.filename

try:
await self._file_lock.check_lock(media_item.file_lock_reference_name)

if not isinstance(media_item.current_attempt, int):
media_item.current_attempt = 1

Expand All @@ -334,7 +334,6 @@ async def download(self, media_item: MediaItem) -> None:
await log(f"Skipping {media_item.url} as it has already been downloaded", 10)
await self.manager.progress_manager.download_progress.add_previously_completed(False)
await self.mark_completed(media_item)
await self._file_lock.release_lock(media_item.file_lock_reference_name)
return

resume_point = partial_file.stat().st_size if partial_file.exists() else 0
Expand All @@ -357,12 +356,9 @@ async def download(self, media_item: MediaItem) -> None:
await self.mark_completed(media_item)
await self.manager.progress_manager.file_progress.mark_task_completed(media_item.download_task_id)
await self.manager.progress_manager.download_progress.add_completed()
await self._file_lock.release_lock(media_item.file_lock_reference_name)
return

except (aiohttp.ServerDisconnectedError, asyncio.TimeoutError, aiohttp.ServerTimeoutError) as e:
await self._file_lock.release_lock(media_item.file_lock_reference_name)

if partial_file and partial_file.is_file():
size = partial_file.stat().st_size
if partial_file.name in self._current_attempt_filesize and self._current_attempt_filesize[media_item.filename] >= size:
Expand All @@ -375,8 +371,6 @@ async def download(self, media_item: MediaItem) -> None:

except (aiohttp.ClientPayloadError, aiohttp.ClientOSError, aiohttp.ClientResponseError, ConnectionResetError,
DownloadFailure, FileNotFoundError, PermissionError) as e:
await self._file_lock.release_lock(media_item.file_lock_reference_name)

if hasattr(e, "status"):
if ((await is_4xx_client_error(e.status) and e.status != HTTPStatus.TOO_MANY_REQUESTS)
or e.status == HTTPStatus.SERVICE_UNAVAILABLE
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def director(manager: Manager) -> None:

logger_debug = logging.getLogger("cyberdrop_dl_debug")
import cyberdrop_dl.utils.utilities
if manager.config_manager.settings_data['Runtime_Options']['log_level'] == -1:
if os.getenv("PYCHARM_HOSTED") is not None or manager.config_manager.settings_data['Runtime_Options']['log_level'] == -1:
manager.config_manager.settings_data['Runtime_Options']['log_level'] = 10
cyberdrop_dl.utils.utilities.DEBUG_VAR = True

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cyberdrop-dl"
version = "5.1.76"
version = "5.1.77"
description = "Bulk downloader for multiple file hosts"
authors = ["Jules Winnfield <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 5715a79

Please sign in to comment.