-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube.js
295 lines (268 loc) · 12.1 KB
/
youtube.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import _ from 'lodash';
import fs from 'fs';
import express from 'express';
import opn from 'opn';
import sh from 'shelljs';
import queue from 'queue';
import Google from 'googleapis';
const {OAuth2} = Google.auth;
import {fileExists} from './utils.js';
import credentials from "./secret/credentials.json";
import {
products,
projectTitle, playlistDescription, videoTitle, videoDescription, videoTags,
outPath, origFPS, finalFPS, speed
} from './config.js';
import {parseTimeStr, niceDate} from './utils.js';
// shared Youtube singleton
const Youtube = Google.youtube("v3");
makePlaylists();
function makePlaylists() {
//const makePlaylistTitle = (product) => `${projectTitle}: ${product.title}`;
//const makePlaylistDescription = product => `${playlistDescription.replace("<TITLE>", product.title)}`;
authenticateYoutube(credentials, './secret/token.json', (oauth2Client) => {
const makeTitle = (product) => `${projectTitle}: ${product.title}`;
const makeDescription = product => `${playlistDescription.replace("<TITLE>", product.title)}`;
ensurePlaylistsForProducts(products, () => {
uploadVideosForProducts(products);
}, {makeTitle, makeDescription});
});
}
function ensurePlaylistsForProducts(products, callback, {makeTitle, makeDescription=()=>''}={}) {
const q = queue({concurrency: 1});
if(!makeTitle) makeTitle = (product) => product.title;
// first get playlists from Youtube API
listPlaylists((err, data) => {
catchErr(err);
// and find their associated product IDs (via the 'id:x' tag)
const playlistsByProductId = _.omit(_.indexBy(data.items, getIdFromTag), null);
// for each expected product, ensure a playlist exists and has the correct information
products.forEach(product => {
const productPlaylist = playlistsByProductId[product.id];
const updatedPlaylist = {
title: makeTitle(product),
description: makeDescription(product),
tags: [`id:${product.id}`].concat(product.tags || [])
};
if(!productPlaylist) { // youtube playlist doesn't exist, create it
q.push(function(next) {
addPlaylist(updatedPlaylist, (err, data) => {
catchErr(err);
console.log('created playlist', updatedPlaylist);
next();
});
});
} else if(!_.every(updatedPlaylist, (val, key) => _.isEqual(productPlaylist.snippet[key], val))) {
// youtube playlist doesn't match expected title/description/tags, update it
q.push(function(next) {
updatePlaylist(productPlaylist.id, updatedPlaylist, (err, data) => {
catchErr(err);
console.log('updated playlist', updatedPlaylist);
next();
})
});
} else console.log(`playlist ${updatedPlaylist.title} is OK.`);
});
q.start(callback);
});
}
function uploadVideosForProducts(products, callback=_.noop) {
const q = queue({concurrency: 1});
// get playlists from Youtube API
listPlaylists((err, data) => {
catchErr(err);
// and find their associated product IDs (via the 'id:x' tag)
const playlistsByProductId = _.omit(_.indexBy(data.items, getIdFromTag), null);
const sessionDirs = sh.ls(outPath);
// then ensure videos exist for each product
products.forEach(product => {
const productPlaylist = playlistsByProductId[product.id];
if(!productPlaylist) return; // assume playlists exist
q.push(function(next) {
ensureVideosForProduct(product, productPlaylist, () => {
console.log(`all videos uploaded for ${product.title}`);
next();
});
});
});
q.start(callback);
});
}
function ensureVideosForProduct(product, playlist, callback) {
const q = queue({concurrency: 1});
// ensures that all videos which exist in outPath for this product are uploaded to Youtube with the correct info
// get the list of items in the playlist from API
listPlaylistItems(playlist.id, (err, data) => {
catchErr(err);
const playlistItems = data.items;
console.log(`got ${playlistItems.length} items in playlist`);
const videoIds = _.pluck(playlistItems, 'snippet.resourceId.videoId');
console.log(videoIds.length)
// have to get video information with videos.list because playlistItems doesn't return tags
getVideoById(videoIds, (err, data) => {
catchErr(err);
console.log(`got info for ${data.items.length} videos`);
// video id:x tags contain the sessionId, ie. directory name, eg. '20150920210000-20150921123000'
const videosBySessionId = _.omit(_.indexBy(data.items, getIdFromTag), null);
const sessionDirs = sh.ls(outPath);
sessionDirs.forEach(sessionId => {
const videoFileName = `interpolated-${origFPS}-${finalFPS}fps-${speed}x.mp4`;
const videoPath = `${outPath}/${sessionId}/${product.crop}/video/${videoFileName}`;
if(fileExists(videoPath) && !videosBySessionId[sessionId]) {
// file exists for this product, and video is not in playlist, so upload it
q.push(function(next) {
const dateStr = parseTimeStr(sessionId.split('-')[0]).format('MMM D, YYYY');
const videoInfo = {projectTitle, product, date: dateStr};
const snippet = {
title: _.template(videoTitle)(videoInfo),
description: _.template(videoDescription)(videoInfo),
tags: (product.tags || []).concat(videoTags || []).concat(`id:${sessionId}`)
};
// upload the video
console.log('uploading', snippet.title, 'from', videoPath);
uploadVideo(videoPath, snippet, (err, data) => {
catchErr(err);
console.log('uploaded successfully!');
// then add it to the correct playlist
addVideoToPlaylist(data.id, playlist.id, (err, data) => {
catchErr(err);
console.log(`added video ${data.snippet.title} to playlist ${playlist.snippet.title}`);
next();
}); // todo figure out position
})
});
} else if(videosBySessionId[sessionId]) {
// todo ensure correct information
console.log('video already exists for', sessionId);
}
});
q.start(callback);
});
});
}
function getIdFromTag(playlist) {
const idRegex = /^id:(.+)/;
const idTag = _.find(playlist.snippet.tags || [], tag => tag.match(idRegex));
return idTag ? idTag.match(idRegex)[1] : null;
}
function catchErr(err) {
if(err) { console.log(err); throw err; }
}
function listPlaylists(callback, {part='id,contentDetails,snippet,status'}={}) {
callback = callback || catchErr;
// todo handle > 50 results with pagination
Youtube.playlists.list({part, mine: true, maxResults: 50}, callback);
}
function listPlaylistItems(playlistId, callback,
{part='id,contentDetails,snippet,status', pageToken=undefined}={}
) {
console.log(part, pageToken)
callback = callback || catchErr;
// todo handle > 50 results with pagination
// Youtube.playlistItems.list({part, playlistId, maxResults: 50}, callback);
const options = {part, playlistId, pageToken, maxResults: 50};
Youtube.playlistItems.list(options, (err, data) => {
if(data.nextPageToken) {
catchErr(err);
listPlaylistItems(playlistId, (nextErr, nextData) => {
catchErr(nextErr);
nextData.items = data.items.concat(nextData.items);
callback(nextErr, nextData);
}, {part, pageToken: data.nextPageToken});
} else callback(err, data);
});
}
function getVideoById(videoIds, callback, {part='id,contentDetails,snippet,status'}={}) {
videoIds = _.isArray(videoIds) ? videoIds : [videoIds];
callback = callback || catchErr;
const limit = 50
// handle > 50 results with pagination
Youtube.videos.list({part, id: _.take(videoIds, limit).join(','), maxResults: limit}, (err, data) => {
if(videoIds.length <= limit) callback(err, data);
else {
catchErr(err);
getVideoById(_.drop(videoIds, limit), (nextErr, nextData) => {
catchErr(err);
nextData.items = data.items.concat(nextData.items);
callback(nextErr, nextData)
}, {part});
}
});
}
function updatePlaylist(id, snippet, callback, {privacyStatus='public'}={}) {
callback = callback || catchErr;
Youtube.playlists.update({
part: 'snippet,status',
resource: {id, snippet, status: {privacyStatus}}
}, callback);
}
function addPlaylist(snippet, callback, {privacyStatus='public'}={}) {
callback = callback || catchErr;
// insert new playlist via Youtube api
Youtube.playlists.insert({
part: 'snippet,status',
resource: {snippet, status: {privacyStatus}}
}, callback);
}
function addVideoToPlaylist(videoId, playlistId, callback, {position}={}) {
callback = callback || catchErr;
// add an existing video to an existing playlist via Youtube api
Youtube.playlistItems.insert({
part: 'snippet,id',
resource: {snippet: {resourceId: {kind: 'youtube#video', videoId}, playlistId, position: 0}}
}, callback);
}
function uploadVideo(videoPath, snippet, callback, {privacyStatus='public'}={}) {
// Upload video via the Youtube API
callback = callback || catchErr;
Youtube.videos.insert({
part: 'snippet,status',
resource: {snippet, status: {privacyStatus}},
media: {body: fs.createReadStream(videoPath)}
}, callback);
}
function authenticateYoutube(credentials, tokenPath, callback) {
const {client_id, client_secret, redirect_uris} = credentials.web;
const oauth2Client = new OAuth2(client_id, client_secret, redirect_uris[0]);
Google.options({auth: oauth2Client});
if(fileExists(tokenPath)) {
console.log('youtube token already exists');
const tokens = JSON.parse(sh.cat(tokenPath));
oauth2Client.setCredentials(tokens);
// get playlists to check if token is really valid or not
listPlaylists((err, data) => {
if(err) {
console.log('youtube token invalid, getting a new one');
getYoutubeTokens(oauth2Client, tokenPath, callback);
} else {
console.log('youtube token is still valid');
callback(oauth2Client);
}
}, {part: 'id'});
} else {
getYoutubeTokens(oauth2Client, tokenPath, callback);
}
}
function getYoutubeTokens(oauth2Client, tokenPath, callback) {
// open the consent page to get token
opn(oauth2Client.generateAuthUrl({
access_type: "offline",
scope: ["https://www.googleapis.com/auth/youtube"]
}));
// create a server to listen for response after user gives consent
const app = express();
const server = app.listen(5000);
app.get('/oauth2callback', (req, res) => {
console.log('trying with code', req.query.code);
oauth2Client.getToken(req.query.code, function(err, token) {
if (err) { res.send('error :( ' + err); throw err; }
oauth2Client.setCredentials(token);
fs.writeFile(tokenPath, JSON.stringify(token), err => {
console.log('saved new google token');
res.send("authorized!");
server.close();
callback(oauth2Client);
});
});
});
}