-
Notifications
You must be signed in to change notification settings - Fork 4
/
ytupload.js
179 lines (159 loc) · 5.72 KB
/
ytupload.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
/*
* This code consists of code of the Google API snippet:
* https://developers.google.com/youtube/v3/docs/videos/insert
*
* Some of this code was changed by zekro.
*/
var fs = require('fs')
var readline = require('readline')
var util = require('util')
var google = require('googleapis')
var googleAuth = require('google-auth-library')
var EventEmitter = require('events')
const SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
const CREDS = 'client_secrets.json'
var TOKEN_PATH = 'google-apis-nodejs-quickstart.json'
class Emitter extends EventEmitter {}
var event = new Emitter()
function createResource(properties) {
var resource = {};
var normalizedProps = properties;
for (var p in properties) {
var value = properties[p];
if (p && p.substr(-2, 2) == '[]') {
var adjustedName = p.replace('[]', '');
if (value) {
normalizedProps[adjustedName] = value.split(',');
}
delete normalizedProps[p];
}
}
for (var p in normalizedProps) {
// Leave properties that don't have values out of inserted resource.
if (normalizedProps.hasOwnProperty(p) && normalizedProps[p]) {
var propArray = p.split('.');
var ref = resource;
for (var pa = 0; pa < propArray.length; pa++) {
var key = propArray[pa];
if (pa == propArray.length - 1) {
ref[key] = normalizedProps[p];
} else {
ref = ref[key] = ref[key] || {};
}
}
};
}
return resource;
}
function videosInsert(auth, requestData) {
var service = google.youtube('v3');
var parameters = removeEmptyParameters(requestData['params']);
parameters['auth'] = auth;
parameters['media'] = { body: fs.createReadStream(requestData['mediaFilename']) };
parameters['notifySubscribers'] = false;
parameters['resource'] = createResource(requestData['properties']);
var req = service.videos.insert(parameters, function(err, data) {
if (err) {
console.log('The API returned an error: ' + err);
}
if (data) {
// console.log(util.inspect(data, false, null));
event.emit('finished')
}
// process.exit();
});
var fileSize = fs.statSync(requestData['mediaFilename']).size;
// show some progress
var id = setInterval(function () {
var uploadedBytes = req.req.connection._bytesDispatched;
var uploadedMBytes = uploadedBytes / 1048576;
var progress = uploadedBytes > fileSize ? 100 : (uploadedBytes / fileSize) * 100;
event.emit('progress', uploadedMBytes, fileSize / 1048576, progress)
//console.log(uploadedMBytes.toFixed(2) + ' MBs uploaded. ' + progress.toFixed(2) + '% completed.');
if (progress === 100) {
console.log('Done uploading, waiting for response...');
clearInterval(id);
}
}, 250);
}
function removeEmptyParameters(params) {
for (var p in params) {
if (!params[p] || params[p] == 'undefined') {
delete params[p];
}
}
return params;
}
function storeToken(token) {
fs.writeFile(TOKEN_PATH, JSON.stringify(token))
console.log('Token stored to ' + TOKEN_PATH)
}
function getNewToken(oauth2Client, requestData, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
})
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('Enter the code from that page here: ', function(code) {
rl.close()
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err)
return
}
oauth2Client.credentials = token
storeToken(token)
callback(oauth2Client, requestData)
})
})
}
function authorize(credentials, requestData, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, requestData, callback)
} else {
oauth2Client.credentials = JSON.parse(token)
callback(oauth2Client, requestData)
}
});
}
function upload(prop) {
if (!prop.file) {
console.log('No file is specified to upload!')
process.exit()
}
fs.readFile(CREDS, (err, cont) => {
if (err) {
console.log('Error loading client secret file: \n', err)
return
}
authorize(JSON.parse(cont), {
'params': {
'part': 'snippet, status'
},
'properties': {
'snippet.categoryId': '22',
'snippet.defaultLanguage': '',
'snippet.description': prop.description ? prop.description : '',
'snippet.tags[]': prop.tags ? prop.tags : '',
'snippet.title': prop.title ? prop.title : prop.file,
'status.embeddable': '',
'status.license': '',
'status.privacyStatus': prop.privacy ? prop.privacy : 'private',
'status.publicStatsViewable': ''
},
'mediaFilename': prop.file
}, videosInsert)
})
}
exports.upload = upload
exports.event = event