This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-historical-youtube-data.js
62 lines (50 loc) · 1.77 KB
/
generate-historical-youtube-data.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
const Realm = require("realm-web");
/**
* Use this script to generate historical YouTube stats
*/
// The Realm app id
const id = "";
const config = {
id,
};
const app = new Realm.App(config);
// For simplicity, I'm enabling anonymous auth for generating the data
// I'm also disabling Private mode for get_previous_youtube_video_stats
async function main() {
const user = await loginAnonymous();
/**
* Set the year here. Also adjust the number of days in February
*/
let year = 2014;
// Going to await these to avoid YouTube api quota limits
await getStatsForMonth(user, year, 1, 31);
await getStatsForMonth(user, year, 2, 28); //Adjust each year!
await getStatsForMonth(user, year, 3, 31);
await getStatsForMonth(user, year, 4, 29);
await getStatsForMonth(user, year, 5, 31);
await getStatsForMonth(user, year, 6, 30);
await getStatsForMonth(user, year, 7, 31);
await getStatsForMonth(user, year, 8, 31);
await getStatsForMonth(user, year, 9, 30);
await getStatsForMonth(user, year, 10, 31);
await getStatsForMonth(user, year, 11, 30);
await getStatsForMonth(user, year, 12, 31);
}
main().catch(console.error);
async function getStatsForMonth(user, year, month, numberOfDaysInMonth) {
let results = await user.functions.get_previous_youtube_video_stats(year, month, 1, 15);
console.log(results);
results = await user.functions.get_previous_youtube_video_stats(year, month, 16, numberOfDaysInMonth);
console.log(results);
}
async function loginAnonymous() {
// Create an anonymous credential
const credentials = Realm.Credentials.anonymous();
try {
// Authenticate the user
const user = await app.logIn(credentials);
return user;
} catch(err) {
console.error("Failed to log in", err);
}
}