-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.js
150 lines (128 loc) · 4.37 KB
/
upload.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
const fs = require("fs");
const fsp = fs.promises;
const readline = require("readline");
const { promisify } = require("util");
const path = require("path");
const cliProgress = require("cli-progress");
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
const TOKEN_PATH = "token.json";
const SCOPES = ["https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube"];
readline.Interface.prototype.question[promisify.custom] = function(prompt) {
return new Promise(resolve =>
readline.Interface.prototype.question.call(this, prompt, resolve),
);
};
readline.Interface.prototype.questionAsync = promisify(
readline.Interface.prototype.question,
);
const getFileName = (filePath) => {
return path.parse(filePath).name;
};
const getCredentials = async () => {
try {
// Load client secrets from a local file.
const content = await fsp.readFile("credentials.json");
return JSON.parse(content);
}
catch(error) {
console.log(`Error loading client secret file: ${ error }`);
}
};
const authorize = async () => {
const credentials = await getCredentials();
const { client_secret, client_id, redirect_uris } = credentials.web;
const oAuth2Client = new OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
let token = await readAccessToken();
if(!token) {
const { tokens } = await getAccessToken(oAuth2Client);
token = tokens;
await writeAccessToken(token);
}
oAuth2Client.setCredentials(token);
return oAuth2Client;
};
const readAccessToken = async () => {
try {
const token = await fsp.readFile(TOKEN_PATH);
return JSON.parse(token);
}
catch(error) {
return false;
}
};
const writeAccessToken = async (token) => {
try {
// Store the token to disk for later program executions
await fsp.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log(`Token stored to ${ TOKEN_PATH }`);
}
catch (error) {
return console.log(error);
}
};
const getAccessToken = async (oAuth2Client) => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES,
});
console.log(`Authorize this app by visiting this url: ${ authUrl }`);
const r1 = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const code = await r1.questionAsync("Enter the code from that page here: ");
r1.close();
try {
return await oAuth2Client.getToken(code);
}
catch (error) {
return console.log(`Error retrieving access token: ${ error }`);
}
};
const upload = async (filePath) => {
const auth = await authorize();
const file = filePath; // File location
const fileSize = fs.statSync(file).size;
const title = getFileName(filePath); // Filename becomes title
const category = "27"; // 'Education' category
const privacy = "unlisted"; // unlisted video setting
// Initialize youtube API
const youtube = google.youtube({ version: "v3", auth });
console.log(`Uploading: ${ title }`);
const progressBar = new cliProgress.SingleBar({
format: "|{bar}| {percentage}% uploaded",
hideCursor: true,
stopOnComplete: true,
}, cliProgress.Presets.shades_grey);
progressBar.start(100, 0);
const res = await youtube.videos.insert(
{
part: "id,snippet,status",
notifySubscribers: false,
requestBody: {
snippet: {
title: title,
description: "",
categoryId: category,
},
status: {
privacyStatus: privacy,
selfDeclaredMadeForKids: true,
},
},
media: {
body: fs.createReadStream(file),
},
},
{
onUploadProgress: evt => {
const progress = (evt.bytesRead / fileSize) * 100;
progressBar.update(progress);
},
}
);
return({ uploadStatus: res.data.status.uploadStatus, file: res.data.snippet.title, urlID: res.data.id });
};
module.exports = upload;