Skip to content

Bulk asynchronous downloader for multiple file hosts

modify hash table to include refer and original filename

datawhores6219fa38a345
diff --git a/cyberdrop_dl/clients/hash_client.py b/cyberdrop_dl/clients/hash_client.py
index 489145869..4afdd8c03 100644
--- a/cyberdrop_dl/clients/hash_client.py
+++ b/cyberdrop_dl/clients/hash_client.py
@@ -34,10 +34,10 @@ class HashClient:
if not pathlib.Path(path).is_dir():
raise Exception("Path is not a directory")
for file in pathlib.Path(path).glob("**/*"):
- await self.hash_item(file)
+ await self.hash_item(file,None,None)
def _get_key_from_file(self,file):
return str(pathlib.Path(file).absolute())
- async def hash_item(self,file):
+ async def hash_item(self,file,original_filename,refer):
key=self._get_key_from_file(file)
if not file.is_file():
return
@@ -48,7 +48,7 @@ class HashClient:
if not hash:
try:
hash = await self.manager.hash_manager.hash_file(file)
- await self.manager.db_manager.hash_table.insert_or_update_hash_db(hash, file.stat().st_size, file)
+ await self.manager.db_manager.hash_table.insert_or_update_hash_db(hash,file,original_filename,refer)
await self.manager.progress_manager.hash_progress.add_new_completed_hash()
except Exception as e:
await log(f"Error hashing {file} : {e}",40)
@@ -62,7 +62,8 @@ class HashClient:
async def hash_item_during_download(self,media_item:MediaItem):
try:
if self.manager.config_manager.global_settings_data['Dupe_Cleanup_Options']['hash_while_downloading']:
- await self.hash_item(media_item.complete_file)
+ await self.hash_item(media_item.complete_file,media_item.original_filename,media_item.referer
+ )
except Exception as e:
await log(f"After hash processing failed: {media_item.complete_file} with error {e}", 40)
@@ -73,8 +74,10 @@ class HashClient:
return
hashes_dict=defaultdict(lambda: defaultdict(list))
# first compare downloads to each other
- for item in list(self.manager.path_manager.completed_downloads):
- hash=await self.hash_item(item)
+ for media_item in list(self.manager.path_manager.completed_downloads):
+
+ hash=await self.hash_item(media_item.complete_file,media_item.original_filename,media_item.referer)
+ item=media_item.complete_file.absolute()
try:
size=item.stat().st_size
if hash:
diff --git a/cyberdrop_dl/managers/path_manager.py b/cyberdrop_dl/managers/path_manager.py
index f6bd6a1ff..f0c0b49f2 100644
--- a/cyberdrop_dl/managers/path_manager.py
+++ b/cyberdrop_dl/managers/path_manager.py
@@ -74,8 +74,8 @@ class PathManager:
self.input_file.touch(exist_ok=True)
self.history_db.touch(exist_ok=True)
- def add_completed(self,media:MediaItem):
- self._completed_downloads.add(media.complete_file.absolute())
+ def add_completed(self,media_item:MediaItem):
+ self._completed_downloads.add(media_item)
@property
def completed_downloads(self):
return self._completed_downloads
\ No newline at end of file
diff --git a/cyberdrop_dl/utils/database/table_definitions.py b/cyberdrop_dl/utils/database/table_definitions.py
index cfbbdd80d..8d7612738 100644
--- a/cyberdrop_dl/utils/database/table_definitions.py
+++ b/cyberdrop_dl/utils/database/table_definitions.py
@@ -27,9 +27,11 @@ create_temp = """CREATE TABLE IF NOT EXISTS temp (downloaded_filename TEXT);"""
create_hash = """
CREATE TABLE IF NOT EXISTS hash (
folder TEXT,
- filename TEXT,
+ download_filename TEXT,
+ original_filename TEXT,
size INT,
hash TEXT,
UNIQUE (folder, filename)
+ PRIMARY KEY (folder, filename,hash)
);
"""
diff --git a/cyberdrop_dl/utils/database/tables/hash_table.py b/cyberdrop_dl/utils/database/tables/hash_table.py
index b03db13e9..af891cd8a 100644
--- a/cyberdrop_dl/utils/database/tables/hash_table.py
+++ b/cyberdrop_dl/utils/database/tables/hash_table.py
@@ -40,7 +40,7 @@ class HashTable:
cursor = await self.db_conn.cursor()
# Check if the file exists with matching folder, filename, and size
- await cursor.execute("SELECT hash FROM hash WHERE folder=? AND filename=? AND size=?", (folder, filename, size))
+ await cursor.execute("SELECT hash FROM hash WHERE folder=? AND download_filename=? AND size=?", (folder, filename, size))
result = await cursor.fetchone()
if result and result[0]:
return result[0]
@@ -63,7 +63,7 @@ class HashTable:
cursor = await self.db_conn.cursor()
try:
- await cursor.execute("SELECT folder, filename FROM hash WHERE hash = ? and size=?", (hash_value,size))
+ await cursor.execute("SELECT folder, download_filename FROM hash WHERE hash = ? and size=?", (hash_value,size))
results = await cursor.fetchall()
return results
except Exception as e:
@@ -71,7 +71,7 @@ class HashTable:
return []
- async def insert_or_update_hash_db(self, hash_value, file_size, file):
+ async def insert_or_update_hash_db(self, hash_value, file,original_filename,refer):
"""
Inserts or updates a record in the specified SQLite database.
@@ -87,20 +87,26 @@ class HashTable:
cursor = await self.db_conn.cursor()
full_path=pathlib.Path(file).absolute()
+ file_size=full_path.stat().st_size
- filename=full_path.name
+ download_filename=full_path.name
folder=str(full_path.parent)
# Assuming a table named 'file_info' with columns: id (primary key), hash, size, filename, folder
try:
- await cursor.execute("INSERT INTO hash (hash, size, filename, folder) VALUES (?, ?, ?, ?)",
- (hash_value, file_size, filename, folder))
+ await cursor.execute("INSERT INTO hash (hash, size, download_filename, folder,original_filename,refer) VALUES (?, ?, ?, ?,?,?)",
+ (hash_value, file_size, download_filename, folder,original_filename,refer))
await self.db_conn.commit()
return True
except IntegrityError:
# Handle potential duplicate key (assuming a unique constraint on hash, filename, and folder)
- await cursor.execute("UPDATE hash SET size=?,hash=? WHERE filename=? AND folder=?",
- (file_size,hash_value, filename, folder))
+ await cursor.execute("""UPDATE hash
+ SET size = ?,
+ hash = ?,
+ referer = CASE WHEN ? IS NOT NULL THEN ? ELSE referer END,
+ original_filename = CASE WHEN ? IS NOT NULL THEN ? ELSE original_filename END
+WHERE download_filename = ? AND folder = ?;""",
+ (file_size,hash_value,refer,original_filename, download_filename, folder))
await self.db_conn.commit()
return True
except Exception as e:
diff --git a/cyberdrop_dl/utils/database/tables/history_table.py b/cyberdrop_dl/utils/database/tables/history_table.py
index f3e679c74..357673a85 100644
--- a/cyberdrop_dl/utils/database/tables/history_table.py
+++ b/cyberdrop_dl/utils/database/tables/history_table.py
@@ -43,7 +43,8 @@ class HistoryTable:
await self.db_conn.execute(create_history)
await self.db_conn.commit()
await self.fix_primary_keys()
- await self.add_columns()
+ await self.add_columns_media()
+ await self.add_columns_hash()
await self.fix_bunkr_v4_entries()
async def check_complete(self, domain: str, url: URL, referer: URL) -> bool:
@@ -189,7 +190,7 @@ class HistoryTable:
await self.db_conn.execute("""ALTER TABLE media_copy RENAME TO media""")
await self.db_conn.commit()
- async def add_columns(self) -> None:
+ async def add_columns_media(self) -> None:
cursor = await self.db_conn.cursor()
result = await cursor.execute("""pragma table_info(media)""")
result = await result.fetchall()
@@ -206,3 +207,22 @@ class HistoryTable:
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()
+
+
+ async def add_columns_hash(self) -> None:
+ cursor = await self.db_conn.cursor()
+ result = await cursor.execute("""pragma table_info(hash)""")
+ result = await result.fetchall()
+ current_cols = [col[1] for col in result]
+
+ if "download_filename" not in current_cols:
+ await self.db_conn.execute("""ALTER TABLE hash RENAME COLUMN filename TO download_filename;""")
+ 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()
+
+ if "referer" not in current_cols:
+ await self.db_conn.execute("""ALTER TABLE hash ADD COLUMN referer TEXT""")
+ await self.db_conn.commit()
\ No newline at end of file