Skip to content

Commit

Permalink
Add download speed limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
Asthowen authored Dec 15, 2022
1 parent 9a86dfb commit 6a0c85f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 26 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,31 @@ Enter a link: https://mobile.alphacoders.com/by-sub-category/227264 or https://w

Enter download path: `~/downloads/wallpapers/` for example.

If you want change download speed, start by typing **y** and after that, enter a new download speed, it must be in Ko.

### With arguments
#### Arguments list
`-S` Link to the wallpaper, must be associated with the `-P` argument.
<br>
`-P` The path to download wallpapers, must be associated with the `-S` argument.
<br>
`-D` The download speed, it must be in Ko.
<br>
`-H` The help command.
<br>
`-V` Get infos about version.

#### Examples
Download wallpaper in `~/downloads/wallpapers/`:
```bash
alphacoders-downloader -S "https://mobile.alphacoders.com/by-sub-category/227264" -P "~/downloads/wallpapers/"
alphacoders-downloader -S "https://mobile.alphacoders.com/by-sub-category/227264" -P "~/downloads/wallpapers/" -D 1024
```

## Dev
**Before committing an update:**
* The code must have a result of 10/10 with pylint, use the command: `pylint --recursive=y alphacoders_downloader/*`
* The code must be cleaned with blake, run the command: `black alphacoders_downloader/ && black setup.py && black build/setup_build.py`

## Author
[<img width="45" src="https://avatars3.githubusercontent.com/u/59535754?s=400&u=48aecdd175dd2dd8867ae063f1973b64d298220b&v=4" alt="Asthowen">](https://github.com/Asthowen)

Expand Down
2 changes: 1 addition & 1 deletion alphacoders_downloader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = "AlphacodersDownloader"
__version__ = "0.1.4.2"
__version__ = "0.1.4.3"
__author__ = "Asthowen"
__license__ = "GNU v3.0"
54 changes: 49 additions & 5 deletions alphacoders_downloader/alphacoders_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@


class AlphacodersDownloader:
def __init__(self, url: str, path: str, client_session: aiohttp.ClientSession):
def __init__(
self, url: str, path: str, size: int, client_session: aiohttp.ClientSession
):
self.url = url
self.path = path if path[-1] == os.sep else path + os.sep
self.size = size
self.client_session = client_session

self.temp_path = self.path + "temp" + os.sep
Expand Down Expand Up @@ -105,7 +108,9 @@ async def download(self, element: list):
write_mode = "ab" if temp_file_exist else "wb"
async with aiofiles.open(temp_path, write_mode) as file:
try:
async for data in request.content.iter_chunked(1024):
async for data in request.content.iter_chunked(
int(self.size / 8)
):
await file.write(data)

file_downloaded += len(data)
Expand Down Expand Up @@ -169,9 +174,22 @@ async def download(command_return: dict):
if os.access(os.path.dirname(path_to_download), os.W_OK) is False:
print_error("This path isn't correct.")
sys.exit()

size = 2048
if "-D" in command_return["args"]:
download_index = command_return["args"].index("-D") + 1
if download_index < len(command_return["args"]):
converted_size = int(command_return["args"][download_index])
if (
converted_size % 8 == 0
and (converted_size / 8) > 0
and ((converted_size / 8) % 8) == 0
):
size = converted_size

async with aiohttp.ClientSession() as client_session:
await AlphacodersDownloader(
wallpapers_url, path_to_download, client_session
wallpapers_url, path_to_download, size, client_session
).start()

@staticmethod
Expand Down Expand Up @@ -204,9 +222,35 @@ async def main():
)
clear_line()

size = None
change_size = False
while size is None:
if change_size is False:
change_size_input = input(
"Do you want to change the default download limit of 2Mo/s (y/n)? > "
)
clear_line()

if change_size_input.lower() in ("y", "yes") or change_size:
change_size = True
new_size_input = input(
"Enter the new speed limit (must be in Ko, and be a multiple of 8) > "
)
clear_line()
if new_size_input.isdigit():
converted = int(new_size_input)
if (
converted % 8 == 0
and (converted / 8) > 0
and ((converted / 8) % 8) == 0
):
size = int(new_size_input)
else:
size = 2048

