Skip to content

Implement parallel download - #7

Open
LainX84 wants to merge 1 commit into
Lysagxra:mainfrom
LainX84:parallel-download
Open

Implement parallel download#7
LainX84 wants to merge 1 commit into
Lysagxra:mainfrom
LainX84:parallel-download

Conversation

@LainX84

@LainX84 LainX84 commented Jul 10, 2026

Copy link
Copy Markdown

Description of Changes

This Pull Request introduces concurrent image downloading, resolves a critical startup IndexError crash, and significantly increases downloader robustness by improving the HTTP retry and rate-limiting logic.

Key Changes

1. Bug Fix & URL Normalization

  • IndexError Resolution: Fixed IndexError: list index out of range in Crawler._generate_album_pages that occurred when downloading single-page albums. The crawler now parses pagination parameters safely, finds the maximum page using max(), and dynamically builds the page list rather than relying on a fragile hardcoded index ([-2]).
  • URL Normalization: Added URL normalization inside Crawler and AlbumDownloader to guarantee a trailing slash / and strip existing query parameters (such as ?p=...). This prevents regex pattern matching mismatches against gallery HTML links.

2. Parallelized Image Downloads

  • Multithreading: Integrated ThreadPoolExecutor in AlbumDownloader to download the pictures of a gallery page concurrently.
  • Concurrency Configuration: Added a MAX_WORKERS setting (defaulting to 5) in config.py to customize download speed while keeping requests polite.
  • Thread Safety:
    • Added synchronization locks (threading.Lock) inside ProgressManager and LiveManager to prevent terminal UI glitches or exceptions during concurrent progress updates.
    • Decoupled requests.Session header mutation by passing request-specific headers directly to each request, ensuring thread-safe operations.

3. Improved HTTP Retry & Rate-Limiting Logic

  • Refactored fetch_with_retries to specifically handle rate-limit status codes 429 (Too Many Requests) and 509 (Bandwidth Exceeded). The downloader now logs a rate-limit warning, sleeps for RATE_LIMIT_SLEEPING_TIME (60 seconds), and retries the download.
  • Introduced immediate exit for non-recoverable HTTP errors (like 404 Not Found) to prevent redundant retry attempts.

Files Changed

  • src/config.py: Added the MAX_WORKERS configuration.
  • src/crawler/crawler.py: Fixed the IndexError, normalized URLs, and added the thread-safe get_reloaded_page() helper.
  • src/downloader/album_downloader.py: Redesigned downloading logic to run concurrently.
  • src/downloader/download_utils.py: Added thread-safe headers support and improved retry logic (handling of 429/509/404).
  • src/managers/progress_manager.py & src/managers/live_manager.py: Integrated thread-safety locks for UI rendering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant