feat(plugins/community/poopmap): add poopmap plugin support (#675)

This commit is contained in:
Matthew Stead
2021-11-27 00:53:36 +02:00
committed by GitHub
parent e4ff40453a
commit 2d144f4ba9
7 changed files with 183 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
//Setup
export default async function({q, imports, data, account}, {enabled = false, token = ""} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.poopmap))
return null
if (!token) return {poops:[], days:7}
const {days} = imports.metadata.plugins.poopmap.inputs({data, account, q})
const {data:{poops}} = await imports.axios.get(`https://api.poopmap.net/api/v1/public_links/${token}`)
const filteredPoops = poops.filter(poop => {
const createdAt = new Date(poop.created_at)
poop.created_at = createdAt.toString()
return createdAt > new Date().getTime() - days * 24 * 60 * 60 * 1000
})
const hours = {max:0}
for (let i = 0; i < filteredPoops.length; i++) {
const poop = filteredPoops[i]
const hour = new Date(poop.created_at).getHours()
hours[hour] = (hours[hour] ?? 0) + 1
hours.max = Math.max(hours[hour], hours.max)
}
//Results
return {poops:hours, days}
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}