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 (
|
2024-04-23 21:27:06 +00:00
|
|
|
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'
|
2024-04-23 21:27:06 +00:00
|
|
|
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,
|
2024-04-23 21:27:06 +00:00
|
|
|
) -> 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
|
|
|
|
|
|
|
|
2024-04-23 21:27:06 +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
|
2024-04-23 21:27:06 +00:00
|
|
|
return RichResult(
|
2024-12-02 15:56:11 +00:00
|
|
|
version = version, # type: ignore
|
2024-04-23 21:27:06 +00:00
|
|
|
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)
|