FileDownloader¶
Multi-threaded HTTP file downloader with chunked parallel downloading, retry logic, and Rich progress bars. Used internally by ISO clients to fetch raw data files from ISO websites.
Design¶
The downloader splits a file into Chunk objects based on HTTP Content-Range support. Each chunk is downloaded in a separate thread. If a chunk is truncated on arrival, it is retried up to max_retries times. Files that do not support range requests fall back to a single-stream download.
Configuration¶
| Parameter | Default | Description |
|---|---|---|
splits |
16 | Number of parallel chunks |
max_connections |
8 | Maximum concurrent threads |
min_split_size |
8 MB | Minimum bytes per chunk |
timeout |
30 s | Per-request timeout |
max_retries |
3 | Retry attempts per chunk |
Classes¶
FileDownloader
¶
FileDownloader(splits: int = DEFAULT_SPLITS, max_connections: int = DEFAULT_MAX_CONNECTIONS, min_split_size: int = DEFAULT_MIN_SPLIT_SIZE, timeout: int = DEFAULT_TIMEOUT, max_retries: int = DEFAULT_MAX_RETRIES, retry_delay: int = DEFAULT_RETRY_DELAY, overwrite: bool = False, verbose: bool = False)
A multi-threaded file downloader with resume capability.
Args: splits: Number of chunks to split the download into for parallel downloading max_connections: Maximum number of concurrent connections min_split_size: Minimum size in bytes for each chunk (default 8MB) timeout: Request timeout in seconds max_retries: Maximum number of retry attempts for failed chunks retry_delay: Initial delay in seconds between retries (increases with each attempt) overwrite: If True, existing files will be overwritten. If False, existing files will be skipped. verbose: If True, show logging statements. If False, suppress non-error logs.
Source code in src/progridpy/utils/downloader.py
Functions¶
download
¶
download(url: str, output_file: Path | str | None = None, description: str = 'Downloading', existing_task_id: TaskID | None = None, show_progress: bool = True, skip_head: bool = False) -> Path
Download a file from a URL.
Args: url: The URL to download from. output_file: Optional path where the file should be saved. If None, the file will be saved in the current directory with a filename determined from the server response or URL. description: Description to show in the progress bar. existing_task_id: If provided, update this existing progress task instead of creating a new one. show_progress: Whether to show progress bar for this download.
Returns: Path: The path where the file was saved.
Raises: DownloadError: If the download fails after all retry attempts.
Source code in src/progridpy/utils/downloader.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
download_batch
¶
download_batch(urls: list[str], output_files: list[Path | str] | None = None, description: str = 'Download', max_concurrent_files: int = 4, fetch_metadata: bool = True) -> tuple[list[Path], list[tuple[int, str]], tuple[int, int, int]]
Download multiple files concurrently.
Args: urls: List of URLs to download output_files: Optional list of output files. If None, files will be saved in the current directory with names determined from the server. description: Description for the batch progress bar max_concurrent_files: Maximum number of files to download concurrently fetch_metadata: If True (default), fetch file sizes upfront via HEAD requests for accurate byte-based progress tracking. If False, skip metadata fetch and use file count for progress tracking instead.
Returns: Tuple of (successful_downloads, failed_urls, counts) where: - successful_downloads is a list of Path objects for successfully downloaded files - failed_urls is a list of (index, url) tuples for failures - counts is a tuple of (downloaded_count, skipped_count, failed_count)
Source code in src/progridpy/utils/downloader.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |