From 00ae2c44faa6da947f6bf76a42c5ca2f66ee9b2f Mon Sep 17 00:00:00 2001 From: Danila Date: Mon, 6 Feb 2023 01:53:38 +0300 Subject: [PATCH] refactor: requests module replaced with built-in urllib --- main.py | 13 +++++++++++-- requirements.txt | 1 - system/lib/__init__.py | 27 ++++++-------------------- system/lib/features/initialization.py | 9 +-------- system/lib/features/update/check.py | 24 +++++++++++------------ system/lib/features/update/download.py | 4 ++-- 6 files changed, 32 insertions(+), 46 deletions(-) diff --git a/main.py b/main.py index c388650..3882029 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ # Refactored by Vorono4ka -# Finished ~99% import time @@ -9,7 +8,14 @@ raise RuntimeError("Please, install loguru using pip") from system import clear -from system.lib import config, locale, refill_menu, menu +from system.lib import ( + config, + locale, + refill_menu, + menu, + check_auto_update, + check_files_updated, +) from system.lib.features.initialization import initialize @@ -21,6 +27,9 @@ def main(): initialize(True) exit() + check_auto_update() + check_files_updated() + refill_menu() while True: diff --git a/requirements.txt b/requirements.txt index 1dc231d..062cc97 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ sc-compression -requests colorama pylzham zstandard diff --git a/system/lib/__init__.py b/system/lib/__init__.py index d786e29..b361089 100644 --- a/system/lib/__init__.py +++ b/system/lib/__init__.py @@ -33,17 +33,14 @@ locale.load(config.language) -try: - # noinspection PyUnresolvedReferences - import requests - - del requests - +def check_auto_update(): if config.auto_update and time.time() - config.last_update > 60 * 60 * 24 * 7: check_update() config.last_update = int(time.time()) config.dump() + +def check_files_updated(): if config.has_update: logger.opt(colors=True).info(f'{locale.update_done % ""}') if Console.question(locale.done_qu): @@ -56,8 +53,6 @@ config.dump() else: exit() -except ImportError: - pass # noinspection PyUnresolvedReferences @@ -131,19 +126,9 @@ def refill_menu(): logger.warning(locale.install_to_unlock % "sc-compression") other = Menu.Category(10, locale.other_features_label) - try: - import requests - - del requests - - other.add( - Menu.Item( - locale.check_update, locale.version % config.version, check_update - ) - ) - except ImportError: - logger.warning(locale.install_to_unlock % "requests") - + other.add( + Menu.Item(locale.check_update, locale.version % config.version, check_update) + ) other.add(Menu.Item(locale.check_for_outdated, None, check_for_outdated)) other.add( Menu.Item( diff --git a/system/lib/features/initialization.py b/system/lib/features/initialization.py index 0f6accb..8db6997 100644 --- a/system/lib/features/initialization.py +++ b/system/lib/features/initialization.py @@ -47,14 +47,7 @@ def initialize(first_init=False): logger.info(locale.verifying) config.initialized = True - try: - # noinspection PyUnresolvedReferences - import requests - - del requests - config.version = get_tags("vorono4ka", "xcoder")[0]["name"][1:] - except ImportError as exception: - logger.exception(exception) + config.version = get_tags("vorono4ka", "xcoder")[0]["name"][1:] config.dump() if first_init: diff --git a/system/lib/features/update/check.py b/system/lib/features/update/check.py index 075a38d..ea3637b 100644 --- a/system/lib/features/update/check.py +++ b/system/lib/features/update/check.py @@ -1,3 +1,4 @@ +import json import os from loguru import logger @@ -38,21 +39,20 @@ def get_pip_info(outdated: bool = False) -> list: def get_tags(owner: str, repo: str): api_url = "https://api.github.com" - tags = [] - import requests + import urllib.request - try: - tags = requests.get( + tags = json.loads( + urllib.request.urlopen( api_url + "/repos/{owner}/{repo}/tags".format(owner=owner, repo=repo) - ).json() - tags = [ - {key: v for key, v in tag.items() if key in ["name", "zipball_url"]} - for tag in tags - ] - except Exception: - pass - del requests + ) + .read() + .decode() + ) + tags = [ + {key: v for key, v in tag.items() if key in ["name", "zipball_url"]} + for tag in tags + ] return tags diff --git a/system/lib/features/update/download.py b/system/lib/features/update/download.py index 0e89cbd..ef6b9d1 100644 --- a/system/lib/features/update/download.py +++ b/system/lib/features/update/download.py @@ -22,10 +22,10 @@ def download_update(zip_url): os.mkdir("updates") try: - import requests + import urllib.request with open("updates/update.zip", "wb") as f: - f.write(requests.get(zip_url).content) + f.write(urllib.request.urlopen(zip_url).read()) f.close() import zipfile