From 7739f5c8c0a66cbd131dfc3a0fd8759a9f1d2710 Mon Sep 17 00:00:00 2001 From: Guizai Date: Tue, 22 Feb 2022 20:13:18 +0800 Subject: [PATCH] fix: source sparkle (#210) fix: source sparkle --- nvchecker_source/sparkle.py | 23 +++++++++++++++++++++-- tests/test_sparkle.py | 12 ++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/nvchecker_source/sparkle.py b/nvchecker_source/sparkle.py index 33bd61f..1a754be 100644 --- a/nvchecker_source/sparkle.py +++ b/nvchecker_source/sparkle.py @@ -6,13 +6,32 @@ from xml.etree import ElementTree from nvchecker.api import session +NAMESPACE = 'http://www.andymatuschak.org/xml-namespaces/sparkle' + + async def get_version(name, conf, *, cache, **kwargs): sparkle = conf['sparkle'] return await cache.get(sparkle, get_version_impl) + async def get_version_impl(sparkle): res = await session.get(sparkle) root = ElementTree.fromstring(res.body) - version = root.find('./channel/item[1]/{http://www.andymatuschak.org/xml-namespaces/sparkle}version') + item = root.find('./channel/item[1]/enclosure') - return version.text + version_string = item.get(f'{{{NAMESPACE}}}shortVersionString') + build_number = item.get(f'{{{NAMESPACE}}}version') + + 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) + + return '-'.join(version) if version else None diff --git a/tests/test_sparkle.py b/tests/test_sparkle.py index 368d9c6..88446a8 100644 --- a/tests/test_sparkle.py +++ b/tests/test_sparkle.py @@ -6,7 +6,11 @@ import pytest pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] async def test_sparkle(get_version): - assert await get_version("example", { - "source": "sparkle", - "sparkle": "https://sparkle-project.org/files/sparkletestcast.xml", - }) == "2.0" + assert await get_version('example', { + 'source': 'sparkle', + 'sparkle': ( + 'https://raw.githubusercontent.com/sparkle-project/Sparkle/' + 'f453625573fc9a251760b65c74df59023b1471c1/Tests/Resources/' + 'testlocalizedreleasenotesappcast.xml' + ), + }) == '6.0'