-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (62 loc) · 2.08 KB
/
script.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
const axios = require("axios");
const { writeFileSync } = require("fs");
access_token = process.argv[2];
const playlist_id = process.argv[3];
axios
.get(
`https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet%2C%20contentDetails&playlistId=${playlist_id}&maxResults=500&key=${access_token}`
)
.then((response) => {
let playlist_data = response.data;
results = [];
new Promise((resolve) => {
let i = 0;
const n = playlist_data.items.length;
console.log("Number of videos in Playlist:", n);
playlist_data.items.forEach((video) => {
let cur_index =
results.push([
video.snippet.title,
`https://www.youtube.com/watch?v=${video.contentDetails.videoId}`,
]) - 1;
// get this video's duration
axios
.get(
`https://youtube.googleapis.com/youtube/v3/videos?part=contentDetails&id=${
video.contentDetails.videoId
}&key=${encodeURIComponent(access_token)}`
)
.then((response) => {
console.log(
`Information of Video ${i + 1} received from YouTube API.`
);
if (response.data.pageInfo.totalResults > 0)
results[cur_index].push(
response.data.items[0].contentDetails.duration
);
})
.catch((err) => {
console.log(err);
})
.finally(() => {
i++;
if (i == n) resolve();
});
});
}).then(() => {
console.log("Data completely received from YouTube API.");
let csvContent = results
.map((item) => {
// Here item refers to a row in that 2D array
var row = item;
// Now join the elements of row with "," using join function
return '"' + row.join('","') + '"';
}) // At this point we have an array of strings
.join("\n");
writeFileSync(`result-${playlist_id}.csv`, csvContent);
console.log("CSV file created");
});
})
.catch((err) => {
console.log(err);
});