2020-05-05 04:20:36 +00:00
|
|
|
# MIT licensed
|
2020-08-14 10:25:48 +00:00
|
|
|
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
2020-05-05 04:20:36 +00:00
|
|
|
# Copyright (c) 2020 Sunlei <guizaicn@gmail.com>
|
|
|
|
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
from nvchecker.api import session, RichResult
|
2022-02-22 12:13:18 +00:00
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace'
|
|
|
|
SPARKLE_NAMESPACE = 'http://www.andymatuschak.org/xml-namespaces/sparkle'
|
2022-02-22 12:13:18 +00:00
|
|
|
|
2020-08-14 10:25:48 +00:00
|
|
|
async def get_version(name, conf, *, cache, **kwargs):
|
2020-05-05 04:20:36 +00:00
|
|
|
sparkle = conf['sparkle']
|
2023-10-18 00:00:46 +00:00
|
|
|
release_notes_language = conf.get('release_notes_language', 'en')
|
|
|
|
return await cache.get((sparkle, release_notes_language), get_version_impl)
|
2020-05-05 04:20:36 +00:00
|
|
|
|
2022-02-22 12:13:18 +00:00
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
async def get_version_impl(info):
|
|
|
|
sparkle, release_notes_language = info
|
2020-08-17 08:21:02 +00:00
|
|
|
res = await session.get(sparkle)
|
2023-10-18 00:00:46 +00:00
|
|
|
root = ElementTree.fromstring(res.body).find('./channel/item[1]')
|
|
|
|
item = root.find('./enclosure')
|
2022-02-22 12:13:18 +00:00
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
version_string = item.get(f'{{{SPARKLE_NAMESPACE}}}shortVersionString')
|
|
|
|
build_number = item.get(f'{{{SPARKLE_NAMESPACE}}}version')
|
2022-02-22 12:13:18 +00:00
|
|
|
|
|
|
|
if (version_string and version_string.isdigit()) and (
|
|
|
|
build_number and not build_number.isdigit()
|
|
|
|
):
|
|
|
|
version_string, build_number = build_number, version_string
|
|
|
|
|
|
|
|
version = []
|
|
|
|
|
|
|
|
if version_string:
|
|
|
|
version.append(version_string)
|
|
|
|
if build_number and (build_number not in version):
|
|
|
|
version.append(build_number)
|
2020-05-05 04:20:36 +00:00
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
version_str = '-'.join(version) if version else None
|
|
|
|
|
|
|
|
release_notes_link = None
|
|
|
|
for release_notes in root.findall(f'./{{{SPARKLE_NAMESPACE}}}releaseNotesLink'):
|
|
|
|
language = release_notes.get(f'{{{XML_NAMESPACE}}}lang')
|
|
|
|
|
|
|
|
# If the release notes have no language set, store them, but keep looking for our preferred language
|
|
|
|
if language is None:
|
|
|
|
release_notes_link = release_notes.text.strip()
|
|
|
|
|
|
|
|
# If the release notes match our preferred language, store them and stop looking
|
|
|
|
if language == release_notes_language:
|
|
|
|
release_notes_link = release_notes.text.strip()
|
|
|
|
break
|
|
|
|
|
|
|
|
if release_notes_link is not None:
|
|
|
|
return RichResult(
|
|
|
|
version = version_str,
|
|
|
|
url = release_notes_link,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return version_str
|