-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (116 loc) · 4.37 KB
/
app.js
File metadata and controls
130 lines (116 loc) · 4.37 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Note: This is a example file
*/
'use strict';
const fs = require('fs');
const async = require('async');
const youtubeService = require('./lib/youtube/youtube_service');
const youtubeConfig = {
key: '<Your key>'
};
/**
* Example to fetch a single video
*/
// return youtubeService.init(youtubeConfig)
// .then(function() {
// return youtubeService.getVideoDetail('rshxQD74nTE');
// })
// .then(function(res) {
// console.log(JSON.stringify(res, null, 3));
// });
// var opts = {
// maxResults: 2,
// channelId: 'UCNNxPxH_zIPxvWy5QMFkruA',
// part: 'snippet',
// // playlistId: 'xxx',
// type: 'video',
// pageToken: null,
// };
// const commentParams = {
// maxResults: 1,
// part: 'snippet',
// videoId: 'xxx',
// // pageToken: 'xxx'
// };
// return youtubeService.init(youtubeConfig)
// .then(function() {
// // return youtubeService.getComments(commentParams);
// //listPlaylist(opts);
// return youtubeService.fetchChannel('UCNNxPxH_zIPxvWy5QMFkruA');
// //_getComments('xxx');
// //getVideoDetail('xxx');
// //fetchAllVideosFromChannel('xxx', []);
// //search(opts);
// })
// .then(function(res) {
// // res.forEach(function(item) {
// // console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
// // console.log('Video id: ', item.id.videoId);
// // console.log('title: ', item.snippet.title);
// // console.log('Description: ', item.snippet.description);
// // console.log('publishedAt: ', item.snippet.publishedAt);
// // console.log('thumbnails: ');
// // console.log('\tdefault: ', item.snippet.thumbnails.default.url);
// // console.log('\tmedium: ', item.snippet.thumbnails.medium.url);
// // console.log('\thigh: ', item.snippet.thumbnails.high.url);
// // });
// console.log(JSON.stringify(res, null, 3));
// })
// .catch(function(err) {
// console.error(err);
// });
// // search('', opts, function(err, results, pageinfo) {
// // if(err) return console.log(err);
// //
// // console.log(JSON.stringify(results, null, 3));
// //
// // console.log(pageinfo);
// // });
//
//
/**
* Fetch complete channel. Note: This will be very heavy call, in terms of time and memory
*/
return youtubeService.init(youtubeConfig)
.then(function() {
const channelId = 'UCNNxPxH_zIPxvWy5QMFkruA';
return youtubeService.fetchAllVideosFromChannel(channelId)
})
.then(function(allVideos) {
//got list of all videos
//Note, this can be a huge list, will consume lot of memory
//Think of putting event notification per video
//get details of all videos
return new Promise(function (resolve, reject) {
async.eachLimit(allVideos, 1,
function (video, callback) {
return youtubeService.getVideoDetail(video.id.videoId)
.then(function (videoDetail) {
return youtubeService.getAllCommentsForVideo(video.id.videoId)
.then(function(comments) {
videoDetail.comments = comments;
return videoDetail;
});
}.bind(this))
.then(function (videoDetail) {
//write in file
//result folder must exist here
const video_filepath = 'result/' + videoDetail.items[0].id + '.json';
console.log('Write in file: ', video_filepath);
fs.writeFileSync(video_filepath, JSON.stringify(videoDetail));
callback();
})
.catch(function (err) {
logger.log('error', err);
callback(err);
});
}.bind(this), function (err) {
if (err) {
reject(err);
} else {
resolve();
}
}
);
}.bind(this));
}.bind(this));