Update download.py
This commit is contained in:
parent
6e005f053e
commit
9780c52f9b
@ -4,12 +4,25 @@ from urllib.parse import urlsplit
|
|||||||
|
|
||||||
logger = logging.getLogger("syllabus")
|
logger = logging.getLogger("syllabus")
|
||||||
|
|
||||||
# Define the hook function
|
# Global or outer-scope tracking dictionary
|
||||||
|
last_logged_percent = {"value": -10} # To ensure first log happens at 0%
|
||||||
|
|
||||||
def my_hook(d):
|
def my_hook(d):
|
||||||
status = d.get('status')
|
status = d.get('status')
|
||||||
|
|
||||||
if status == 'downloading':
|
if status == 'downloading':
|
||||||
logger.info(f"Downloading: {d.get('_percent_str')} at {d.get('_speed_str')} for {d.get('filename')}")
|
total_bytes = d.get('total_bytes') or d.get('total_bytes_estimate')
|
||||||
|
downloaded = d.get('downloaded_bytes', 0)
|
||||||
|
|
||||||
|
if total_bytes:
|
||||||
|
percent = int(downloaded / total_bytes * 100)
|
||||||
|
|
||||||
|
if percent >= last_logged_percent["value"] + 10:
|
||||||
|
last_logged_percent["value"] = (percent // 10) * 10
|
||||||
|
logger.info(
|
||||||
|
f"Downloading: {d.get('_percent_str')} at {d.get('_speed_str')} for {d.get('filename')}"
|
||||||
|
)
|
||||||
|
|
||||||
elif status == 'finished':
|
elif status == 'finished':
|
||||||
logger.info(f"Download completed: {d.get('filename')}")
|
logger.info(f"Download completed: {d.get('filename')}")
|
||||||
elif status == 'error':
|
elif status == 'error':
|
||||||
@ -19,6 +32,7 @@ def my_hook(d):
|
|||||||
elif status == 'processing':
|
elif status == 'processing':
|
||||||
logger.info(f"Processing: {d.get('filename')}")
|
logger.info(f"Processing: {d.get('filename')}")
|
||||||
|
|
||||||
|
|
||||||
def ebook(url, author):
|
def ebook(url, author):
|
||||||
destination = f"/ebooks/{author}"
|
destination = f"/ebooks/{author}"
|
||||||
os.makedirs(destination, exist_ok=True) # Create the folder if it doesn't exist
|
os.makedirs(destination, exist_ok=True) # Create the folder if it doesn't exist
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user