nvchecker/nvchecker_source/regex.py

35 lines
997 B
Python
Raw Normal View History

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):
2020-08-17 08:21:02 +00:00
key = tuple(sorted(conf.items()))
2020-08-14 10:25:48 +00:00
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')
data = conf.get('post_data')
if data is None:
res = await session.get(conf['url'])
else:
res = await session.post(conf['url'], body = data, headers = {
'Content-Type': conf.get('post_data_type', 'application/x-www-form-urlencoded')
})
2020-08-17 08:21:02 +00:00
body = res.body.decode(encoding)
versions = regex.findall(body)
if not versions and not conf.get('missing_ok', False):
raise GetVersionError('version string not found.')
return versions