-
Notifications
You must be signed in to change notification settings - Fork 2
/
wakatime.js
95 lines (87 loc) · 2.88 KB
/
wakatime.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const fs = require("fs");
const https = require("https");
const fileName = `wakatime.md`;
const str1 = fs.readFileSync(fileName, "utf8");
function apiKeyBase64(apiKey) {
const apiKeyBuffer = Buffer.from(apiKey);
return apiKeyBuffer.toString("base64");
}
function request(apiKey, path) {
const options = {
port: 443,
hostname: "wakatime.com",
path: `/api/v1${path}`,
method: "GET",
headers: {
Authorization: `Basic ${apiKeyBase64(apiKey)}`,
},
};
return new Promise((resolve, reject) => {
let responseBody = "";
const req = https.request(options, res => {
res.on("data", data => {
responseBody += data;
});
res.on("end", () => {
try {
const bodyJSON = JSON.parse(responseBody);
resolve(bodyJSON);
} catch (e) {
reject(e);
}
});
});
req.end();
});
}
const requestObj = apiKey => ({
last7Days: () => request(apiKey, "/users/current/stats/last_7_days"),
last30Days: () => request(apiKey, "/users/current/stats/last_30_days"),
last6Months: () => request(apiKey, "/users/current/stats/last_6_months"),
lastYear: () => request(apiKey, "/users/current/stats/last_year"),
summaries: (start, end) => request(apiKey, `/users/current/summaries?start=${start}&end=${end}`),
currentUser: () => request(apiKey, "/users/current"),
});
const keys = [
{
key: "362feac5-3d8b-4b88-95f5-db0d6cf92d18",
name: "veveue",
},
];
function getEndDate() {
const month = new Date().getMonth() + 1;
const day = new Date().getDate();
const year = new Date().getFullYear();
function makeZero(value) {
return value > 9 ? value : `0${value}`;
}
return `${year}-${makeZero(month)}-${makeZero(day)}`;
}
fs.writeFile(
fileName,
// eslint-disable-next-line max-len
`${str1} \n\n\n\n | --------------------------------- 统计时间 ${getEndDate()} --------------------------------- \n\n | 姓名 | 时长 | \n | ---------- | ------------- | `,
err => {
if (err) {
return console.log(err);
}
}
);
async function main() {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
// eslint-disable-next-line no-await-in-loop
const data = await requestObj(key.key).summaries(getEndDate(),getEndDate());
const str = fs.readFileSync(fileName, "utf8");
fs.writeFile(
fileName,
`${str} \n | ${key.name} | ${(data.data && data.data[0].grand_total.text.replace("hrs", "时").replace("mins", "分")) || 0} |`,
err => {
if (err) {
return console.log(err);
}
}
);
}
}
main().catch(console.error);