badgen.net/libs/live-fns/amo.js

58 wiersze
1.4 KiB
JavaScript
Czysty Zwykły widok Historia

2018-07-26 13:53:45 +00:00
const axios = require('../axios.js')
const xml2js = require('xml2js')
const millify = require('millify')
const round = require('../utils/round.js')
const stars = require('../utils/stars.js')
2018-07-30 06:41:01 +00:00
const semColor = require('../utils/sem-color.js')
2018-07-26 13:53:45 +00:00
2018-07-30 06:50:45 +00:00
module.exports = async function (topic, ...args) {
2018-07-26 13:53:45 +00:00
const endpoint = `https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/${args[0]}`
const xml = await axios.get(endpoint).then(res => res.data)
2018-07-30 06:50:45 +00:00
const { addon } = await new Promise((resolve, reject) => {
2018-07-26 13:53:45 +00:00
xml2js.parseString(xml, {
trim: true,
explicitArray: false
}, (err, res) => err ? reject(err) : resolve(res))
})
2018-07-30 06:50:45 +00:00
switch (topic) {
case 'v':
return {
subject: 'mozilla add-on',
status: 'v' + addon.version,
color: semColor(addon.version)
}
case 'users':
return {
subject: 'users',
status: millify(parseInt(addon.daily_users)),
color: 'green'
}
case 'rating':
return {
subject: 'rating',
status: `${round(addon.rating, 2)}/5`,
color: 'green'
}
case 'stars':
return {
subject: 'stars',
status: stars(addon.rating),
color: 'green'
}
case 'reviews':
return {
subject: 'reviews',
status: addon.reviews.$.num,
color: 'green'
}
default:
return {
subject: 'mozilla add-on',
status: 'unknown',
color: 'grey'
}
}
2018-07-26 13:53:45 +00:00
}