-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
115 lines (102 loc) · 3.24 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
var config = require("./config");
var request = require('request');
// youtube-api
var youtube = require('youtube-api');
youtube.authenticate({
type: "key",
key: config.youtube.apikey
});
// xml2js
var xml2js = require('xml2js');
var parseString = new xml2js.Parser({
explicitArray: false
}).parseString;
// express
var express = require('express');
var app = express();
app.set('views', './views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
// body-parser
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
function getAllSubscriptions(channel, cb, pagetoken, context) {
youtube.subscriptions.list({
part: "snippet",
channelId: channel,
maxResults: 50,
pageToken: pagetoken
}, function (err, data) {
if(err != null){
cb(err, null);
return;
}
if (context === undefined) {
context = {
videos: [],
total: data.items.length,
done: 0
};
} else {
context.total += data.items.length;
}
if(data.nextPageToken !== undefined){
getAllSubscriptions(channel, cb, data.nextPageToken, context);
}
data.items.forEach(function(entry) {
request("https://www.youtube.com/feeds/videos.xml?channel_id=" + entry.snippet.resourceId.channelId,
function (error, response, body) {
if (!error && response.statusCode == 200) {
parseString(body, function (err, result) {
context.videos.push.apply(context.videos, result.feed.entry);
});
}
context.done++;
if (context.done === context.total) {
context.videos.sort(function(a, b){return b.updated.localeCompare(a.updated);})
context.videos = context.videos.slice(0, config.app.videosinresponse);
cb(null, context.videos);
}
});
});
});
}
app.get('/', function (req, res) {
res.render('index');
});
app.post('/', function (req, res) {
res.redirect('/feed/' + req.body.channelid);
});
app.get('/feed/:channelid', function (req, res) {
getAllSubscriptions(req.params.channelid, function(err, videos) {
if (err != null){
res.status(500).send('Error\n' + JSON.stringify(err)).end();
} else {
for (i = 0; i < videos.length; i++) {
videos[i]['media:group']['media:description'] =
'<iframe width="650" height="390" src="https://www.youtube.com/embed/' + videos[i]['yt:videoId'] +
'?autoplay=0" frameborder="0" allowfullscreen></iframe><br>' +
videos[i]['media:group']['media:description'].split("\n").join("<br>");
}
res.set('Content-Type', 'text/xml');
// Fuckit, KISS! I just need a feed
res.render('rss', { config: config, videos: videos, channelid: req.params.channelid });
}
});
});
app.get('/api/:channelid', function (req, res) {
getAllSubscriptions(req.params.channelid, function(videos) {
res.set('Content-Type', 'text/xml');
res.render('atom', { videos: videos });
});
});
app.use(function(req, res) {
res.status(404).send('Not found');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('yt-api-v3-newsubscriptionvideos listening at http://%s:%s', host, port);
});