Skip to content

Commit

Permalink
Fix some things
Browse files Browse the repository at this point in the history
  • Loading branch information
Asthowen committed Jan 5, 2022
1 parent a92fe29 commit 301c2ca
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 45 deletions.
1 change: 0 additions & 1 deletion .github/workflows/generate-rpm-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
runs-on: ubuntu-latest
container:
image: fedora:latest

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
16 changes: 12 additions & 4 deletions .github/workflows/publish-to-aur.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
name: Publish AlphacodersDownloader to AUR

on:
release:
types: [published]
workflow_run:
workflow: 'Publish AlphacodersDownloader to Test-PyPI and PyPI'
types:
- completed
workflow_dispatch:

jobs:
publish_to_aur:
publish-to-aur:
name: Publish AlphacodersDownloader to AUR
runs-on: ubuntu-latest
container:
image: archlinux:latest
steps:
- name: Downloading Files
- name: Checkout code
uses: actions/checkout@v2

- name: Generate checksum
run: |
cd archlinux && pacman -S pacman-contrib --noconfirm && updpkgsums
- name: Update AUR Package
uses: KSXGitHub/github-actions-deploy-aur@v2.2.4
with:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
workflow_dispatch:

jobs:
build-n-publish:
build-and-publish-package:
name: Build and publish AlphacodersDownloader to Test PyPI and PyPI
runs-on: ubuntu-latest
steps:
Expand All @@ -25,6 +25,7 @@ jobs:
- name: Build a binary wheel and a source tarball
run: |
python -m build --sdist --wheel --outdir dist/ .
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@master
with:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ apt update && apt install python3-alphacodersdownloader
```

##### With deb file
Download latest release on: https://github.com/Asthowen/AlphacodersWallpaperDownloader/releases/latest.
Download the latest release on: https://github.com/Asthowen/AlphacodersWallpaperDownloader/releases/latest.

Install package (replace fileName by the file name):
```bash
Expand All @@ -91,7 +91,7 @@ yum check-update && yum install AlphacodersDownloader
```

##### With RPM file
Download latest release on: https://github.com/Asthowen/AlphacodersWallpaperDownloader/releases/latest.
Download the latest release on: https://github.com/Asthowen/AlphacodersWallpaperDownloader/releases/latest.

Install package (replace fileName by the file name):
```bash
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.3"
__version__ = "0.1.3.1"
__author__ = "Asthowen"
__license__ = "GNU v3.0"
66 changes: 44 additions & 22 deletions alphacoders_downloader/alphacoders_downloader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from alphacoders_downloader.utils import limit_tasks, clear_line, print_error
from alphacoders_downloader.exceptions import WallpapersNotFounds
from alphacoders_downloader.cursor import HiddenCursor, show
from alphacoders_downloader.progress_bar import ProgressBar
from bs4 import BeautifulSoup
from typing import Union
import setproctitle
import aiofiles
import asyncio
Expand All @@ -10,13 +12,16 @@
import os


main_class = None


class Main:
def __init__(self, url: str, path: str):
self.url = url
self.path = path if path[-1] == os.sep else path + os.sep
self.temp_path = self.path + 'temp' + os.sep

self.progress_bar: ProgressBar = None
self.progress_bar: Union[ProgressBar, None] = None
self.client_session = None
self.images_list = []
self.links_len = 0
Expand All @@ -26,20 +31,27 @@ def __init__(self, url: str, path: str):

async def parse_url(self, url: str):
async with self.client_session.get(url) as r:
links = BeautifulSoup(await r.text(), 'html.parser')
page = BeautifulSoup(await r.text(), 'html.parser')

for link in links.find('div', attrs={'id': 'big_container'}).findAll('img'):
href = str(link.get('src'))
find_images_urls = page.find('div', attrs={'id': 'big_container'})

if find_images_urls is None:
raise WallpapersNotFounds(url)
else:
for link in find_images_urls.find_all('img'):
href = str(link.get('src'))

if href.startswith('https://images') or href.startswith('https://mfiles'):
images_link_list_split = href.split('/')
images_name_file_thumb = images_link_list_split[len(images_link_list_split) - 1]
images_name_file = images_name_file_thumb.split('-')[len(images_name_file_thumb.split('-')) - 1]
if href.startswith('https://images') or href.startswith('https://mfiles'):
images_link_list_split = href.split('/')
images_name_file_thumb = images_link_list_split[len(images_link_list_split) - 1]
images_name_file = images_name_file_thumb.split('-')[len(images_name_file_thumb.split('-')) - 1]

self.images_list.append([href.replace(images_name_file_thumb, '') + images_name_file, images_name_file])
self.images_list.append(
[href.replace(images_name_file_thumb, '') + images_name_file, images_name_file]
)

self.links_got += 1
self.progress_bar.progress(self.links_got)
self.links_got += 1
self.progress_bar.progress(self.links_got)

async def download(self, element: list):
if os.path.isfile(self.path + element[1]) is False:
Expand Down Expand Up @@ -68,10 +80,10 @@ async def start(self):
self.client_session = aiohttp.ClientSession()

if os.path.exists(self.path) is False:
os.mkdir(self.path)
os.makedirs(self.path)

if os.path.exists(self.temp_path) is False:
os.mkdir(self.temp_path)
os.makedirs(self.temp_path)

pages_list = []
page_char = '&' if 'https://mobile.alphacoders.com/' not in self.url else '?'
Expand Down Expand Up @@ -102,7 +114,7 @@ async def start(self):

self.images_len = len(self.images_list)

self.progress_bar.set_progress_bar_parameters(self.images_len, 'Downloading images', 0, True)
self.progress_bar.set_progress_bar_parameters(self.images_len, 'Downloading backgrounds', 0, True)
await limit_tasks(10, *[self.download(element) for element in self.images_list])

await self.client_session.close()
Expand All @@ -112,16 +124,23 @@ async def start(self):

async def main():
setproctitle.setproctitle('AlphacodersDownloader')
url = input(
'Please enter the download url (e.g. '
'https://wall.alphacoders.com/search.php?search=sword+art+online). > '
).replace(' ', '')
clear_line()
path = input("Please enter the folder where the images are saved (e.g. ~/downloads/backgrounds/). > ")
clear_line()
url = ''
while 'https://' not in url and 'alphacoders.com' not in url:
url = input(
'Please enter the download url (e.g. '
'https://wall.alphacoders.com/search.php?search=sword+art+online). > '
).replace(' ', '')
clear_line()

path = ''
while os.access(os.path.dirname(path), os.W_OK) is False:
path = input('Please enter the folder where the images are saved (e.g. ~/downloads/backgrounds/). > ')
clear_line()

with HiddenCursor() as _:
await Main(url, path).start()
global main_class
main_class = Main(url, path)
await main_class.start()


def start():
Expand All @@ -131,5 +150,8 @@ def start():
print('\nStop the script...')
show()
except Exception as e:
if hasattr(main_class, 'client_session'):
if main_class.client_session is not None and main_class.client_session.closed is False:
asyncio.get_event_loop().run_until_complete(main_class.client_session.close())
print_error(str(e))
show()
10 changes: 5 additions & 5 deletions alphacoders_downloader/cursor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

# Author: James Spencer: http://stackoverflow.com/users/1375885/james-spencer
# Packager: Gijs Timmers: https://github.com/GijsTimmers
# Packager: Gijs Timers: https://github.com/GijsTimmers

# Based on James Spencer's answer on StackOverflow:
# http://stackoverflow.com/q/5174810
Expand All @@ -16,7 +16,7 @@
import ctypes

class _CursorInfo(ctypes.Structure):
_fields_ = [("size", ctypes.c_int), ("visible", ctypes.c_byte)]
_fields_ = [('size', ctypes.c_int), ('visible', ctypes.c_byte)]


def hide(stream=sys.stdout):
Expand All @@ -27,7 +27,7 @@ def hide(stream=sys.stdout):
ci.visible = False
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
stream.write("\033[?25l")
stream.write('\033[?25l')
stream.flush()


Expand All @@ -39,7 +39,7 @@ def show(stream=sys.stdout):
ci.visible = True
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
stream.write("\033[?25h")
stream.write('\033[?25h')
stream.flush()


Expand All @@ -50,5 +50,5 @@ def __init__(self, stream=sys.stdout):
def __enter__(self):
hide(stream=self._stream)

def __exit__(self, type, value, traceback):
def __exit__(self, exit_type, exit_value, exit_traceback):
show(stream=self._stream)
3 changes: 3 additions & 0 deletions alphacoders_downloader/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class WallpapersNotFounds(Exception):
def __init__(self, wallpaper: str):
super().__init__(f"I can't found wallpapers at URL: {wallpaper}!")
5 changes: 3 additions & 2 deletions alphacoders_downloader/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def progress(self, iteration: float):

def __update_progress_bar(self):
f = int(100 * self.iteration // self.total)
print(f"{self.prefix} [{'█' * f + ' ' * (100 - f)}] {100 * (self.iteration / float(self.total)):.2f}%",
end='\r')
print(
f"{self.prefix} [{'█' * f + ' ' * (100 - f)}] {100 * (self.iteration / float(self.total)):.2f}%", end='\r'
)

if self.iteration == self.total:
print()
Expand Down
10 changes: 5 additions & 5 deletions archlinux/PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Maintainer: Asthowen <contact@asthowen.fr>
# Maintainer: squitch <contact.squitch@gmail.com>

pkgname='python-alphacodersdownloader'
pkgver=0.1.3
pkgver=0.1.3.1
pkgrel=1
pkgdesc="A script for download backgrounds on https://alphacoders.com written in Python."
pkgdesc='A script for download backgrounds on https://alphacoders.com written in Python.'
arch=('any')
url='https://github.com/Asthowen/AlphacodersWallpaperDownloader/'
license=('GNUv3')
makedepends=('python-setuptools')
depends=('python>=3.6' 'python-aiohttp' 'python-aiofiles' 'python-setproctitle' 'python-beautifulsoup4')
source=("https://pypi.io/packages/source/A/AlphacodersDownloader/AlphacodersDownloader-$pkgver.tar.gz")
sha256sums=('SKIP')
source=('https://pypi.io/packages/source/A/AlphacodersDownloader/AlphacodersDownloader-$pkgver.tar.gz')

build() {
cd AlphacodersDownloader-$pkgver
Expand All @@ -20,4 +20,4 @@ build() {
package() {
cd AlphacodersDownloader-$pkgver
python setup.py install --root="$pkgdir" --optimize=1 --skip-build
}
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='AlphacodersDownloader',
version='0.1.3',
version='0.1.3.1',
author='Asthowen',
author_email='contact@asthowen.fr',
maintainer='Asthowen',
Expand Down
2 changes: 1 addition & 1 deletion setup_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name='AlphacodersDownloader',
version='0.1.3',
version='0.1.3.1',
author='Asthowen',
author_email='contact@asthowen.fr',
maintainer='Asthowen',
Expand Down

0 comments on commit 301c2ca

Please sign in to comment.