始终从 pypi 检查最新版本

This commit is contained in:
yzqzss 2023-07-20 19:14:09 +08:00
parent 76dbe05cbe
commit 1e6d8d80cc
3 changed files with 47 additions and 1 deletions

View File

@ -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)

View File

@ -3,4 +3,11 @@ class VideosBasePathNotFoundError(FileNotFoundError):
self.path = path
def __str__(self):
return f"Videos base path {self.path} not found"
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

View File

@ -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}.')