mirror of
https://github.com/KevinMidboe/waka-box.git
synced 2025-10-29 18:00:21 +00:00
60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
require("dotenv").config();
|
|
const { WakaTimeClient, RANGE } = require("wakatime-client");
|
|
const Octokit = require("@octokit/rest");
|
|
|
|
const {
|
|
GIST_ID: gistId,
|
|
GITHUB_TOKEN: githubToken,
|
|
WAKATIME_API_KEY: wakatimeApiKey
|
|
} = process.env;
|
|
|
|
const wakatime = new WakaTimeClient(wakatimeApiKey);
|
|
|
|
const octokit = new Octokit({
|
|
auth: `token ${githubToken}`
|
|
});
|
|
|
|
async function main() {
|
|
const stats = await wakatime.getMyStats({ range: RANGE.LAST_7_DAYS });
|
|
await updateGist(stats);
|
|
}
|
|
|
|
async function updateGist(stats) {
|
|
let gist;
|
|
try {
|
|
gist = await octokit.gists.get({ gist_id: gistId });
|
|
} catch (error) {
|
|
console.error(`Unable to get gist\n${error}`);
|
|
}
|
|
// Get original filename to update that same file
|
|
const filename = Object.keys(gist.data.files)[0];
|
|
|
|
const lines = [];
|
|
for (let i = 0; i < 5; i++) {
|
|
const data = stats.data.languages[i];
|
|
const name = data.name.padEnd(10);
|
|
const percent = data.percent;
|
|
const time = data.text;
|
|
|
|
lines.push(`${name} - ${time}`);
|
|
}
|
|
|
|
try {
|
|
await octokit.gists.update({
|
|
gist_id: gistId,
|
|
files: {
|
|
[filename]: {
|
|
filename: `📊 Weekly development breakdown`,
|
|
content: lines.join("\n")
|
|
}
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error(`Unable to update gist\n${error}`);
|
|
}
|
|
}
|
|
|
|
(async () => {
|
|
await main();
|
|
})();
|