2017-02-28 07:24:53 +00:00
|
|
|
# MIT licensed
|
2020-08-14 10:25:48 +00:00
|
|
|
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
2017-02-28 07:24:53 +00:00
|
|
|
|
2013-11-03 10:21:50 +00:00
|
|
|
import re
|
|
|
|
import sre_constants
|
2018-04-11 08:18:41 +00:00
|
|
|
|
2020-08-14 12:04:05 +00:00
|
|
|
from nvchecker.api import session, GetVersionError
|
2013-11-03 10:21:50 +00:00
|
|
|
|
2020-08-14 10:25:48 +00:00
|
|
|
async def get_version(name, conf, *, cache, **kwargs):
|
|
|
|
key = sorted(conf.items())
|
|
|
|
return await cache.get(key, get_version_impl)
|
|
|
|
|
|
|
|
async def get_version_impl(info):
|
|
|
|
conf = dict(info)
|
|
|
|
|
2013-11-03 10:21:50 +00:00
|
|
|
try:
|
2017-07-04 09:04:29 +00:00
|
|
|
regex = re.compile(conf['regex'])
|
2020-08-14 10:25:48 +00:00
|
|
|
except sre_constants.error as e:
|
|
|
|
raise GetVersionError('bad regex', exc_info=e)
|
2013-11-03 10:21:50 +00:00
|
|
|
|
|
|
|
encoding = conf.get('encoding', 'latin1')
|
|
|
|
|
|
|
|
kwargs = {}
|
2017-07-04 09:04:29 +00:00
|
|
|
headers = {}
|
2013-11-03 10:21:50 +00:00
|
|
|
if conf.get('proxy'):
|
2017-07-04 09:04:29 +00:00
|
|
|
kwargs["proxy"] = conf.get("proxy")
|
2015-03-14 07:54:26 +00:00
|
|
|
if conf.get('user_agent'):
|
2017-07-04 09:04:29 +00:00
|
|
|
headers['User-Agent'] = conf['user_agent']
|
2013-11-03 10:21:50 +00:00
|
|
|
|
2017-07-04 09:04:29 +00:00
|
|
|
async with session.get(conf['url'], headers=headers, **kwargs) as res:
|
2017-07-08 07:48:03 +00:00
|
|
|
body = (await res.read()).decode(encoding)
|
2013-12-03 16:34:14 +00:00
|
|
|
try:
|
2019-03-06 13:51:04 +00:00
|
|
|
version = regex.findall(body)
|
2017-07-08 07:48:03 +00:00
|
|
|
except ValueError:
|
|
|
|
version = None
|
2020-08-14 10:25:48 +00:00
|
|
|
if not conf.get('missing_ok', False):
|
|
|
|
raise GetVersionError('version string not found.')
|
2017-07-08 07:59:54 +00:00
|
|
|
return version
|