-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (103 loc) · 2.74 KB
/
index.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
require('dotenv').config();
const Youtube = require('youtube-api'),
fs = require('fs'),
readJson = require('r-json'),
Lien = require('lien'),
Logger = require('bug-killer'),
opn = require('opn'),
prettyBytes = require('pretty-bytes');
const chokidar = require('chokidar');
const videoLocation = process.env.VIDEO_LOCATION;
const videoDescription = process.env.VIDEO_DESRCIPTION || new Date();
const credentialsLocation = process.env.CREDENTIALS_LOCATION;
const allowedExtensions = [
'mov',
'mpeg4',
'mp4',
'avi',
'mpg',
'wmv',
'mpegps',
'flv'
];
const path = require('path');
let isUploading = false;
if (!videoLocation) {
console.log('Please specify a watch folder');
process.exit();
}
if (!credentialsLocation) {
console.log('Please specify your credentials location');
process.exit();
}
const CREDENTIALS = readJson(credentialsLocation);
const server = new Lien({
host: 'localhost',
port: 5000
});
const 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']
}));
setLoader = (request) => {
setInterval(() => {
if (isUploading) {
Logger.log(`${prettyBytes(request.req.connection._bytesDispatched)} bytes uploaded.`);
}
}, 250);
};
uploadVideo = (videoPath, videoName) => {
isUploading = true;
const req = Youtube.videos.insert({
resource: {
snippet: {
title: videoName,
description: videoDescription
},
status: {
privacyStatus: 'private'
}
},
part: 'snippet,status',
media: {
body: fs.createReadStream(videoPath)
}
}, (err) => {
isUploading = false;
if (err) console.log(err);
console.log('Done.');
});
setLoader(req);
}
createDirectoryWatcher = () => {
const joinedExtensions = allowedExtensions.join(',');
const location = `${videoLocation}/**/*.{${joinedExtensions}}`;
const watcher = chokidar.watch(location, {
persistent: true,
ignoreInitial: true
});
watcher.on('add', (newVideo) => {
const videoName = path.basename(newVideo);
console.log('File', videoName, 'has been added');
uploadVideo(newVideo, videoName);
});
}
server.addPage('/oauth2callback', lien => {
Logger.log(`Trying to get the token using the following code: ${lien.query.code}`);
oauth.getToken(lien.query.code, (err, token) => {
if (err) {
lien.lien(err, 400);
return Logger.log(err);
}
Logger.log('Got the token.');
oauth.setCredentials(token);
lien.end('You can start adding videos on your folder.');
createDirectoryWatcher();
});
});