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. This method, only accept 200 http response. * :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. """ 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' ) response = http.urlopen(request) if response.getcode() != 200: raise HTTPError("Bad response returned by server") return response.read().decode('utf-8') def check_for_update(version_current: str, version_url: str, version_pattern: Pattern) -> bool: """ 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