-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatcher-upload.js
108 lines (101 loc) · 3.25 KB
/
watcher-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
const Youtube = require("youtube-api"),
readJson = require("r-json"),
Lien = require("lien"),
fs = require('fs'),
Logger = require("bug-killer"),
opn = require("opn"),
prettyBytes = require("pretty-bytes"),
path = require("path");
const watch = require('node-watch');
// I downloaded the file from OAuth2 -> Download JSON
const CREDENTIALS = readJson(`${__dirname}/credentials.json`);
const descSample = fs.readFileSync('sampleDescription.txt', 'utf8');
// Init lien server
var server = new Lien({
host: "localhost"
, port: 5000
});
const outputsDir = "./Outputs/";
const uploadsDir = "./Uploads/";
if (!fs.existsSync(uploadsDir)){
fs.mkdirSync(uploadsDir);
}
authenticate();
watch(outputsDir, { filter: /\.mp4$/ }, function(evt, path) {
if (evt == 'update') {
upload(path);
}
});
function authenticate() {
// Authenticate
// You can access the Youtube resources via OAuth2 only.
// https://developers.google.com/youtube/v3/guides/moving_to_oauth#service_accounts
var oauth = Youtube.authenticate({
type: "oauth"
, client_id: CREDENTIALS.web.client_id
, client_secret: CREDENTIALS.web.client_secret
, redirect_url: CREDENTIALS.web.redirect_uris[0]
});
opn(oauth.generateAuthUrl({
access_type: "offline"
, scope: ["https://www.googleapis.com/auth/youtube.upload"]
}));
// Handle oauth2 callback
server.addPage("/oauth2callback", lien => {
Logger.log("Getting token...");
oauth.getToken(lien.query.code, (err, tokens) => {
if (err) {
lien.lien(err, 400);
return Logger.log(err);
}
Logger.log("Token received.");
oauth.setCredentials(tokens);
lien.end(`<script>document.location = "http://google.fr"</script>`);
checkOutputFolder();
});
});
}
function upload(videoPath) {
const videoTitle = path.basename(videoPath).replace(".mp4", "").replace(".avi", "");
const video = JSON.parse(fs.readFileSync('./Projects/' + videoTitle + '/video.json', 'utf8'));
const videoDesc = descSample.replace("[@TITLE]", video.title).replace("[@SOUNDCLOUDLINK]", video.url)
console.log("Uploading " + video.title);
var req = Youtube.videos.insert({
resource: {
// Video title and description
snippet: {
title: video.title,
description: videoDesc
},
// I don't want to spam my subscribers
status: {
privacyStatus: "public"
}
},
// This is for the callback function
part: "snippet,status",
// Create the readable stream to upload the video
media: {
body: fs.createReadStream(videoPath)
}
}, (err, data) => {
console.log(video.title + " uploaded.");
clearInterval(upOutput);
fs.rename(outputsDir + path.basename(videoPath), uploadsDir + path.basename(videoPath), () => {
console.log("Video was successfuly moved.");
});
return;
});
var upOutput = setInterval(function () {
console.log(`${prettyBytes(req.req.connection._bytesDispatched)} bytes uploaded.`);
}, 250);
}
function checkOutputFolder() {
const files = fs.readdirSync(outputsDir);
for(var i = 0; i < files.length; i++) {
if(files[i].substr(-4) === '.mp4') {
console.log(files[i]);
upload(outputsDir + files[i]);
}
}
}