From 3e8a55a24d8f34d04c3e0f612751c0223792ad2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Thu, 17 Dec 2020 00:09:07 +0000 Subject: [PATCH] feat(ctan): add rating badge --- api/ctan.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/api/ctan.ts b/api/ctan.ts index 2f6f06e..c00503b 100644 --- a/api/ctan.ts +++ b/api/ctan.ts @@ -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/:pkg': handler, + '/ctan/:topic/:pkg': handler, } }) @@ -22,7 +23,7 @@ async function handler ({ topic, pkg }: PathArgs) { license, version: versionInfo, } = await client.get(`pkg/${pkg}`).json() - 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(/.*?([\d.]+)\s/i)?.[1]) + if (Number.isNaN(rating)) { + return { + subject: 'rating', + status: 'invalid', + color: 'grey' + } + } + return { + subject: 'rating', + status: millify(rating), + color: 'green' + } + } } }