2022-09-11 13:38:36 +02:00
|
|
|
from re import fullmatch, Pattern
|
|
|
|
from urllib import request as http
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_version(url: str) -> str:
|
|
|
|
"""
|
|
|
|
Retrieve the latest webpicdownloader version available.
|
2022-09-12 00:29:39 +02:00
|
|
|
This method, only accept 200 http response.
|
2022-09-11 13:38:36 +02:00
|
|
|
|
|
|
|
* :url: -> Url of the "VERSION" file on the repository.
|
|
|
|
* RETURN -> the latests 'VERSION' file content.
|
|
|
|
* THROWABLE -> can raise HTTP or URL error see urllib doc.
|
|
|
|
"""
|
2022-09-12 00:29:39 +02:00
|
|
|
request = http.Request(
|
|
|
|
url=url,
|
|
|
|
headers={'User-Agent': "Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/605.1.15 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/604.1"},
|
|
|
|
method='GET'
|
|
|
|
)
|
2022-09-11 13:38:36 +02:00
|
|
|
response = http.urlopen(request)
|
|
|
|
if response.getcode() != 200:
|
|
|
|
raise HTTPError("Bad response returned by server")
|
|
|
|
return response.read().decode('utf-8')
|
|
|
|
|
2022-09-12 00:29:39 +02:00
|
|
|
def check_for_update(version_current: str, version_url: str, version_pattern: Pattern) -> bool:
|
2022-09-11 13:38:36 +02:00
|
|
|
"""
|
|
|
|
Verify if a new version is available.
|
|
|
|
|
|
|
|
* :current: -> Current version
|
|
|
|
* :version_url: -> Url of the VERSION file on the repository.
|
|
|
|
* :version_pattern: -> Patter to match version retrieved with the version_url.
|
|
|
|
* RETURN -> True means a new version is available, False else.
|
|
|
|
"""
|
|
|
|
version = fetch_version(version_url)
|
|
|
|
if not fullmatch(version_pattern, version):
|
|
|
|
raise ValueError('The version retrieved from the remote server is not valid')
|
|
|
|
return version_current != version
|