-
Notifications
You must be signed in to change notification settings - Fork 1
/
wakatime-activity.js
43 lines (34 loc) · 1.31 KB
/
wakatime-activity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import axios from 'axios'
import { adjust, append, evolve, pathOr, pipe, reduce } from 'ramda'
const user = process.env.WAKATIME_USER || 'Cuchi'
const interval = Number(process.env.WAKATIME_INTERVAL || 7200)
const url = `https://wakatime.com/api/v1/users/@${user}/stats/last_7_days`
const placeHolderChartData = { datasets: [{ data: [] }], labels: [] }
let activity = placeHolderChartData
async function updateActivity() {
const toChartData = pipe(
pathOr([], ['data', 'data', 'languages']),
reduce(
(chartData, { name, percent }) =>
evolve({
datasets: adjust(0, evolve({ data: append(percent) })),
labels: append(name)
}, chartData),
placeHolderChartData))
try {
const response = await axios.get(url)
if (response.status !== 200) {
throw new Error(response.data.message)
}
activity = toChartData(response)
} catch (err) {
console.error(`Error retrieving WakaTime data: ${err.message}`)
}
}
setInterval(updateActivity, interval * 1000)
const firstRequest = updateActivity()
export default async function (req, res) {
await firstRequest
res.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(activity))
}