with HiddenCursor() as _:
async with aiohttp.ClientSession() as client_session:
await AlphacodersDownloader(url, path, client_session).start()
await AlphacodersDownloader(url, path, size, client_session).start()
else:
parser = ArgumentsBuilder(
"A script for download wallpapers on https://alphacoders.com/.",
Expand All @@ -217,7 +261,7 @@ async def main():
"-S",
action=CommandsHandler().download,
description="Download wallpapers.",
command_usage="-S wallpapers_url -P path",
command_usage="-S wallpapers_url -P path -D 1024",
)
parser.add_argument(
"-V",
Expand Down
2 changes: 1 addition & 1 deletion alphacoders_downloader/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = "AlphacodersDownloader"
__version__ = "0.1.4.2"
__version__ = "0.1.4.3"
__author__ = "Asthowen"
__license__ = "GNU v3.0"
2 changes: 1 addition & 1 deletion build/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Maintainer: squitch <[email protected]>

pkgname='python-alphacodersdownloader'
pkgver=0.1.4.2
pkgver=0.1.4.3
pkgrel=1
pkgdesc='A script for download backgrounds on https://alphacoders.com written in Python.'
arch=('any')
Expand Down
6 changes: 4 additions & 2 deletions build/setup_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name='AlphacodersDownloader',
version='0.1.4.2',
version='0.1.4.3',
author='Asthowen',
author_email='[email protected]',
maintainer='Asthowen',
Expand All @@ -14,7 +14,9 @@
url='https://github.com/Asthowen/AlphacodersDownloader',
packages=setuptools.find_packages(),
entry_points={
'console_scripts': ['alphacoders-downloader = alphacoders_downloader.alphacoders_downloader:start']
"console_scripts": [
"alphacoders-downloader = alphacoders_downloader.alphacoders_downloader:start"
]
},
python_requires='>= 3.6',
include_package_data=True,
Expand Down
2 changes: 1 addition & 1 deletion build/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: |
This script allows you to download wallpapers from the site https://alphacoders.com/.
* Download by categories (e.g: https://wall.alphacoders.com/by_sub_category.php?id=207679&name=Date+A+Live+Wallpapers)
* Download mobiles wallpapers (e.g: https://mobile.alphacoders.com/by-sub-category/207679)
version: 0.1.4.2
version: 0.1.4.3
grade: stable
base: core20
architectures:
Expand Down
30 changes: 16 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import setuptools

with open('README.md', 'r', encoding='utf-8', errors='ignore') as f:
with open("README.md", "r", encoding="utf-8", errors="ignore") as f:
long_description = f.read()

setuptools.setup(
name='AlphacodersDownloader',
version='0.1.4.2',
author='Asthowen',
author_email='[email protected]',
maintainer='Asthowen',
maintainer_email='[email protected]',
license='GNU v3.0',
description='A script for download wallpapers on https://alphacoders.com written in Python.',
name="AlphacodersDownloader",
version="0.1.4.3",
author="Asthowen",
author_email="[email protected]",
maintainer="Asthowen",
maintainer_email="[email protected]",
license="GNU v3.0",
description="A script for download wallpapers on https://alphacoders.com written in Python.",
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/Asthowen/AlphacodersDownloader',
long_description_content_type="text/markdown",
url="https://github.com/Asthowen/AlphacodersDownloader",
packages=setuptools.find_packages(),
entry_points={
'console_scripts': ['alphacoders-downloader = alphacoders_downloader.alphacoders_downloader:start']
"console_scripts": [
"alphacoders-downloader = alphacoders_downloader.alphacoders_downloader:start"
]
},
python_requires='>= 3.6',
python_requires=">= 3.6",
include_package_data=True,
install_requires=['aiohttp', 'aiofiles', 'setproctitle', 'beautifulsoup4']
install_requires=["aiohttp", "aiofiles", "setproctitle", "beautifulsoup4"],
)

0 comments on commit 6a0c85f

Please sign in to comment.