Skip to content

Bulk asynchronous downloader for multiple file hosts

Change from URL based tracking to size based/filename based tracking.

Jules-WinnfieldXc39ef5c310af
diff --git a/cyberdrop_dl/__init__.py b/cyberdrop_dl/__init__.py
index 200cbc3be..09c419b16 100644
--- a/cyberdrop_dl/__init__.py
+++ b/cyberdrop_dl/__init__.py
@@ -1 +1 @@
-__version__ = "2.13.2"
+__version__ = "2.13.3"
diff --git a/cyberdrop_dl/utils/base_functions.py b/cyberdrop_dl/utils/base_functions.py
index 0851e7e07..5d8048f11 100644
--- a/cyberdrop_dl/utils/base_functions.py
+++ b/cyberdrop_dl/utils/base_functions.py
@@ -54,14 +54,15 @@ def sanitize(input: str) -> str:
def sql_initialize():
- download_history = "download_history.sqlite"
+ download_history = "download_history_size_based.sqlite"
if not os.path.isfile(download_history):
conn = sqlite3.connect(download_history)
curr = conn.cursor()
create_table_query = """CREATE TABLE downloads (
- direct_url TEXT PRIMARY KEY,
- filename TEXT NOT NULL,
- completed INTEGER NOT NULL
+ filename TEXT,
+ size INTEGER NOT NULL,
+ completed INTEGER NOT NULL,
+ PRIMARY KEY (filename, size)
);"""
curr.execute(create_table_query)
conn.commit()
@@ -71,8 +72,8 @@ def sql_initialize():
return conn, curr
-async def sql_check_existing(cursor: sqlite3.Cursor, url):
- cursor.execute("""SELECT completed FROM downloads WHERE direct_url = '%s'""" % str(url))
+async def sql_check_existing(cursor: sqlite3.Cursor, url, size):
+ cursor.execute("""SELECT completed FROM downloads WHERE filename = '%s' and size = %d""" % (str(url), size))
sql_file_check = cursor.fetchone()
if sql_file_check:
if sql_file_check[0] == 1:
@@ -80,14 +81,14 @@ async def sql_check_existing(cursor: sqlite3.Cursor, url):
return False
-async def sql_insert_file(connection: sqlite3.Connection, cursor: sqlite3.Cursor, url, filename, completed):
- cursor.execute("""INSERT OR IGNORE INTO downloads VALUES ('%s', '%s', %d)""" % (url, filename, completed))
+async def sql_insert_file(connection: sqlite3.Connection, cursor: sqlite3.Cursor, filename, size, completed):
+ cursor.execute("""INSERT OR IGNORE INTO downloads VALUES ('%s', %d, %d)""" % (filename, size, completed))
connection.commit()
-async def sql_update_file(connection: sqlite3.Connection, cursor: sqlite3.Cursor, url, filename, completed):
+async def sql_update_file(connection: sqlite3.Connection, cursor: sqlite3.Cursor, filename, size, completed):
cursor.execute("""UPDATE downloads SET completed = %d WHERE
- direct_url = '%s' AND filename = '%s'""" % (completed, url, filename))
+ filename = '%s' AND size = %d""" % (completed, filename, size))
connection.commit()
diff --git a/cyberdrop_dl/utils/downloaders.py b/cyberdrop_dl/utils/downloaders.py
index 6aee0a432..51feb916c 100644
--- a/cyberdrop_dl/utils/downloaders.py
+++ b/cyberdrop_dl/utils/downloaders.py
@@ -135,12 +135,7 @@ class Downloader:
logging.debug("Skipping " + filename)
return
- if await sql_check_existing(self.cursor, str(url)):
- log("\n%s Already Downloaded" % filename)
- logger.debug("%s was found in db file (Previously downloaded)" % filename)
- return
- await sql_insert_file(self.connection, self.cursor, url, filename, 0)
resume_point = 0
complete_file = (self.folder / self.title / filename)
@@ -156,6 +151,14 @@ class Downloader:
log(f"\nServer for {url} is either down or the file no longer exists", Fore.RED)
return
total = int(resp.headers.get('Content-Length', str(0))) + resume_point
+
+ if await sql_check_existing(self.cursor, filename, total):
+ log("\n%s Already Downloaded" % filename)
+ logger.debug("%s was found in db file (Previously downloaded)" % filename)
+ return
+
+ await sql_insert_file(self.connection, self.cursor, filename, total, 0)
+
with tqdm(
total=total, unit_scale=True,
unit='B', leave=False, initial=resume_point,
@@ -165,7 +168,6 @@ class Downloader:
async for chunk, _ in resp.content.iter_chunks():
await f.write(chunk)
progress.update(len(chunk))
- await sql_update_file(self.connection, self.cursor, url, filename, 1)
resp.close()
await self.rename_file(filename)
except (aiohttp.client_exceptions.ClientPayloadError, aiohttp.client_exceptions.ClientOSError,
@@ -186,6 +188,7 @@ class Downloader:
await aiofiles.os.remove(temp_file)
else:
temp_file.rename(complete_file)
+ await sql_update_file(self.connection, self.cursor, filename, complete_file.stat().st_size, 1)
logger.debug("Finished " + filename)
async def download_and_store(
@@ -205,10 +208,10 @@ class Downloader:
fileext = filename.split('.')[-1]
filename = filename[:MAX_FILENAME_LENGTH] + '.' + fileext
- if (self.folder / self.title / filename).exists():
- await sql_update_file(self.connection, self.cursor, url, filename, 1)
- logger.debug(str(self.folder / self.title / filename) + " Already Exists")
- await sql_update_file(self.connection, self.cursor, url, filename, 1)
+ complete_file = (self.folder / self.title / filename)
+ if complete_file.exists():
+ await sql_update_file(self.connection, self.cursor, filename, complete_file.stat().st_size, 1)
+ logger.debug(str(complete_file) + " Already Exists")
else:
logger.debug("Working on " + str(url))
try: