forked from mnimn-png/Node.jsYoutubeVideoUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (134 loc) · 3.75 KB
/
app.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
const fs = require("fs");
const express = require("express");
const multer = require("multer");
const OAuth2Data = require("./credentials.json");
var title, description;
var tags = [];
const { google } = require("googleapis");
const app = express();
const CLIENT_ID = OAuth2Data.web.client_id;
const CLIENT_SECRET = OAuth2Data.web.client_secret;
const REDIRECT_URL = OAuth2Data.web.redirect_uris[0];
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL
);
var authed = false;
// If modifying these scopes, delete token.json.
const SCOPES =
"https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/userinfo.profile";
app.set("view engine", "ejs");
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, "./videos");
},
filename: function (req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
},
});
var upload = multer({
storage: Storage,
}).single("file"); //Field name and max count
app.get("/", (req, res) => {
if (!authed) {
// Generate an OAuth URL and redirect there
var url = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES,
});
console.log(url);
res.render("index", { url: url });
} else {
var oauth2 = google.oauth2({
auth: oAuth2Client,
version: "v2",
});
oauth2.userinfo.get(function (err, response) {
if (err) {
console.log(err);
} else {
console.log(response.data);
name = response.data.name;
pic = response.data.picture;
res.render("success", {
name: response.data.name,
pic: response.data.picture,
success: false,
});
}
});
}
});
app.post("/upload", (req, res) => {
upload(req, res, function (err) {
if (err) {
console.log(err);
return res.end("Something went wrong");
} else {
console.log(req.file.path);
title = req.body.title;
description = req.body.description;
tags = req.body.tags;
console.log(title);
console.log(description);
console.log(tags);
const youtube = google.youtube({ version: "v3", auth: oAuth2Client });
console.log(youtube)
youtube.videos.insert(
{
resource: {
// Video title and description
snippet: {
title:title,
description:description,
tags:tags
},
// I don't want to spam my subscribers
status: {
privacyStatus: "private",
},
},
// This is for the callback function
part: "snippet,status",
// Create the readable stream to upload the video
media: {
body: fs.createReadStream(req.file.path)
},
},
(err, data) => {
if(err) throw err
console.log(data)
console.log("Done.");
fs.unlinkSync(req.file.path);
res.render("success", { name: name, pic: pic, success: true });
}
);
}
});
});
app.get("/logout", (req, res) => {
authed = false;
res.redirect("/");
});
app.get("/google/callback", function (req, res) {
const code = req.query.code;
if (code) {
// Get an access token based on our OAuth code
oAuth2Client.getToken(code, function (err, tokens) {
if (err) {
console.log("Error authenticating");
console.log(err);
} else {
console.log("Successfully authenticated");
console.log(tokens);
oAuth2Client.setCredentials(tokens);
authed = true;
res.redirect("/");
}
});
}
});
app.listen(5000, () => {
console.log("App is listening on Port 5000");
});