2021-01-28 10:05:58 +00:00
|
|
|
# MIT licensed
|
|
|
|
# Copyright (c) 2021 lilydjwg <lilydjwg@gmail.com>, et al.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from nvchecker.api import session, GetVersionError
|
|
|
|
|
|
|
|
async def get_version(name, conf, *, cache, **kwargs):
|
|
|
|
key = tuple(sorted(conf.items()))
|
|
|
|
return await cache.get(key, get_version_impl)
|
|
|
|
|
|
|
|
async def get_version_impl(info):
|
|
|
|
conf = dict(info)
|
|
|
|
url = conf['url']
|
|
|
|
header = conf.get('header', 'Location')
|
|
|
|
follow_redirects = conf.get('follow_redirects', False)
|
|
|
|
method = conf.get('method', 'HEAD')
|
|
|
|
|
|
|
|
try:
|
|
|
|
regex = re.compile(conf['regex'])
|
2023-04-30 11:12:34 +00:00
|
|
|
except re.error as e:
|
2021-01-28 10:05:58 +00:00
|
|
|
raise GetVersionError('bad regex', exc_info=e)
|
|
|
|
|
|
|
|
res = await session.request(
|
|
|
|
url,
|
|
|
|
method = method,
|
|
|
|
follow_redirects = follow_redirects,
|
|
|
|
)
|
|
|
|
|
|
|
|
header_value = res.headers.get(header)
|
|
|
|
if not header_value:
|
|
|
|
raise GetVersionError('header %s not found or is empty' % header)
|
|
|
|
|
|
|
|
try:
|
|
|
|
version = regex.findall(header_value)
|
|
|
|
except ValueError:
|
|
|
|
raise GetVersionError('version string not found.')
|
|
|
|
return version
|