diff --git a/app/download.py b/app/download.py index 4fcb2cb..aaead5f 100644 --- a/app/download.py +++ b/app/download.py @@ -4,12 +4,25 @@ from urllib.parse import urlsplit 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): status = d.get('status') - + 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': logger.info(f"Download completed: {d.get('filename')}") elif status == 'error': @@ -19,6 +32,7 @@ def my_hook(d): elif status == 'processing': logger.info(f"Processing: {d.get('filename')}") + def ebook(url, author): destination = f"/ebooks/{author}" os.makedirs(destination, exist_ok=True) # Create the folder if it doesn't exist