-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 2.2 KB
/
index.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
const schedule = require("node-schedule");
const tracksHandler = require("./lib/tracks");
const server = require("./lib/token");
const dbAccess = require("./lib/database/dataAccess");
const dbInsert = require("./lib/database/dataInsertion");
const wrapped = require("./lib/wrapped");
async function main() {
server.start(5000);
await updateAllUsersTracks();
// await wrappedThing();
let hourlyTask = schedule.scheduleJob(
{ minute: 0 },
updateAllUsersTracks
);
let monthlyTask = schedule.scheduleJob(
{ day: 1 },
wrappedThing
);
}
async function updateAllUsersTracks() {
console.log("Scheduled task 'updateAllUserTracks' now running...");
let total = 0;
let users = await dbAccess.getAllUsers();
for (const user of users) {
console.log(user.display_name);
let token = await server.getUserToken(user);
const recentTracks = await tracksHandler.getRecentTracks(
token.access_token,
user.played_at
);
if (!recentTracks) {
continue;
}
await dbInsert.handleRecentTracks(recentTracks.items, user);
total += recentTracks.items.length;
}
console.log("Scheduled task 'updateAllUserTracks' completed. " +
total + " total records added");
}
function millisecondsToHoursAndMinutes(milliseconds) {
// Calculate hours, minutes, and seconds
const hours = Math.floor(milliseconds / (1000 * 60 * 60));
const minutes = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
return `${hours} hours and ${minutes} minutes`
}
async function wrappedThing() {
const wrappedObject = await wrapped.handleWrapped();
console.log(wrappedObject.title);
console.log(wrappedObject.global_stats.total_count + " plays");
console.log(millisecondsToHoursAndMinutes(wrappedObject.global_stats.total_time_listened));
for (const user of wrappedObject.user_stats) {
console.log(user.display_name);
console.log(user.total_count + " plays");
console.log(millisecondsToHoursAndMinutes(user.total_time_listened));
}
}
main();
// updateAllUsersTracks();