diff --git a/api/liberapay.ts b/api/liberapay.ts new file mode 100644 index 0000000..1eab416 --- /dev/null +++ b/api/liberapay.ts @@ -0,0 +1,65 @@ +import got from '../libs/got' +import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler' + +export default createBadgenHandler({ + title: 'Liberapay', + examples: { + '/liberapay/gives/aurelienpierre': 'giving', + '/liberapay/receives/GIMP': 'receiving', + '/liberapay/patrons/microG': 'patrons count', + '/liberapay/goal/Changaco': 'goal progress', + }, + handlers: { + '/liberapay/:topic/:slug': handler + } +}) + +// https://github.com/badges/shields/blob/master/services/liberapay + +async function handler ({ topic, slug }: PathArgs) { + const endpoint = `https://liberapay.com/${slug}/public.json` + + const details = await got(endpoint).json() + const receivingLocaleOptions = { + style: 'currency', + currency: details.giving.currency + } + const givingLocaleOptions = { + style: 'currency', + currency: details.receiving.currency + } + + let goal = 'not set' + if (details.goal) { + const goalAmount = parseFloat(details.goal.amount) + const receivesAmount = parseFloat(details.receiving.amount) + goal = `${Math.round((receivesAmount / goalAmount) * 100)}%` + } + + switch (topic) { + case 'gives': + return { + subject: 'gives', + status: `${parseFloat(details.giving.amount).toLocaleString('en-US', givingLocaleOptions)}/week`, + color: 'yellow' + } + case 'receives': + return { + subject: 'receives', + status: `${parseFloat(details.receiving.amount).toLocaleString('en-US', receivingLocaleOptions)}/week`, + color: 'yellow' + } + case 'patrons': + return { + subject: 'patrons', + status: details.npatrons, + color: 'yellow' + } + case 'goal': + return { + subject: 'goal progress', + status: goal, + color: goal !== 'not set' ? 'yellow' : 'grey' + } + } +} diff --git a/libs/badge-list.ts b/libs/badge-list.ts index 10eae00..1233134 100644 --- a/libs/badge-list.ts +++ b/libs/badge-list.ts @@ -60,6 +60,7 @@ export const liveBadgeList = [ 'runkit', 'https', 'jenkins', + 'liberapay' ] export async function loadBadgeMeta () {