-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
59 lines (53 loc) · 1.63 KB
/
server.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
var config = require('nodejs-config')(
__dirname
);
var ytAudioToPodcast = require('./youtube-audio-to-podcast.js');
var http = require("http");
var server = http.createServer(function(request, response) {
// Parse out the YT channel name
// Format : /[YouTube channel name]/feed.xml
var urlParts = request.url.split('/');
var channel = urlParts[1];
var filename = urlParts[2];
var mp3FileMatch = (filename || '').match(/(.*)\.mp3$/);
if (filename == 'feed.xml' || filename == 'user.xml') {
response.writeHead(200, {"Content-Type": "application/rss+xml;charset=utf-8"});
ytAudioToPodcast.getPodcastRssXmlByUsername(
channel,
request.url,
'http://'+request.headers.host+'/'+channel+'/[videoId].mp3',
function(xml) {
response.write(xml);
response.end();
}
);
} else if (filename == 'channel.xml') {
response.writeHead(200, {"Content-Type": "application/rss+xml;charset=utf-8"});
ytAudioToPodcast.getPodcastRssXmlByChannelId(
channel,
request.url,
'http://'+request.headers.host+'/'+channel+'/[videoId].mp3',
function(xml) {
response.write(xml);
response.end();
}
);
} else if (mp3FileMatch) {
response.writeHead(200, {
"Content-Type": "audio/mpeg",
'Transfer-Encoding':'chunked',
'connection': 'keep-alive',
'Content-Transfer-Encoding': 'binary',
});
var videoId = mp3FileMatch[1];
var audioStream = ytAudioToPodcast.getAudioStreamByVideoId(videoId);
audioStream.pipe(response);
} else {
response.writeHead(404);
response.write('Invalid request');
response.end();
}
});
var port = config.get('local.port');
server.listen(port);
console.log("Listening on "+port);