Skip to content

Bulk asynchronous downloader for multiple file hosts

write some functions for the history_table

Jules-WinnfieldX677a5d842b6f
diff --git a/cyberdrop_dl/utils/db/tables/history_table.py b/cyberdrop_dl/utils/db/tables/history_table.py
index 99ee5499d..3cbf4f9a5 100644
--- a/cyberdrop_dl/utils/db/tables/history_table.py
+++ b/cyberdrop_dl/utils/db/tables/history_table.py
@@ -1,8 +1,12 @@
import aiosqlite
+from typing import TYPE_CHECKING
from yarl import URL
from cyberdrop_dl.utils.db.table_definitions import create_history
+if TYPE_CHECKING:
+ from cyberdrop_dl.utils.dataclasses.url_objects import MediaItem
+
async def get_db_path(url: URL, referer: str = "") -> str:
"""Gets the URL path to be put into the DB and checked from the DB"""
@@ -40,3 +44,31 @@ class HistoryTable:
result = await cursor.execute("""SELECT completed FROM media WHERE domain = ? and url_path = ?""", (domain, url_path))
sql_file_check = await result.fetchone()
return sql_file_check and sql_file_check[0] != 0
+
+ async def insert_uncompleted(self, domain: str, media_item: MediaItem) -> None:
+ """Inserts an uncompleted file into the database"""
+ url_path = await get_db_path(media_item.url, str(media_item.referer))
+ await self.db_conn.execute("""INSERT OR IGNORE INTO media (domain, url_path, album_path, referer, original_filename, completed) VALUES (?, ?, ?, ?, ?, ?)""", (domain, url_path, media_item.referer, media_item.original_filename, 0))
+ await self.db_conn.commit()
+
+ async def mark_complete(self, domain: str, media_item: MediaItem) -> None:
+ """Mark a download as completed in the database"""
+ url_path = await get_db_path(media_item.url, str(media_item.referer))
+ download_filename = media_item.download_filename if isinstance(media_item.download_filename, str) else ""
+ await self.db_conn.execute("""UPDATE media SET completed = 1, downloaded_filename = ? WHERE domain = ? and url_path = ?""", (download_filename, domain, url_path))
+ await self.db_conn.commit()
+
+ async def check_filename_exists(self, filename: str):
+ """Checks whether a downloaded filename exists in the database"""
+ cursor = await self.db_conn.cursor()
+ result = await cursor.execute("""SELECT EXISTS(SELECT 1 FROM media WHERE download_filename = ?)""", (filename,))
+ sql_file_check = await result.fetchone()
+ return sql_file_check == 1
+
+ async def get_downloaded_filename(self, domain: str, media_item: MediaItem):
+ """Returns the downloaded filename from the database"""
+ url_path = await get_db_path(media_item.url, str(media_item.referer))
+ cursor = await self.db_conn.cursor()
+ result = await cursor.execute("""SELECT downloaded_filename FROM media WHERE domain = ? and url_path = ?""", (domain, url_path))
+ sql_file_check = await result.fetchone()
+ return sql_file_check[0] if sql_file_check else None