2020-12-16 12:22:02 +00:00
|
|
|
import got from '../libs/got'
|
2020-12-19 04:01:44 +00:00
|
|
|
import { stars, version, versionColor } from '../libs/utils'
|
2020-12-16 12:22:02 +00:00
|
|
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
|
|
|
|
|
|
|
const CTAN_API_URL = 'https://ctan.org/json/2.0/'
|
|
|
|
|
|
|
|
const client = got.extend({ prefixUrl: CTAN_API_URL, timeout: 3500 })
|
|
|
|
|
|
|
|
export default createBadgenHandler({
|
|
|
|
title: 'CTAN',
|
|
|
|
examples: {
|
|
|
|
'/ctan/v/latexindent': 'version',
|
2020-12-19 04:01:44 +00:00
|
|
|
'/ctan/license/latexdiff': 'license',
|
|
|
|
'/ctan/rating/pgf-pie': 'rating',
|
|
|
|
'/ctan/stars/pgf-pie': 'stars'
|
2020-12-16 12:22:02 +00:00
|
|
|
},
|
|
|
|
handlers: {
|
2020-12-19 04:01:44 +00:00
|
|
|
'/ctan/:topic<v|license>/:pkg': apiHandler,
|
|
|
|
'/ctan/:topic<rating|stars>/:pkg': webHandler
|
2020-12-16 12:22:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-19 04:01:44 +00:00
|
|
|
async function apiHandler ({ topic, pkg }: PathArgs) {
|
2020-12-16 12:22:02 +00:00
|
|
|
const {
|
|
|
|
license,
|
|
|
|
version: versionInfo,
|
|
|
|
} = await client.get(`pkg/${pkg}`).json<any>()
|
2020-12-19 04:01:44 +00:00
|
|
|
const { number: ver } = versionInfo
|
2020-12-16 12:22:02 +00:00
|
|
|
|
|
|
|
switch (topic) {
|
|
|
|
case 'v':
|
|
|
|
return {
|
|
|
|
subject: 'ctan',
|
|
|
|
status: version(ver),
|
|
|
|
color: versionColor(ver)
|
|
|
|
}
|
|
|
|
case 'license':
|
|
|
|
return {
|
|
|
|
subject: 'license',
|
|
|
|
status: license || 'unknown',
|
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 04:01:44 +00:00
|
|
|
|
|
|
|
async function webHandler ({ topic, pkg }: PathArgs) {
|
|
|
|
const url = 'https://ctan.org/vote/ajaxSummary'
|
|
|
|
const searchParams = { pkg }
|
|
|
|
const html = await got.get(url, { searchParams }).text()
|
2021-02-14 02:03:26 +00:00
|
|
|
const rating = Number(html.match(/<span>[^<]*?([\d.]+)\s/i)?.[1])
|
2020-12-19 04:01:44 +00:00
|
|
|
|
|
|
|
switch (topic) {
|
|
|
|
case 'rating':
|
|
|
|
return {
|
|
|
|
subject: 'rating',
|
|
|
|
status: `${rating.toFixed(2)}/5`,
|
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
case 'stars':
|
|
|
|
return {
|
|
|
|
subject: 'stars',
|
|
|
|
status: stars(rating),
|
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|