code::stats

node v14.20.1
version: 2.0.0
endpointsharetweet
An endpoint availible for getting codestats.net data for badgen.net
/* * ENDPOINT USAGE * - https://runkit.com/tagnumelite/code-stats/:username/[:language] * * EXAMPLE * API * - https://runkit.com/tagnumelite/code-stats/TagnumElite/TypeScript * BADGE * - https://badgen.net/runkit/tagnumelite/code-stats/TagnumElite/TypeScript */ const axios = require('axios'); const { send } = require('micro'); const LEVEL_FACTOR = 0.025; const client = axios.create({ baseURL: 'https://codestats.net/api/' }); exports.endpoint = async function (request, response) { const args = request.url.split('/').filter(Boolean); if (args.length < 1) { return send(response, 400, { subject: 'codestats', status: 'missing username', color: 'grey', }); } else if (args.length === 1) { args[1] = ''; } try { send(response, 200, await getStats(args[0], args[1])); } catch (err) { send(response, err.response ? err.response.status : 400, { subject: 'codestats', status: 'invalid', color: 'gray', }); } }; async function getStats(username, language = '') { const { error, languages, total_xp, new_xp } = await client .get(`/users/${username}`) .then((res) => res.data); if (error !== undefined) { return { subject: 'codestats', status: error, color: 'red', }; } if (language !== '') { var xps = 0; if (language in languages) { xps = languages[language]['xps']; } return { subject: language, status: `${xps}xp`, color: 'green', }; } else { return { subject: 'codestats', status: `${total_xp}xp`, color: 'green', }; } }
Loading…

no comments

    sign in to comment