Skip to content

Bulk asynchronous downloader for multiple file hosts

Simplify SQLHelper.insert_media

Arkadiy Illarionov1562b567513e
diff --git a/cyberdrop_dl/base_functions/sql_helper.py b/cyberdrop_dl/base_functions/sql_helper.py
index d672d1f61..dee0245a4 100644
--- a/cyberdrop_dl/base_functions/sql_helper.py
+++ b/cyberdrop_dl/base_functions/sql_helper.py
@@ -5,7 +5,12 @@ from pathlib import Path
from typing import List, Optional
from cyberdrop_dl.base_functions.base_functions import get_db_path
-from cyberdrop_dl.base_functions.data_classes import AlbumItem, CascadeItem, DomainItem
+from cyberdrop_dl.base_functions.data_classes import (
+ AlbumItem,
+ CascadeItem,
+ DomainItem,
+ MediaItem,
+)
class SQLHelper:
@@ -127,44 +132,43 @@ class SQLHelper:
"""Download Table Operations"""
- async def insert_media(self, domain: str, url_path: str, album_path: str, referer: str, download_path: str,
- download_filename: str, original_filename: str, completed: int) -> None:
+ async def _insert_media(self, domain: str, album_path: str, media: MediaItem) -> None:
+ """Inserts a media entry into the media table without commit"""
+ url_path = await get_db_path(media.url, domain)
+ self.curs.execute("""INSERT OR IGNORE INTO media VALUES (?, ?, ?, ?, '', '', ?, 0)""",
+ (domain, url_path, album_path, str(media.referer), media.original_filename))
+
+ async def _insert_album(self, domain: str, album_path: str, album: AlbumItem) -> None:
+ """Inserts an albums media into the media table without commit"""
+ for media in album.media:
+ await self._insert_media(domain, album_path, media)
+
+ async def insert_media(self, domain: str, album_path: str, media: MediaItem) -> None:
"""Inserts a media entry into the media table"""
- self.curs.execute("""INSERT OR IGNORE INTO media VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
- (domain, url_path, album_path, referer, download_path, download_filename, original_filename, completed))
+ await self._insert_media(domain, album_path, media)
self.conn.commit()
async def insert_album(self, domain: str, album_path: str, album: AlbumItem) -> None:
"""Inserts an albums media into the media table"""
if album.media:
- for media in album.media:
- url_path = await get_db_path(media.url, domain)
- self.curs.execute("""INSERT OR IGNORE INTO media VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
- (domain, url_path, album_path, str(media.referer), "", "", media.original_filename, 0))
- self.conn.commit()
+ await self._insert_album(domain, album_path, album)
+ self.conn.commit()
async def insert_domain(self, domain_name: str, album_path: str, domain: DomainItem) -> None:
"""Inserts a domains media into the media table"""
if domain.albums:
- for _, album in domain.albums.items():
- for media in album.media:
- url_path = await get_db_path(media.url, domain_name)
- self.curs.execute("""INSERT OR IGNORE INTO media VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
- (domain_name, url_path, album_path, str(media.referer), "", "",
- media.original_filename, 0))
- self.conn.commit()
+ for album in domain.albums.values():
+ await self._insert_album(domain_name, album_path, album)
+ self.conn.commit()
async def insert_cascade(self, cascade: CascadeItem) -> None:
"""Inserts a cascades media into the media table"""
if not await cascade.is_empty():
for domain, domain_obj in cascade.domains.items():
- for _, album_obj in domain_obj.albums.items():
+ for album_obj in domain_obj.albums.values():
for media in album_obj.media:
- url_path = await get_db_path(media.url, domain)
- self.curs.execute("""INSERT OR IGNORE INTO media VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
- (domain, url_path, media.referer.path, str(media.referer), "",
- "", media.original_filename, 0))
- self.conn.commit()
+ await self._insert_media(domain, media.referer.path, media)
+ self.conn.commit()
async def get_downloaded_filename(self, url_path: str, filename: str) -> Optional[str]:
"""Gets downloaded filename given the url path and original filename"""
diff --git a/cyberdrop_dl/crawlers/Anonfiles_Spider.py b/cyberdrop_dl/crawlers/Anonfiles_Spider.py
index 2234bd68c..c457afc07 100644
--- a/cyberdrop_dl/crawlers/Anonfiles_Spider.py
+++ b/cyberdrop_dl/crawlers/Anonfiles_Spider.py
@@ -36,7 +36,7 @@ class AnonfilesCrawler:
media_item = MediaItem(link, url, complete, filename, ext, filename)
await album_obj.add_media(media_item)
if not complete:
- await self.SQL_Helper.insert_media("anonfiles", url_path, "", str(url), "", "", filename, 0)
+ await self.SQL_Helper.insert_media("anonfiles", "", media_item)
else:
await log(f"[red]Dead: {str(url)}[/red]", quiet=self.quiet)
diff --git a/cyberdrop_dl/crawlers/Bunkr_Spider.py b/cyberdrop_dl/crawlers/Bunkr_Spider.py
index 51c082661..f33d889af 100644
--- a/cyberdrop_dl/crawlers/Bunkr_Spider.py
+++ b/cyberdrop_dl/crawlers/Bunkr_Spider.py
@@ -28,8 +28,7 @@ class BunkrCrawler:
await album_obj.add_media(media)
await log(f"[green]Finished: {str(url)}[/green]", quiet=self.quiet)
if not media.complete:
- url_path = await get_db_path(media.url)
- await self.SQL_Helper.insert_media("bunkr", url_path, "", str(url), "", "", media.original_filename, 0)
+ await self.SQL_Helper.insert_media("bunkr", "", media)
return album_obj
if "a" in url.parts: