From ce088e4540e338ac8d2d02d9dac9c61c191eaaf3 Mon Sep 17 00:00:00 2001 From: Asthowen Date: Sat, 15 Oct 2022 22:01:49 +0200 Subject: [PATCH] Add pylint --- .pylintrc | 2 + .../alphacoders_downloader.py | 53 ++++++++++--------- .../util/arguments_builder.py | 20 +++---- alphacoders_downloader/util/cursor.py | 26 +++++---- alphacoders_downloader/util/progress_bar.py | 5 +- setup.cfg | 48 +++++++++++++++++ 6 files changed, 102 insertions(+), 52 deletions(-) create mode 100644 .pylintrc create mode 100644 setup.cfg diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..353b4d4 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,2 @@ +[MASTER] +disable=C0114,C0115,C0116,W1203,C0301,C0411,C0412,E0611,R0902,R0915,R0913,R0903,R0914,R0912,C0207,R0916 \ No newline at end of file diff --git a/alphacoders_downloader/alphacoders_downloader.py b/alphacoders_downloader/alphacoders_downloader.py index 7fa4aae..be1b034 100644 --- a/alphacoders_downloader/alphacoders_downloader.py +++ b/alphacoders_downloader/alphacoders_downloader.py @@ -4,6 +4,7 @@ from alphacoders_downloader.exceptions import WallpapersNotFounds from alphacoders_downloader.util.cursor import HiddenCursor, show from alphacoders_downloader.util.progress_bar import ProgressBar +from alphacoders_downloader import __version__, __license__ from alphacoders_downloader.util.spinner import Spinner from bs4 import BeautifulSoup from typing import Union @@ -39,36 +40,36 @@ async def fetch_images(self, image_url: str): images_name_file = images_name_file_thumb.split('-')[len(images_name_file_thumb.split('-')) - 1] if os.path.isfile(os.path.join(self.path, images_name_file)) is False: image_url = image_url.replace(images_name_file_thumb, '') + images_name_file - async with self.client_session.head(image_url) as r: - file_size = int(r.headers['Content-Length']) + async with self.client_session.head(image_url) as request: + file_size = int(request.headers['Content-Length']) self.total_size_to_download += file_size self.images_list.append([image_url, images_name_file, file_size]) async def parse_url(self, url: str): - async with self.client_session.get(url, cookies={'AlphaCodersView': 'paged'}) as r: - page = BeautifulSoup(await r.text(), 'html.parser') + async with self.client_session.get(url, cookies={'AlphaCodersView': 'paged'}) as request: + page = BeautifulSoup(await request.text(), 'html.parser') find_images_urls = page.find('div', {'id': 'page_container'}).find_all('div', 'thumb-container-big') if find_images_urls is None: raise WallpapersNotFounds(url) - else: - for link in find_images_urls: - href = str(link.find('img').get('src')) - if (href.startswith('https://images') or href.startswith( - 'https://mfiles')) and href not in self.temp_images_list: - self.temp_images_list.append(href) + for a_element in find_images_urls: + href = str(a_element.find('img').get('src')) + + if (href.startswith('https://images') or href.startswith('https://mfiles')) and href not in \ + self.temp_images_list: + self.temp_images_list.append(href) - self.links_got += 1 + self.links_got += 1 - a_element = page.find('div', {'class': 'pagination-simple center'}).find_all('a') + a_elements = page.find('div', {'class': 'pagination-simple center'}).find_all('a') changed_element = None - if a_element is not None and a_element: - for x in a_element: - if x.text.strip() == 'Next >>': - changed_element = x + if a_elements is not None and a_elements: + for a_element in a_elements: + if a_element.text.strip() == 'Next >>': + changed_element = a_element break if changed_element is not None: url = changed_element.get('href') @@ -90,13 +91,13 @@ async def download(self, element: list): headers['Range'] = f'bytes={file_size}-' file_downloaded = 0 - async with self.client_session.get(element[0], headers=headers) as r: + async with self.client_session.get(element[0], headers=headers) as request: try: write_mode = 'ab' if temp_file_exist else 'wb' - async with aiofiles.open(temp_path, write_mode) as f: + async with aiofiles.open(temp_path, write_mode) as file: try: - async for data in r.content.iter_chunked(1024): - await f.write(data) + async for data in request.content.iter_chunked(1024): + await file.write(data) file_downloaded += len(data) self.progress_bar.progress(len(data)) @@ -150,10 +151,8 @@ async def download(command_return: dict): @staticmethod def get_version(_): - from alphacoders_downloader import __version__, __license__ - version_text = f'\033[1mAlphacodersDownloader {__version__}\033[0m\n' - version_text += f'Created by \033[1mAsthowen\033[0m - \033[1mcontact@asthowen.fr\033[0m\n' + version_text += 'Created by \033[1mAsthowen\033[0m - \033[1mcontact@asthowen.fr\033[0m\n' version_text += f'License: \033[1m{__license__}\033[0m' print(version_text) @@ -196,16 +195,18 @@ async def main(): def start(): + # pylint: disable=W0703 try: os.get_terminal_size(0) asyncio.get_event_loop().run_until_complete(main()) except OSError: - print_error('Your terminal does not support all the features needed for AlphacodersDownloader, please use another one.') + print_error('Your terminal does not support all the features needed for AlphacodersDownloader, please use ' + 'another one.') show() except KeyboardInterrupt: clear_line() print('Stop the script...') show() - except Exception as e: - print_error(str(e)) + except Exception as exception: + print_error(str(exception)) show() diff --git a/alphacoders_downloader/util/arguments_builder.py b/alphacoders_downloader/util/arguments_builder.py index 298aa32..a49a16a 100644 --- a/alphacoders_downloader/util/arguments_builder.py +++ b/alphacoders_downloader/util/arguments_builder.py @@ -25,9 +25,9 @@ def add_argument( def build_help(self): if self.__help_content is None: self.__help_content = f"\n\033[1m{self.description}\033[0m\n\nCommand list:\n" - for x in self.arguments: - self.__help_content += f'・\033[1m{self.command_base} {self.arguments[x]["command_usage"]}\033[0m ' \ - f'| {self.arguments[x]["description"]}\n' + for _, command_json in self.arguments.items(): + self.__help_content += f'・\033[1m{self.command_base} {command_json["command_usage"]}\033[' \ + f'0m | {command_json["description"]}\n' print(self.__help_content) @@ -37,14 +37,14 @@ async def build(self): has_been_found = False - for i, x in enumerate(self.args): - x = x.lower() - if x in self.arguments: + for argument in self.args: + argument = argument.lower() + if argument in self.arguments: has_been_found = True - self.arguments[x]['args'] = self.args - if asyncio.iscoroutinefunction(self.arguments[x]['action']): - await self.arguments[x]['action'](self.arguments[x]) + self.arguments[argument]['args'] = self.args + if asyncio.iscoroutinefunction(self.arguments[argument]['action']): + await self.arguments[argument]['action'](self.arguments[argument]) else: - self.arguments[x]['action'](self.arguments[x]) + self.arguments[argument]['action'](self.arguments[argument]) if has_been_found is False: print_error(f"\033[1mThis command doesn't exist. Please check the command: {self.command_base} -H.\033[0m") diff --git a/alphacoders_downloader/util/cursor.py b/alphacoders_downloader/util/cursor.py index 3da83e4..acb73e1 100644 --- a/alphacoders_downloader/util/cursor.py +++ b/alphacoders_downloader/util/cursor.py @@ -1,49 +1,47 @@ -# -*- coding: utf-8 -*- - # Author: James Spencer: http://stackoverflow.com/users/1375885/james-spencer # Packager: Gijs Timers: https://github.com/GijsTimmers # Based on James Spencer's answer on StackOverflow: # http://stackoverflow.com/q/5174810 -# Licence: GPL v3 -# https://www.gnu.org/licenses/gpl-3.0.html - import sys import os if os.name == 'nt': import ctypes + class _CursorInfo(ctypes.Structure): _fields_ = [('size', ctypes.c_int), ('visible', ctypes.c_byte)] +# pylint: disable=W0201 def hide(stream=sys.stdout): if os.name == 'nt': - ci = _CursorInfo() + cursor_info = _CursorInfo() handle = ctypes.windll.kernel32.GetStdHandle(-11) - ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci)) - ci.visible = False - ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci)) + ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(cursor_info)) + cursor_info.visible = False + ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(cursor_info)) elif os.name == 'posix': stream.write('\033[?25l') stream.flush() +# pylint: disable=W0201 def show(stream=sys.stdout): if os.name == 'nt': - ci = _CursorInfo() + cursor_info = _CursorInfo() handle = ctypes.windll.kernel32.GetStdHandle(-11) - ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci)) - ci.visible = True - ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci)) + ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(cursor_info)) + cursor_info.visible = True + ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(cursor_info)) elif os.name == 'posix': stream.write('\033[?25h') stream.flush() -class HiddenCursor(object): +class HiddenCursor: def __init__(self, stream=sys.stdout): self._stream = stream diff --git a/alphacoders_downloader/util/progress_bar.py b/alphacoders_downloader/util/progress_bar.py index 8c379fe..dd9832e 100644 --- a/alphacoders_downloader/util/progress_bar.py +++ b/alphacoders_downloader/util/progress_bar.py @@ -99,14 +99,15 @@ def __update_progress_bar(self): self.is_started = False @staticmethod - def __parse_size(num): + def __parse_size(num) -> str: for unit in ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'): if abs(num) < 1024.0: return f"{num:3.1f}{unit}/s" num /= 1024.0 + return '0B/s' @staticmethod - def __parse_duration(duration: int): + def __parse_duration(duration: int) -> str: minutes, seconds = divmod(duration, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..36c3ba5 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,48 @@ +[metadata] +name = AlphacodersDownloader +version = attr: alphacoders_downloader.__version__ +url = https://github.com/Asthowen/AlphacodersDownloader +project_urls = + GitHub: issues = https://github.com/Asthowen/AlphacodersDownloader/issues + GitHub: repo = https://github.com/Asthowen/AlphacodersDownloader +description = A script for download wallpapers on https://alphacoders.com. +long_description = file: README.md +long_description_content_type = text/markdown +maintainer = Asthowen +maintainer_email = contact@asthowen.fr +license = GPLv3 +license_files = LICENSE +classifiers = + Development Status :: 5 - Production/Stable + + Framework :: AsyncIO + + Intended Audience :: Developers + + License :: OSI Approved :: GNU General Public License v3 (GPLv3) + + Operating System :: POSIX + Operating System :: MacOS :: MacOS X + Operating System :: Microsoft :: Windows + + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + + Topic :: Internet :: WWW/HTTP + +[options] +python_requires = >=3.7 +packages = find: +# https://setuptools.readthedocs.io/en/latest/setuptools.html#setting-the-zip-safe-flag +zip_safe = False +include_package_data = True + +install_requires = + beautifulsoup4 >= 4 + setproctitle >= 1.3.1 + aiofiles >= 0.8.0 + aiohttp >= 3.8 \ No newline at end of file