Skip to main content

A concurrent python download manager

Reason this release was yanked:

require python 3.8+, no min python version specified

Project description

pypdl

pypdl is a Python library for downloading files from the internet. It provides features such as multi-threaded downloads, retry download in case of failure, option to continue downloading using a different URL if necessary, progress tracking, pause/resume functionality, and many more.

Table of Contents

Prerequisites

  • Python 3.8 or later.

Installation

To install the pypdl, run the following command:

pip install pypdl

Usage

Basic Usage

To download a file using the pypdl, simply create a new Downloader object and call its start method, passing in the URL of the file to be downloaded:

from pypdl import Downloader

dl = Downloader()
dl.start('http://example.com/file.txt')

Advanced Usage

The Downloader object provides additional options for advanced usage:

dl.start(
    url='http://example.com/file.txt',
    file_path='file.txt',
    segments=10,
    display=True,
    multithread=True,
    block=True,
    retries=0,
    mirror_func=None,
    etag=True
)

Each option is explained below:

  • url: The URL of the file to download.
  • file_path: An optional path to save the downloaded file. By default, it uses the present working directory. If file_path is a directory, then the file is downloaded into it otherwise, the file is downloaded into the given path.
  • segments: The number of segments the file should be divided in multi-threaded download. The default value is 10.
  • display: Whether to display download progress and other optional messages. The default value is True.
  • multithread: Whether to use multi-threaded download. The default value is True.
  • block: Whether to block until the download is complete. The default value is True.
  • retries: The number of times to retry the download in case of an error. The default value is 0.
  • mirror_func: A function to get a new download URL in case of an error.
  • etag: Whether to validate etag before resuming downloads. The default value is True.

Examples

Here is an example that demonstrates how to use pypdl library to download a file using headers, proxies and authentication:

from pypdl import Downloader

def main():
    # Using headers
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"}
    # Using proxies
    proxies = {
                    "http": "http://10.10.1.10:3128",
                    "https": "https://10.10.1.10:1080",
                }
    # Using authentication
    auth = ("user","pass")

    # create a new downloader object
    dl = Downloader(headers=headers, proxies=proxies, auth=auth)

    # start the download
    dl.start(
        url='https://speed.hetzner.de/100MB.bin',
        file_path='100MB.bin',
        segments=10,
        display=True,
        multithread=True,
        block=True,
        retries=3,
        mirror_func=None,
        etag=True,
    )

if __name__ == '__main__':
    main()

This example downloads a file from the internet using 10 threads and displays the download progress. If the download fails, it will retry up to 3 times. we are also using headers, proxies and authentication.

Another example of implementing pause resume functionality and printing the progress to console:

from pypdl import Downloader
from threading import Event

# create a downloader object
dl = Downloader()

# start the download process
# block=False so we can print the progress
# display=False so we can print the progress ourselves
dl.start('https://example.com/file.zip', segments=8,block=False,display=False)

# print the progress
while dl.progress != 70:
  print(dl.progress)

# stop the download process
d.stop() 

#do something
#...

# resume the download process
dl.start('https://example.com/file.zip', segments=8,block=False,display=False)

# print rest of the progress
while not d.completed:
  print(dl.progress)

This example we start the download process and print the progress to console. We then stop the download process and do something else. After that we resume the download process and print the rest of the progress to console. This can be used to create a pause/resume functionality.

API Reference

Downloader()

The Downloader class represents a file downloader that can download a file from a given URL to a specified file path. The class supports both single-threaded and multi-threaded downloads and many other features like retry download incase of failure and option to continue downloading using a different url if necessary, pause/resume functionality, progress tracking etc.

Keyword Arguments

  • params: (dict, Optional) A dictionary, list of tuples or bytes to send as a query string. Default is None.
  • allow_redirects: (bool, Optional) A Boolean to enable/disable redirection. Default is True.
  • auth: (tuple, Optional) A tuple to enable a certain HTTP authentication. Default is None.
  • cert: (str or tuple, Optional) A String or Tuple specifying a cert file or key. Default is None.
  • cookies: (dict, Optional) A dictionary of cookies to send to the specified url. Default is None.
  • headers: (dict, Optional) A dictionary of HTTP headers to send to the specified url. Default is None.
  • proxies: (dict, Optional) A dictionary of the protocol to the proxy url. Default is None.
  • timeout: (number or tuple, Optional) A number, or a tuple, indicating how many seconds to wait for the client to make a connection and/or send a response. Default is 20 seconds.
  • verify: (bool or str, Optional) A Boolean or a String indication to verify the servers TLS certificate or not. Default is True.

