nvchecker/nvchecker_source/go.py

41 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-03-09 10:41:20 +00:00
# MIT licensed
# Copyright (c) 2024 bgme <i@bgme.me>.
from lxml import html
from nvchecker.api import (
RichResult, Entry, AsyncCache, KeyManager,
2024-03-09 10:48:47 +00:00
session, GetVersionError,
2024-03-09 10:41:20 +00:00
)
GO_PKG_URL = 'https://pkg.go.dev/{pkg}?tab=versions'
GO_PKG_VERSION_URL = 'https://pkg.go.dev/{pkg}@{version}'
2024-03-09 10:41:20 +00:00
async def get_version(
2024-03-09 10:48:47 +00:00
name: str, conf: Entry, *,
cache: AsyncCache, keymanager: KeyManager,
**kwargs,
) -> RichResult:
2024-03-09 10:48:47 +00:00
key = tuple(sorted(conf.items()))
return await cache.get(key, get_version_impl)
2024-03-09 10:41:20 +00:00
async def get_version_impl(info) -> RichResult:
2024-03-09 10:48:47 +00:00
conf = dict(info)
pkg_name = conf.get('go')
url = GO_PKG_URL.format(pkg=pkg_name)
res = await session.get(url)
doc = html.fromstring(res.body.decode())
elements = doc.xpath("//div[@class='Version-tag']/a/text()")
try:
2024-12-02 15:56:11 +00:00
version = elements[0] # type: ignore
return RichResult(
2024-12-02 15:56:11 +00:00
version = version, # type: ignore
url = GO_PKG_VERSION_URL.format(pkg=pkg_name, version=version),
)
2024-03-09 10:48:47 +00:00
except IndexError:
raise GetVersionError("parse error", pkg_name=pkg_name)