Update download.py
This commit is contained in:
parent
1915e89f57
commit
6d7b115268
@ -5,32 +5,38 @@ from urllib.parse import urlsplit
|
|||||||
logger = logging.getLogger("syllabus")
|
logger = logging.getLogger("syllabus")
|
||||||
|
|
||||||
# Global or outer-scope tracking dictionary
|
# Global or outer-scope tracking dictionary
|
||||||
last_logged_percent = {"value": -10} # To ensure first log happens at 0%
|
last_logged_percent = {}
|
||||||
|
|
||||||
def my_hook(d):
|
def my_hook(d):
|
||||||
status = d.get('status')
|
status = d.get('status')
|
||||||
|
filename = d.get('filename')
|
||||||
|
|
||||||
if status == 'downloading':
|
if status == 'downloading':
|
||||||
total_bytes = d.get('total_bytes') or d.get('total_bytes_estimate')
|
total_bytes = d.get('total_bytes') or d.get('total_bytes_estimate')
|
||||||
downloaded = d.get('downloaded_bytes', 0)
|
downloaded = d.get('downloaded_bytes', 0)
|
||||||
|
|
||||||
if total_bytes:
|
if total_bytes and filename:
|
||||||
percent = int(downloaded / total_bytes * 100)
|
percent = int(downloaded / total_bytes * 100)
|
||||||
|
current_value = last_logged_percent.get(filename, -10)
|
||||||
|
|
||||||
if percent >= last_logged_percent["value"] + 10:
|
if percent >= current_value + 10:
|
||||||
last_logged_percent["value"] = (percent // 10) * 10
|
last_logged_percent[filename] = (percent // 10) * 10
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Downloading: {d.get('_percent_str')} at {d.get('_speed_str')} for {d.get('filename')}"
|
f"Downloading: {d.get('_percent_str')} at {d.get('_speed_str')} for {filename}"
|
||||||
)
|
)
|
||||||
|
|
||||||
elif status == 'finished':
|
elif status == 'finished':
|
||||||
logger.info(f"Download completed: {d.get('filename')}")
|
logger.info(f"Download completed: {filename}")
|
||||||
|
# Optionally reset or clean up
|
||||||
|
last_logged_percent.pop(filename, None)
|
||||||
|
|
||||||
elif status == 'error':
|
elif status == 'error':
|
||||||
logger.error(f"Error occurred: {d.get('error')}")
|
logger.error(f"Error occurred: {d.get('error')}")
|
||||||
elif status == 'postprocessing':
|
elif status == 'postprocessing':
|
||||||
logger.info(f"Post-processing: {d.get('filename')}")
|
logger.info(f"Post-processing: {filename}")
|
||||||
elif status == 'processing':
|
elif status == 'processing':
|
||||||
logger.info(f"Processing: {d.get('filename')}")
|
logger.info(f"Processing: {filename}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def ebook(url, author):
|
def ebook(url, author):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user