Update gist with WakaTime stats

This commit is contained in:
Matan Kushner
2019-03-17 14:52:15 -04:00
parent 89180b0baf
commit 0f2ee0ac31
3 changed files with 15 additions and 19 deletions

View File

@@ -1,7 +1,6 @@
require("dotenv").config(); require("dotenv").config();
const { WakaTimeClient, RANGE } = require("wakatime-client"); const { WakaTimeClient, RANGE } = require("wakatime-client");
const Octokit = require("@octokit/rest"); const Octokit = require("@octokit/rest");
const wordwrap = require("wordwrap");
const { const {
GIST_ID: gistId, GIST_ID: gistId,
@@ -17,13 +16,10 @@ const octokit = new Octokit({
async function main() { async function main() {
const stats = await wakatime.getMyStats({ range: RANGE.LAST_7_DAYS }); const stats = await wakatime.getMyStats({ range: RANGE.LAST_7_DAYS });
console.log(stats.data.languages); await updateGist(stats);
// await updateGist(tweet);
} }
async function updateGist(tweet) { async function updateGist(stats) {
const wrap = wordwrap(46);
let gist; let gist;
try { try {
gist = await octokit.gists.get({ gist_id: gistId }); gist = await octokit.gists.get({ gist_id: gistId });
@@ -32,18 +28,24 @@ async function updateGist(tweet) {
} }
// Get original filename to update that same file // Get original filename to update that same file
const filename = Object.keys(gist.data.files)[0]; const filename = Object.keys(gist.data.files)[0];
const parsedDate = new Date(tweet.created_at);
const timeAgo = formatDistanceStrict(parsedDate, new Date()); 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 { try {
await octokit.gists.update({ await octokit.gists.update({
gist_id: gistId, gist_id: gistId,
files: { files: {
[filename]: { [filename]: {
filename: `@${twitterHandle} - ${timeAgo} ago | ❤ ${ filename: `📊 Weekly development breakdown`,
tweet.favorite_count content: lines.join("\n")
} | 🔁 ${tweet.retweet_count}`,
content: wrap(tweet.text)
} }
} }
}); });

5
package-lock.json generated
View File

@@ -919,11 +919,6 @@
"execa": "^0.10.0" "execa": "^0.10.0"
} }
}, },
"wordwrap": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
"integrity": "sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus="
},
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",

View File

@@ -8,8 +8,7 @@
"@octokit/rest": "^16.18.1", "@octokit/rest": "^16.18.1",
"axios": "^0.18.0", "axios": "^0.18.0",
"dotenv": "^7.0.0", "dotenv": "^7.0.0",
"wakatime-client": "^2.0.2", "wakatime-client": "^2.0.2"
"wordwrap": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"husky": "^1.3.1", "husky": "^1.3.1",