Skip to content

Commit

Permalink
Add support for dynamic url
Browse files Browse the repository at this point in the history
  • Loading branch information
mjishnu committed Aug 1, 2024
1 parent d659f40 commit 2d6a0c9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ dl.start(
Each option is explained below:
- `allow_reuse`: Whether to allow reuse of existing Pypdl object for next download. The default value is `False`.
- `logger`: A logger object to log messages. The default value is custom `Logger` with the name *Pypdl*.
- `url`: The URL of the file to download.
- `url`: This can either be the URL of the file to download or a function that returns the URL.
- `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-segmented download. The default value is 10.
- `display`: Whether to display download progress and other optional messages. The default value is `True`.
Expand Down Expand Up @@ -157,25 +157,29 @@ while not d.completed:

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.

Another example of using hash validation:
Another example of using hash validation with dynamic url:

```py
from pypdl import Pypdl

# Generate the url dynamically
def dynamic_url():
return 'https://example.com/file.zip'

# create a pypdl object
dl = Pypdl()

# if block = True --> returns a FileValidator object
file = dl.start('https://example.com/file.zip', block=True)
file = dl.start(dynamic_url, block=True)

# validate hash
if file.validate_hash(correct_hash,'sha256'):
print('Hash is valid')
else:
print('Hash is invalid')

# if block = False --> returns a AutoShutdownFuture object
file = dl.start('https://example.com/file.zip', block=False)
# scenario where block = False --> returns a AutoShutdownFuture object
file = dl.start(dynamic_url, block=False)

# do something
# ...
Expand Down Expand Up @@ -338,7 +342,7 @@ The `Pypdl` class represents a file downloader that can download a file from a g

##### Parameters

- `url`: (str) The download URL.
- `url`: ((str, function), Required) This can either be the URL of the file to download or a function that returns the 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-segmented download.
- `display`: (bool, Optional) Whether to display download progress and other optional messages.
Expand Down
2 changes: 1 addition & 1 deletion pypdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.4.3"
__version__ = "1.4.4"

from .pypdl_manager import Pypdl
from .pypdl_factory import PypdlFactory
4 changes: 3 additions & 1 deletion pypdl/pypdl_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def start(
Start the download process.
Parameters:
url (str): The URL to download from.
url (Callable[[], str], Required): This can either be the URL of the file to download or a function that returns the URL.
file_path (str, Optional): The path to save the downloaded file. If not provided, the file is saved in the current working directory.
If `file_path` is a directory, the file is saved in that directory. If `file_path` is a file name, the file is saved with that name.
segments (int, Optional): The number of segments to divide the file into for multi-segment download. Default is 10.
Expand Down Expand Up @@ -127,6 +127,8 @@ def download():
return None

self._reset()
url = url() if callable(url) else url

if self._allow_reuse:
future = self._pool.submit(download)
else:
Expand Down

0 comments on commit 2d6a0c9

Please sign in to comment.