Attributes

  • size: The total size of the file to be downloaded, in bytes.
  • progress: The download progress percentage.
  • speed: The download speed, in MB/s.
  • time_spent: The time spent downloading, in seconds.
  • downloaded: The amount of data downloaded so far, in bytes.
  • eta: The estimated time remaining for download completion, in the format "HH:MM:SS".
  • remaining: The amount of data remaining to be downloaded, in bytes.
  • failed: A flag that indicates if the download failed.
  • completed: A flag that indicates if the download is complete.

Methods

  • start(url, file_path, segments=10, display=True, multithread=True, block=True, retries=0, mirror_func=None, etag=False): Starts the download process.

    Parameters
    • url: (str) The download URL.
    • file_path: (str, Optional) The optional file path to save the download. By default, it uses the present working directory. If file_path is a directory, then the file is downloaded into it; otherwise, the file is downloaded with the given name.
    • segments: (int, Optional) The number of segments the file should be divided into for multi-threaded download.
    • display: (bool, Optional) Whether to display download progress and other optional messages.
    • multithread: (bool, Optional) Whether to use multi-threaded download.
    • block: (bool, Optional) Whether to block until the download is complete.
    • retries: (int, Optional) The number of times to retry the download in case of an error.
    • mirror_func: (function, Optional) A function to get a new download URL in case of an error.
    • etag: (bool, Optional) Whether to validate etag before resuming downloads.

Helper Classes

Basicdown()

The Basicdown class is the base downloader class that provides the basic structure for downloading files.

Attributes
  • curr: The current size of the downloaded file in bytes.
  • completed: A flag that indicates if the download is complete.
  • id: The ID of the current instance.
  • stop: An event that can be used to stop the download process.
  • error: An event that can be used to signal an error.
Methods
  • download(url, path, mode, **kwargs): Downloads data in chunks.

Simpledown()

The Simpledown class extends Basicdown and is responsible for downloading a whole file in a single segment.

Parameters
  • url: Url of the file.
  • file_path: Path to save the file.
  • stop: Stop event.
  • error: Error event.
  • **kwargs: Additional keyword arguments.
Methods
  • worker(): Downloads a whole file in a single segment.

Multidown()

The Multidown class extends Basicdown and is responsible for downloading a specific segment of a file.

Parameters
  • segement_table: Dictionary that contains the download information.
  • segment_id: ID of the download part.
  • stop: Stop event.
  • error: Error event.
  • **kwargs: Additional keyword arguments.
Methods
  • worker(): Downloads a part of the file in multiple segments.

License

pypdl is licensed under the MIT License. See the LICENSE file for more details.

Contribution

Contributions to pypdl are always welcome. If you want to contribute to this project, please fork the repository and submit a pull request.

Contact

If you have any questions, issues, or feedback about pypdl, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypdl-1.2.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

pypdl-1.2.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file pypdl-1.2.0.tar.gz.

File metadata

  • Download URL: pypdl-1.2.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for pypdl-1.2.0.tar.gz
Algorithm Hash digest
SHA256 74a44cfddfdf5eb726f689a7a6f1af91a61baf84850d83cf2fc145f7f2ad2f2e
MD5 be2d32c423617c58f362275dec7ed717
BLAKE2b-256 b9c5041001dbda3b36fdfb1ec9195aa5b3a0e22b4bf3d7a3c48d6de1eb224259

See more details on using hashes here.

File details

Details for the file pypdl-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pypdl-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for pypdl-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3a735f87cf66a0bd82a2e3f9e7b607a8b3ea85302f678b5daa0c3516e2f7f11
MD5 2863f26394d51940bac0abfc709ee1b9
BLAKE2b-256 45ff0480391951a8ec4bf76a6b070bb6e24738fc1de9436869b446340c6475ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page