Commit d3028fdb authored by Marco Flowers's avatar Marco Flowers

Fixed some of the style issues with the code

parent b1af9716
......@@ -19,7 +19,7 @@ var index = require('./controllers/index.js')(query, cache, streamApi)
var genres = require('./controllers/genres.js')(query, cache, streamApi);
var formats = require('./controllers/formats.js')(query, cache, streamApi);
var yp_cgi = require('./controllers/yp-cgi.js')(query, qs, validator);
var listen = require('./controllers/listen.js')(query, qs, streamApi)
var listen = require('./controllers/listen.js')(query, qs, streamApi);
......@@ -43,52 +43,45 @@ app.get('/', index);
app.post('/cgi-bin/yp-cgi', yp_cgi);
app.get('/by_genre/:genre', genres);
app.get('/by_format/:format', formats);
app.get('/listen/:streamId/:filename',listen)
app.get('/listen/:streamId/:filename',listen);
/* JSON API */
function respond(res, err, rows, result)
{
if(err)
{
function respond(res, err, rows, result) {
if(err) {
res.send([]);
}
else
{
} else {
res.send(rows);
}
}
app.get('/streams/', function(req,res){
res.set('Content-Type', 'application/json')
streamApi(req.query,function(err, rows,result){
respond(res,err,rows,result)
res.set('Content-Type', 'application/json');
streamApi(req.query, function(err, rows,result){
respond(res, err, rows, result);
});
});
app.get('/streams/:streamId',function(req,res){
res.set('Content-Type', 'application/json')
params = {'id':req.params.streamId}
streamApi(params,function(err,rows,result){
if(err || result.rowCount != 1)
{
app.get('/streams/:streamId', function(req,res){
res.set('Content-Type', 'application/json');
params = {'id':req.params.streamId};
streamApi(params, function(err,rows,result){
if(err || result.rowCount != 1) {
res.send([]);
}
else
{
} else {
res.send(rows[0]);
}
});
})
});
app.get('/genres/', function(req, res) {
res.set('Content-Type', 'application/json')
res.set('Content-Type', 'application/json');
var genresq = 'SELECT DISTINCT val FROM (SELECT unnest(genres) as val FROM streams) s;';
query(genresq, function(err, rows, result) {
respond(res,err,rows,result)
respond(res, err, rows, result);
});
});
app.get('/formats/', function(req, res) {
res.set('Content-Type', 'application/json')
res.set('Content-Type', 'application/json');
var formats = 'SELECT DISTINCT val FROM (SELECT unnest(codec_sub_types) as val FROM streams) s;';
query(formats, function(err, rows, result) {
respond(res,err,rows,result)
respond(res, err, rows, result);
});
});
......
......@@ -34,8 +34,8 @@ function getCachedFormatStreams(format, cb) {
}
function getFormatStreams(format, cb) {
params = {'format':format}
streamApi(params,function(err, rows, result) {
var params = {'format':format};
streamApi(params, function(err, rows, result) {
cb(err, result);
});
}
......
......@@ -34,8 +34,8 @@ function getCachedGenreStreams(genre, cb) {
}
function getGenreStreams(genre, cb) {
params = {'genre':genre}
streamApi(params,function(err, rows, result) {
var params = {'genre':genre};
streamApi(params, function(err, rows, result) {
cb(err, result);
});
}
......
......@@ -18,7 +18,7 @@ function index(req, res) {
} else {
error = false;
}
console.log(result.rows)
console.log(result.rows);
res.render("index", {
title: '',
servers: result.rows,
......@@ -35,8 +35,8 @@ function getCachedRandomStreams(count, cb) {
}
function getRandomStreams(count, cb) {
params = {'limit':count,'order':0}
streamApi(params,function(err, rows, result) {
var params = {'limit':count,'order':0};
streamApi(params, function(err, rows, result) {
cb(err, result);
});
}
......
......@@ -8,67 +8,61 @@ function init(q, c, s) {
}
function getListen(req, res) {
params = {'id':req.params.streamId}
console.log(req.get('User-Agent'))
var userAgent = req.get('User-Agent')
filename = req.params.filename
params = {'id':req.params.streamId};
console.log(req.get('User-Agent'));
var userAgent = req.get('User-Agent');
filename = req.params.filename;
streamApi(params,function(err, rows, result) {
if(err || result.rowCount != 1)
{
if(err || result.rowCount != 1) {
res.status(404);
res.send('Stream not in the Database');
return
return;
}
var extension = filename.substr(filename.lastIndexOf('.')+1)
console.log(extension)
if(extension != 'm3u' && extension != 'xspf')
{
var extension = filename.substr(filename.lastIndexOf('.')+1);
console.log(extension);
if(extension != 'm3u' && extension != 'xspf') {
res.status(400);
res.send('Not the right extension');
return
return;
}
var listenurls = rows[0].listenurls
if(extension == 'm3u')
{
var outputString = ''
var listenurls = rows[0].listenurls;
var outputString;
if(extension == 'm3u') {
outputString = '';
for (var i = 0; i < listenurls.length; i++)
{
outputString += listenurls[i]+'\r\n'
outputString += listenurls[i]+'\r\n';
}
res.set('Content-Type','audio/x-mpegurl')
res.set('Content-Type', 'audio/x-mpegurl');
if(userAgent.indexOf('/MSIE 5.5/') != -1)
{
res.set('Content-Disposition','inline; filename="listen.m3u"')
res.set('Content-Disposition', 'inline; filename="listen.m3u"');
}
else
{
res.set('Content-Disposition','filename="listen.m3u"')
res.set('Content-Disposition', 'filename="listen.m3u"');
}
res.send(outputString)
}
else if(extension == 'xspf')
{
var outputString = '<?xml version="1.0" encoding="UTF-8"?>\
res.send(outputString);
} else if(extension == 'xspf') {
outputString = '<?xml version="1.0" encoding="UTF-8"?>\
\r\n<playlist version="1" xmlns="http://xspf.org/ns/0/">\
\r\n <title>'+rows[0].stream_name+'</title>\
\r\n <info>'+rows[0].url+'</info>\
\r\n <trackList>'
for (var i = 0; i < listenurls.length; i++)
\r\n <trackList>';
for (var d = 0; d < listenurls.length; d++)
{
outputString +='\r\n <track><location>'+listenurls[i]+'</location></track>'
outputString +='\r\n <track><location>'+listenurls[d]+'</location></track>';
}
outputString +='\r\n </trackList>\r\n</playlist>'
res.set('Content-Type','application/xspf+xml')
res.set('Content-Disposition','filename="listen.xspf"')
if(userAgent.indexOf('/MSIE 5.5/') != -1)
{
res.set('Content-Disposition','inline; filename="listen.xspf"')
outputString +='\r\n </trackList>\r\n</playlist>';
res.set('Content-Type','application/xspf+xml');
res.set('Content-Disposition','filename="listen.xspf"');
if(userAgent.indexOf('/MSIE 5.5/') != -1) {
res.set('Content-Disposition','inline; filename="listen.xspf"');
}
else
{
res.set('Content-Disposition','filename="listen.xspf"')
else {
res.set('Content-Disposition','filename="listen.xspf"');
}
res.send(outputString)
res.send(outputString);
}
});
}
......
......@@ -18,176 +18,140 @@ function init(q, c) {
*/
function streamApi(params,callback)
{
var allStreamsBeg = 'SELECT s.id, s.stream_name, s.stream_type, s.description,s.songname, s.url, s.avg_listening_time, s.codec_sub_types, s.bitrate, s.hits, s.cm, s.samplerate, s.channels, s.quality, s.genres, array_agg(sm.listenurl) AS listenurls, COUNT(sm.listeners) AS listeners, COUNT(sm.max_listeners) AS max_listeners FROM streams s INNER JOIN server_mounts AS sm ON s.id = sm.stream_id '
var allStreamsGroup = 'GROUP BY s.id '
var countItems = 1
var items = []
var queryString = allStreamsBeg
var allStreamsBeg = 'SELECT s.id, s.stream_name, s.stream_type, s.description,s.songname, s.url, s.avg_listening_time, s.codec_sub_types, s.bitrate, s.hits, s.cm, s.samplerate, s.channels, s.quality, s.genres, array_agg(sm.listenurl) AS listenurls, COUNT(sm.listeners) AS listeners, COUNT(sm.max_listeners) AS max_listeners FROM streams s INNER JOIN server_mounts AS sm ON s.id = sm.stream_id ';
var allStreamsGroup = 'GROUP BY s.id ';
var countItems = 1;
var items = [];
var queryString = allStreamsBeg;
var genre, format, streamId, search, order, limit, offset;
if(params.genre)
{
genre = params.genre
if(Array.isArray(genre))
{
if(params.genre) {
genre = params.genre;
if(Array.isArray(genre)) {
for(var i =0; i <genre.length;i++)
{
queryString += genreFormatModifier(countItems, 's.genres')
countItems++
items.push(genre[i])
queryString += genreFormatModifier(countItems, 's.genres');
countItems++;
items.push(genre[i]);
}
}
else
{
queryString += genreFormatModifier(countItems, 's.genres')
countItems++
items.push(genre)
} else {
queryString += genreFormatModifier(countItems, 's.genres');
countItems++;
items.push(genre);
}
}
if(params.format)
{
format = params.format
if(Array.isArray(format))
{
if(params.format) {
format = params.format;
if(Array.isArray(format)) {
for(var i =0; i <genre.length;i++)
{
queryString += genreFormatModifier(countItems, 's.codec_sub_types')
countItems++
items.push(format[i])
queryString += genreFormatModifier(countItems, 's.codec_sub_types');
countItems++;
items.push(format[i]);
}
}
else
{
queryString += genreFormatModifier(countItems, 's.codec_sub_types')
countItems++
items.push(format)
} else {
queryString += genreFormatModifier(countItems, 's.codec_sub_types');
countItems++;
items.push(format);
}
}
if(params.id)
{
streamId = params.id
if(countItems > 1)
{
queryString += 'AND $'+countItems+' = s.id '
}
else
{
queryString += 'WHERE $'+countItems+' = s.id '
}
countItems++
items.push(streamId)
if(params.id) {
streamId = params.id;
if(countItems > 1) {
queryString += 'AND $'+countItems+' = s.id ';
} else {
queryString += 'WHERE $'+countItems+' = s.id ';
}
countItems++;
items.push(streamId);
}
if(params.q)
{
search = params.q
if(params.q) {
search = params.q;
var add = false;
var searchWord = '';
var searchArray ='(';
if(Array.isArray(search))
{
search = search[0]
if(Array.isArray(search)) {
search = search[0];
}
search = search.split(' ')
var searchString = 'WHERE ('
if(countItems > 1)
{
searchString = 'AND ('
search = search.split(' ');
var searchString = 'WHERE (';
if(countItems > 1) {
searchString = 'AND (';
}
for(var i=0;i<search.length;i++)
{
if(i>3)
{
if(i>3) {
break;
}
// minimum word length of 3
if(search[i].length<3)
{
if(search[i].length<3) {
continue;
}
add = true
add = true;
//maximum word length of 10
if(search[i].length>10)
{
search[i] = search[i].substring(0,10)
if(search[i].length>10) {
search[i] = search[i].substring(0,10);
}
searchWord += search[i]+'%'
searchString += '$'+countItems+' LIKE ANY(s.genres) OR '
countItems++
items.push(search[i])
}
if(add)
{
searchString += 's.stream_name LIKE $'+countItems
+' OR s.description LIKE $'+countItems+' '
items.push(searchWord)
countItems++
searchWord += search[i]+'%';
searchString += '$'+countItems+' LIKE ANY(s.genres) OR ';
countItems++;
items.push(search[i]);
}
if(add) {
searchString += 's.stream_name LIKE $'+countItems+' OR s.description LIKE $'+countItems+' ';
items.push(searchWord);
countItems++;
searchString += ') '
queryString += searchString
}
else
{
searchString += ') ';
queryString += searchString;
} else {
// search query won't return any results
callback(0,[],{})
return
callback(0,[],{});
return;
}
}
// add in the Group BY
queryString = queryString + allStreamsGroup
if(params.order)
{
order = params.order
if(order == 0)
{
queryString += 'ORDER BY random() '
}
else if(order == 1)
{
queryString += 'ORDER BY listeners DESC'
}
else if(order == 2)
{
queryString += 'ORDER BY listeners ASC'
queryString = queryString + allStreamsGroup;
if(params.order) {
order = params.order;
if(order == 0) {
queryString += 'ORDER BY random() ';
} else if(order == 1) {
queryString += 'ORDER BY listeners DESC';
} else if(order == 2) {
queryString += 'ORDER BY listeners ASC';
}
}
if(params.limit) {
limit = params.limit
if(Array.isArray(limit))
{
limit = limit[0]
}
else
{
queryString += 'LIMIT $'+countItems+' '
countItems++
items.push(limit)
limit = params.limit;
if(Array.isArray(limit)) {
limit = limit[0];
} else {
queryString += 'LIMIT $'+countItems+' ';
countItems++;
items.push(limit);
}
}
if(params.offset) {
offset = params.offset
if(Array.isArray(offset))
{
offset = offset[0]
}
else
{
queryString += 'OFFSET $'+countItems+' '
countItems++
items.push(offset)
offset = params.offset;
if(Array.isArray(offset)) {
offset = offset[0];
} else {
queryString += 'OFFSET $'+countItems+' ';
countItems++;
items.push(offset);
}
}
//console.log(queryString)
query(queryString,items,callback)
query(queryString, items, callback);
}
function genreFormatModifier(count, type)
{
if(count > 1)
{
return ('AND $'+count+' = ANY('+type+') ')
if(count > 1) {
return ('AND $'+count+' = ANY('+type+') ');
}
else
{
return ('WHERE $'+count+' = ANY('+type+') ')
else {
return ('WHERE $'+count+' = ANY('+type+') ');
}
}
......
......@@ -22,8 +22,7 @@ function checkPresent(toCheck, check)
{
for(var i=0;i<check.length;i++)
{
if(!(check[i] in toCheck))
{
if(!(check[i] in toCheck)) {
return false;
}
}
......@@ -34,8 +33,7 @@ function multiIndexOf(toCheck, check)
{
for(var i=0;i<check.length;i++)
{
if(toCheck.indexOf(check[i]) != -1)
{
if(toCheck.indexOf(check[i]) != -1) {
return false;
}
}
......@@ -46,54 +44,48 @@ function ypAdd(req, res) {
var start = new Date().getTime();
var params = req.body;
// check mandatory arguments
var mandatoryArgs = ['sn', 'type', 'genre', 'listenurl']
var mandatoryArgs = ['sn', 'type', 'genre', 'listenurl'];
var illegalListenUrls = ['dev.local','testvm.hivane.net',
'backup.abidingradio.com']
var abuseIps = ['92.246.30.112']
'backup.abidingradio.com'];
var abuseIps = ['92.246.30.112'];
var misconfiguredUrls = ['hostingcenter.com']
var misconfiguredIps = ['192.240.97.','192.240.102.','50.7..']
var misconfiguredUrls = ['hostingcenter.com'];
var misconfiguredIps = ['192.240.97.','192.240.102.','50.7..'];
//need to add others
var defaultServerNames = ['Unspecified name','This is my server name',
'Stream Name','My Station name']
'Stream Name','My Station name'];
if( checkPresent(params, mandatoryArgs) == false)
{
if( checkPresent(params, mandatoryArgs) == false) {
ypRes(res, false, "Not enough arguments", -1, null);
return;
}
if( validator.isURL(params.listenurl) == false)
{
if( validator.isURL(params.listenurl) == false) {
ypRes(res, false, "Not a real listenurl", -1, null);
return;
}
if( multiIndexOf(params.listenurl, illegalListenUrls) == false)
{
if( multiIndexOf(params.listenurl, illegalListenUrls) == false) {
ypRes(res, false, "Illegal listen_url. Don't test against a production \
server, thanks! ", -1, null);
return;
}
if( multiIndexOf(params.listenurl, abuseIps) == false)
{
if( multiIndexOf(params.listenurl, abuseIps) == false) {
ypRes(res, false, "Your server has been banned for abuse, have a nice \
day!", -1, null);
return;
}
if( multiIndexOf(params.listenurl, misconfiguredUrls) == false)// || multiIndexOf(ip,misconfiguredIps))
{
// || multiIndexOf(ip,misconfiguredIps))
if( multiIndexOf(params.listenurl, misconfiguredUrls) == false) {
ypRes(res, false, "The network range in which your server resides has \
been suspended due to a high number of wrongly configured servers! Please\
contact webmaster@xiph.org urgently!", -1, null);
return;
}
if( multiIndexOf(params.sn, defaultServerNames) == false)
{
if( multiIndexOf(params.sn, defaultServerNames) == false) {
ypRes(res, false, "Default stream name detected, please configure your \
source client, thanks! ", -1, null);
return;
......@@ -104,26 +96,24 @@ function ypAdd(req, res) {
var insertStream = 'INSERT INTO streams (stream_name, stream_type, genres, bitrate, cluster_pass, description, url, codec_sub_types, channels, samplerate, quality) \
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) \
RETURNING id;'
RETURNING id;';
var insertServerMount = 'INSERT INTO server_mounts (sid, stream_id, lasttouch, listenurl) \
VALUES (uuid_generate_v4(), $1, now(), $2) \
RETURNING sid;'
RETURNING sid;';
//inserts new stream
//inserts new server_mount
async.waterfall([
function start(cb) {
// try to insert the Stream
query(insertStream,[final.server_name, final.server_type, final.genres,
query(insertStream, [final.server_name, final.server_type, final.genres,
final.bitrate, final.cluster_pass, final.description, final.url,
final.codec_sub_types, final.channels, final.samplerate, final.quality],cb)
final.codec_sub_types, final.channels, final.samplerate, final.quality], cb);
},
function(row,result, cb)
{
function(row,result, cb) {
// try to insert the Server Mount
query(insertServerMount,[row[0].id,final.listenurl], cb)
query(insertServerMount, [row[0].id,final.listenurl], cb);
},
function(row,result, cb)
{
function(row,result, cb) {
ypRes(res, true, "Successfully Added", row[0].sid, 200);
//var end = new Date().getTime();
......@@ -132,9 +122,8 @@ function ypAdd(req, res) {
},
],
function(err, result) {
if(err)
{
console.log(err)
if(err) {
console.log(err);
ypRes(res, false, "Server error", -1, null);
}
});
......@@ -151,34 +140,29 @@ function ypTouch(req, res) {
async.waterfall([