badge(opam): swap cheerio with regex matching (#460)

pull/469/head
Dario Vladović 2020-12-19 05:29:27 +01:00 zatwierdzone przez GitHub
rodzic 547c421ba1
commit 1c055a7858
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 3 dodań i 32 usunięć

Wyświetl plik

@ -1,16 +1,9 @@
import cheerio from 'cheerio'
import got from '../libs/got'
import { millify, version, versionColor } from '../libs/utils'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
const OPAM_REPO_URL = 'https://opam.ocaml.org/packages/'
type PackageInfo = {
version: string,
license: string,
downloads: number
}
export default createBadgenHandler({
title: 'OCaml Package Manager',
examples: {
@ -26,14 +19,10 @@ export default createBadgenHandler({
async function handler ({ topic, pkg }: PathArgs) {
const html = await got(pkg, { prefixUrl: OPAM_REPO_URL }).text()
const {
downloads,
license,
version: ver
} = await getPackageInfo(html)
switch (topic) {
case 'v': {
const ver = html.match(/class="package-version">([^<]+)<\//i)?.[1] ?? ''
return {
subject: 'opam',
status: version(ver),
@ -41,6 +30,7 @@ async function handler ({ topic, pkg }: PathArgs) {
}
}
case 'license': {
const license = html.match(/<th>license<\/th>\s*<td>([^<]+)<\//i)?.[1] ?? ''
return {
subject: 'license',
status: license || 'unknown',
@ -48,6 +38,7 @@ async function handler ({ topic, pkg }: PathArgs) {
}
}
case 'dm': {
const downloads = Number(html.match(/<th>statistics<\/th>\s*<td>installed\s*<strong>([^<]+)<\//i)?.[1])
return {
subject: 'downloads',
status: millify(downloads) + '/month',
@ -56,23 +47,3 @@ async function handler ({ topic, pkg }: PathArgs) {
}
}
}
function getPackageInfo(html: string): PackageInfo {
const info: PackageInfo = { version: '', license: '', downloads: NaN }
const $ = cheerio.load(html)
const text = (selector: any) => $(selector).text().trim()
info.version = text($('.package-version').first())
$('.package-info th').filter((_, el) => {
const $el = $(el)
const label = text($el).toLowerCase()
if (label === 'license') {
info.license = text($el.next())
} else if (label === 'statistics') {
info.downloads = parseInt(text($el.next().find('strong')), 0)
}
return !info.license && isNaN(info.downloads)
})
return info
}