Skip to content

Bulk asynchronous downloader for multiple file hosts

add filesize to media table

datawhores5a16ddb0b3b2
diff --git a/cyberdrop_dl/clients/download_client.py b/cyberdrop_dl/clients/download_client.py
index 4a5d81d79..f9a6885e3 100644
--- a/cyberdrop_dl/clients/download_client.py
+++ b/cyberdrop_dl/clients/download_client.py
@@ -115,7 +115,7 @@ class DownloadClient:
if not proceed:
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, domain)
+ await self.process_completed(media_item, domain)
await self.handle_media_item_completion(media_item,downloaded=False)
@@ -156,11 +156,11 @@ class DownloadClient:
async def download_file(self, manager: Manager, domain: str, media_item: MediaItem) -> bool:
"""Starts a file"""
- if self.manager.config_manager.settings_data['Download_Options']['skip_download_mark_completed']:
+ if self.manager.config_manager.settings_data['Download_Options']['skip_download_process_completed']:
await log(f"Download Skip {media_item.url} due to mark completed option", 10)
await self.manager.progress_manager.download_progress.add_skipped()
await self.mark_incomplete(media_item, domain)
- await self.mark_completed(media_item, domain)
+ await self.process_completed(media_item, domain)
return False
async def save_content(content: aiohttp.StreamReader) -> None:
@@ -169,7 +169,7 @@ class DownloadClient:
downloaded = await self._download(domain, manager, media_item, save_content)
if downloaded:
media_item.partial_file.rename(media_item.complete_file)
- await self.mark_completed(media_item, domain)
+ await self.process_completed(media_item, domain)
await self.handle_media_item_completion(media_item,downloaded=True)
return downloaded
@@ -179,12 +179,12 @@ class DownloadClient:
"""Marks the media item as incomplete in the database"""
await self.manager.db_manager.history_table.insert_incompleted(domain, media_item)
- async def mark_completed(self, media_item: MediaItem, domain: str) -> None:
- await self._add_db(media_item,domain)
-
- async def _add_db(self,media_item: MediaItem, domain: str):
+ async def process_completed(self, media_item: MediaItem, domain: str) -> None:
"""Marks the media item as completed in the database and adds to the completed list"""
await self.manager.db_manager.history_table.mark_complete(domain, media_item)
+ await self.manager.db_manager.history_table.add_file_size(media_item)
+
+
async def handle_media_item_completion(self, media_item,downloaded=False) -> None:
"""Sends to hash client to handle hashing and marks as completed/current download"""
diff --git a/cyberdrop_dl/utils/database/table_definitions.py b/cyberdrop_dl/utils/database/table_definitions.py
index 8d7612738..082a6f378 100644
--- a/cyberdrop_dl/utils/database/table_definitions.py
+++ b/cyberdrop_dl/utils/database/table_definitions.py
@@ -18,6 +18,7 @@ create_fixed_history = """CREATE TABLE IF NOT EXISTS media_copy (domain TEXT,
download_path TEXT,
download_filename TEXT,
original_filename TEXT,
+ file_size INT,
completed INTEGER NOT NULL,
PRIMARY KEY (domain, url_path, original_filename)
);"""
@@ -29,7 +30,7 @@ CREATE TABLE IF NOT EXISTS hash (
folder TEXT,
download_filename TEXT,
original_filename TEXT,
- size INT,
+ file_size INT,
hash TEXT,
UNIQUE (folder, filename)
PRIMARY KEY (folder, filename,hash)
diff --git a/cyberdrop_dl/utils/database/tables/history_table.py b/cyberdrop_dl/utils/database/tables/history_table.py
index 643893f5c..110611abe 100644
--- a/cyberdrop_dl/utils/database/tables/history_table.py
+++ b/cyberdrop_dl/utils/database/tables/history_table.py
@@ -1,4 +1,5 @@
from __future__ import annotations
+import pathlib
from sqlite3 import Row, IntegrityError
@@ -120,6 +121,14 @@ class HistoryTable:
(domain, url_path))
await self.db_conn.commit()
+ async def add_filesize(self, domain: str, media_item: MediaItem) -> None:
+ """add the file size to the db"""
+ domain = await get_db_domain(domain)
+ url_path = await get_db_path(media_item.url, str(media_item.referer))
+ file_size=pathlib.Path(media_item.complete_file).stat().st_size
+ await self.db_conn.execute("""UPDATE media SET file_size=? CURRENT_TIMESTAMP WHERE domain = ? and url_path = ?""",
+ (file_size,domain, url_path))
+ await self.db_conn.commit()
async def check_filename_exists(self, filename: str) -> bool:
"""Checks whether a downloaded filename exists in the database"""
cursor = await self.db_conn.cursor()
@@ -160,7 +169,7 @@ class HistoryTable:
"""Returns a list of all items"""
cursor = await self.db_conn.cursor()
result = await cursor.execute("""
-SELECT m.referer,m.download_path,m.completed_at
+SELECT m.referer,m.download_path,
FROM hash h
INNER JOIN media m ON h.download_filename= m.download_filename
WHERE h.hash = 'eb669b6362e031fa2b0f1215480c4e30';
@@ -219,7 +228,9 @@ WHERE h.hash = 'eb669b6362e031fa2b0f1215480c4e30';
if "completed_at" not in current_cols:
await self.db_conn.execute("""ALTER TABLE media ADD COLUMN completed_at TIMESTAMP""")
await self.db_conn.commit()
-
+ if "file_size" not in current_cols:
+ await self.db_conn.execute("""ALTER TABLE media ADD COLUMN file_size INT""")
+ await self.db_conn.commit()
async def add_columns_hash(self) -> None:
cursor = await self.db_conn.cursor()
@@ -231,6 +242,10 @@ WHERE h.hash = 'eb669b6362e031fa2b0f1215480c4e30';
await self.db_conn.execute("""ALTER TABLE hash RENAME COLUMN filename TO download_filename;""")
await self.db_conn.commit()
+ if "file_size" not in current_cols:
+ await self.db_conn.execute("""ALTER TABLE hash RENAME COLUMN size TO file_size;""")
+ await self.db_conn.commit()
+
if "original_filename" not in current_cols:
await self.db_conn.execute("""ALTER TABLE hash ADD COLUMN original_filename TEXT""")
await self.db_conn.commit()