feat(ctan): add rating badge

pull/458/head
Dario Vladović 2020-12-17 00:09:07 +00:00 zatwierdzone przez GitHub
rodzic a37cceb2e5
commit 3e8a55a24d
1 zmienionych plików z 23 dodań i 4 usunięć

Wyświetl plik

@ -1,5 +1,5 @@
import got from '../libs/got'
import { version, versionColor } from '../libs/utils'
import { millify, version, versionColor } from '../libs/utils'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
const CTAN_API_URL = 'https://ctan.org/json/2.0/'
@ -10,10 +10,11 @@ export default createBadgenHandler({
title: 'CTAN',
examples: {
'/ctan/v/latexindent': 'version',
'/ctan/license/latexdiff': 'license'
'/ctan/license/latexdiff': 'license',
'/ctan/rating/pgf-pie': 'rating'
},
handlers: {
'/ctan/:topic<v|license>/:pkg': handler,
'/ctan/:topic<v|license|rating>/:pkg': handler,
}
})
@ -22,7 +23,7 @@ async function handler ({ topic, pkg }: PathArgs) {
license,
version: versionInfo,
} = await client.get(`pkg/${pkg}`).json<any>()
const { number: ver } = versionInfo;
const { number: ver } = versionInfo
switch (topic) {
case 'v':
@ -37,5 +38,23 @@ async function handler ({ topic, pkg }: PathArgs) {
status: license || 'unknown',
color: 'green'
}
case 'rating': {
const url = 'https://ctan.org/vote/ajaxSummary'
const searchParams = { pkg }
const html = await got.get(url, { searchParams }).text()
const rating = Number(html.match(/<span>.*?([\d.]+)\s/i)?.[1])
if (Number.isNaN(rating)) {
return {
subject: 'rating',
status: 'invalid',
color: 'grey'
}
}
return {
subject: 'rating',
status: millify(rating),
color: 'green'
}
}
}
}