mirror of
https://github.com/lilydjwg/nvchecker.git
synced 2025-03-10 06:14:02 +00:00
revert httpheader and regex file
This commit is contained in:
parent
c1a7bebc78
commit
4aa5078169
2 changed files with 46 additions and 77 deletions
|
@ -3,53 +3,37 @@
|
|||
|
||||
import re
|
||||
import sre_constants
|
||||
from nvchecker.api import (
|
||||
VersionResult, Entry, KeyManager,
|
||||
TemporaryError, session, GetVersionError
|
||||
)
|
||||
|
||||
async def get_version(name, conf, **kwargs):
|
||||
return await get_version_real(name, conf, **kwargs)
|
||||
from nvchecker.api import session, GetVersionError
|
||||
|
||||
async def get_version_real(
|
||||
name: str, conf: Entry, *, keymanager: KeyManager,
|
||||
**kwargs,
|
||||
) -> VersionResult:
|
||||
async def get_version(name, conf, *, cache, **kwargs):
|
||||
key = tuple(sorted(conf.items()))
|
||||
return await cache.get(key, get_version_impl)
|
||||
|
||||
url = conf.get('url')
|
||||
header = conf.get('header', 'Location')
|
||||
follow_redirects = conf.get('follow_redirects', False)
|
||||
method = conf.get('method', 'HEAD')
|
||||
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')
|
||||
|
||||
# Load token from config
|
||||
token = conf.get('token')
|
||||
# Load token from keyman
|
||||
if token is None:
|
||||
key_name = 'httpheader_' + name
|
||||
token = keymanager.get_key(key_name)
|
||||
try:
|
||||
regex = re.compile(conf['regex'])
|
||||
except sre_constants.error as e:
|
||||
raise GetVersionError('bad regex', exc_info=e)
|
||||
|
||||
# Set private token if token exists.
|
||||
headers = {}
|
||||
if token:
|
||||
headers["Authorization"] = token
|
||||
res = await session.request(
|
||||
url,
|
||||
method = method,
|
||||
follow_redirects = follow_redirects,
|
||||
)
|
||||
|
||||
try:
|
||||
regex = re.compile(conf['regex'])
|
||||
except sre_constants.error as e:
|
||||
raise GetVersionError('bad regex', exc_info=e)
|
||||
header_value = res.headers.get(header)
|
||||
if not header_value:
|
||||
raise GetVersionError('header %s not found or is empty' % header)
|
||||
|
||||
res = await session.request(
|
||||
url,
|
||||
method=method,
|
||||
headers=headers,
|
||||
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
|
||||
try:
|
||||
version = regex.findall(header_value)
|
||||
except ValueError:
|
||||
raise GetVersionError('version string not found.')
|
||||
return version
|
||||
|
|
|
@ -3,43 +3,28 @@
|
|||
|
||||
import re
|
||||
import sre_constants
|
||||
from nvchecker.api import (
|
||||
VersionResult, Entry, KeyManager,
|
||||
TemporaryError, session, GetVersionError
|
||||
)
|
||||
|
||||
async def get_version(name, conf, **kwargs):
|
||||
return await get_version_real(name, conf, **kwargs)
|
||||
from nvchecker.api import session, GetVersionError
|
||||
|
||||
async def get_version_real(
|
||||
name: str, conf: Entry, *, keymanager: KeyManager,
|
||||
**kwargs,
|
||||
) -> VersionResult:
|
||||
async def get_version(name, conf, *, cache, **kwargs):
|
||||
key = tuple(sorted(conf.items()))
|
||||
return await cache.get(key, get_version_impl)
|
||||
|
||||
# Load token from config
|
||||
token = conf.get('token')
|
||||
# Load token from keyman
|
||||
if token is None:
|
||||
key_name = 'regex_' + name
|
||||
token = keymanager.get_key(key_name)
|
||||
async def get_version_impl(info):
|
||||
conf = dict(info)
|
||||
|
||||
# Set private token if token exists.
|
||||
headers = {}
|
||||
if token:
|
||||
headers["Authorization"] = token
|
||||
try:
|
||||
regex = re.compile(conf['regex'])
|
||||
except sre_constants.error as e:
|
||||
raise GetVersionError('bad regex', exc_info=e)
|
||||
|
||||
try:
|
||||
regex = re.compile(conf['regex'])
|
||||
except sre_constants.error as e:
|
||||
raise GetVersionError('bad regex', exc_info=e)
|
||||
encoding = conf.get('encoding', 'latin1')
|
||||
|
||||
encoding = conf.get('encoding', 'latin1')
|
||||
|
||||
res = await session.get(conf.get('url'), headers=headers)
|
||||
body = res.body.decode(encoding)
|
||||
try:
|
||||
version = regex.findall(body)
|
||||
except ValueError:
|
||||
if not conf.get('missing_ok', False):
|
||||
raise GetVersionError('version string not found.')
|
||||
return version
|
||||
res = await session.get(conf['url'])
|
||||
body = res.body.decode(encoding)
|
||||
try:
|
||||
version = regex.findall(body)
|
||||
except ValueError:
|
||||
if not conf.get('missing_ok', False):
|
||||
raise GetVersionError('version string not found.')
|
||||
return version
|
||||
|
|
Loading…
Add table
Reference in a new issue