From 1e6d8d80cca09356725685783cca529bcb74fa00 Mon Sep 17 00:00:00 2001 From: yzqzss Date: Thu, 20 Jul 2023 19:14:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A7=8B=E7=BB=88=E4=BB=8E=20pypi=20=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E6=9C=80=E6=96=B0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biliarchiver/cli_tools/bili_archive_bvids.py | 4 +++ biliarchiver/exception.py | 9 ++++- biliarchiver/utils/version_check.py | 35 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 biliarchiver/utils/version_check.py diff --git a/biliarchiver/cli_tools/bili_archive_bvids.py b/biliarchiver/cli_tools/bili_archive_bvids.py index 493e813..4fe1eed 100644 --- a/biliarchiver/cli_tools/bili_archive_bvids.py +++ b/biliarchiver/cli_tools/bili_archive_bvids.py @@ -15,6 +15,8 @@ from rich.console import Console from httpx import AsyncClient, Client from rich.traceback import install from biliarchiver.utils.http_patch import HttpOnlyCookie_Handler +from biliarchiver.utils.version_check import check_outdated_version +from biliarchiver.version import BILI_ARCHIVER_VERSION install() from biliarchiver.utils.string import human_readable_upper_part_map @@ -83,6 +85,8 @@ def _main(): with open(args.bvids, 'r', encoding='utf-8') as f: bvids_from_file = f.read().splitlines() + check_outdated_version(pypi_project='biliarchiver', self_version=BILI_ARCHIVER_VERSION) + loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) diff --git a/biliarchiver/exception.py b/biliarchiver/exception.py index 7a32e1c..db1e641 100644 --- a/biliarchiver/exception.py +++ b/biliarchiver/exception.py @@ -3,4 +3,11 @@ class VideosBasePathNotFoundError(FileNotFoundError): self.path = path def __str__(self): - return f"Videos base path {self.path} not found" \ No newline at end of file + return f"Videos base path {self.path} not found" + +class VersionOutdatedError(Exception): + def __init__(self, version): + self.version = version + + def __str__(self): + return "Version outdated: %s" % self.version \ No newline at end of file diff --git a/biliarchiver/utils/version_check.py b/biliarchiver/utils/version_check.py new file mode 100644 index 0000000..878ea00 --- /dev/null +++ b/biliarchiver/utils/version_check.py @@ -0,0 +1,35 @@ +import requests + +from biliarchiver.exception import VersionOutdatedError + +def get_latest_version(pypi_project: str): + '''Returns the latest version of pypi_project.''' + project_url_pypi = f'https://pypi.org/pypi/{pypi_project}/json' + + try: + response = requests.get(project_url_pypi, timeout=5, headers={'Accept': 'application/json', 'Accept-Encoding': 'gzip'}) + except requests.exceptions.Timeout or requests.exceptions.ConnectionError: + print(f'Warning: Could not get latest version of {pypi_project} from pypi.org. (Timeout)') + return None + if response.status_code == 200: + data = response.json() + latest_version: str = data['info']['version'] + return latest_version + else: + print(f'Warning: Could not get latest version of {pypi_project}. HTTP status_code: {response.status_code}') + return None + +def check_outdated_version(pypi_project: str, self_version: str, raise_error: bool = True): + latest_version = get_latest_version(pypi_project) + if latest_version is None: + return + elif latest_version != self_version: + print('=' * 47) + print(f'Warning: You are using an outdated version of {pypi_project} ({self_version}).') + print(f' The latest version is {latest_version}.') + print(f' You can update {pypi_project} with "pip3 install --upgrade {pypi_project}".') + print('=' * 47, end='\n\n') + if raise_error: + raise VersionOutdatedError(version=self_version) + else: # latest_version == self_version + print(f'You are using the latest version of {pypi_project}.')