combine.fm/routes/index.js

137 lines
4 KiB
JavaScript
Raw Normal View History

2014-12-02 11:46:39 +00:00
"use strict";
2014-12-02 01:35:10 +00:00
var parse = require('url').parse;
2014-11-30 12:21:03 +00:00
var express = require('express');
var router = express.Router();
var googleplaymusic = require('../lib/googleplaymusic');
var spotify = require('../lib/spotify');
2014-12-01 11:53:25 +00:00
var rdio = require('../lib/rdio');
2014-12-04 15:45:52 +00:00
var beats = require('../lib/beats');
2014-11-30 12:21:03 +00:00
var cache = {googleplaymusic:{}, spotify:{},rdio:{}};
2014-11-30 12:21:03 +00:00
router.get('/:service/:type/:id', function(req, res) {
var service = req.params.service;
var type = req.params.type;
var id = req.params.id;
var items = [];
2014-11-30 12:21:03 +00:00
switch(service) {
case "spotify":
spotify.lookupId(id, type, function(result) {
items.push(result);
googleplaymusic.search(result, function(item) {
items.push(item);
rdio.search(result, function(item) {
items.push(item);
2014-12-04 15:45:52 +00:00
beats.search(result, function(item) {
items.push(item);
res.render(result.type, {items: items});
});
2014-12-01 11:53:25 +00:00
});
2014-11-30 12:21:03 +00:00
});
});
break;
case "google":
googleplaymusic.lookupId(id, type, function(result) {
items.push(result);
spotify.search(result, function(item) {
items.push(item);
rdio.search(result, function(item) {
items.push(item);
2014-12-04 15:45:52 +00:00
beats.search(result, function(item) {
items.push(item);
res.render(result.type, {items: items});
});
2014-12-01 11:53:25 +00:00
});
});
});
break;
case "rdio":
2014-12-04 00:20:52 +00:00
rdio.lookupId(id, function(result) {
items.push(result);
googleplaymusic.search(result, function(item) {
items.push(item);
spotify.search(result, function(item) {
items.push(item);
2014-12-04 15:45:52 +00:00
beats.search(result, function(item) {
items.push(item);
res.render(result.type, {items: items});
});
});
});
});
break;
case "beats":
beats.lookupId(id, function(result) {
items.push(result);
googleplaymusic.search(result, function(item) {
items.push(item);
spotify.search(result, function(item) {
items.push(item);
rdio.search(result, function(item) {
items.push(item);
res.render(result.type, {items: items});
});
2014-12-01 11:53:25 +00:00
});
2014-11-30 12:21:03 +00:00
});
});
break;
}
});
router.post('/search', function(req, res) {
2014-12-02 01:35:10 +00:00
var url = parse(req.body.url);
2014-11-30 12:21:03 +00:00
2014-12-04 00:35:04 +00:00
if (!url.host) {
req.flash('search-error', 'Please paste a link below to find matches');
res.redirect('/');
return;
}
2014-12-02 01:35:10 +00:00
if (url.host.match(/rd\.io$/) || url.host.match(/rdio\.com$/)) {
rdio.lookupUrl(url.href, function(result) {
2014-12-01 11:53:25 +00:00
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/rdio/" + result.type + "/" + result.id);
});
2014-12-02 01:35:10 +00:00
} else if (url.host.match(/spotify\.com$/)) {
spotify.parseUrl(url.href, function(result) {
2014-11-30 12:21:03 +00:00
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/spotify/" + result.type + "/" + result.id);
});
2014-12-02 01:35:10 +00:00
} else if (url.host.match(/play\.google\.com$/)) {
googleplaymusic.parseUrl(url.href, function(result) {
if (!result) {
2014-11-30 12:21:03 +00:00
req.flash('search-error', 'No match found for this link');
res.redirect('/');
} else {
res.redirect("/google/" + result.type + "/" + result.id);
2014-11-30 12:21:03 +00:00
}
});
2014-12-04 15:45:52 +00:00
} else if (url.host.match(/beatsmusic\.com$/)) {
beats.parseUrl(url.href, function(result) {
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/beats/" + result.type + "/" + result.id);
});
2014-11-30 12:21:03 +00:00
} else {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
});
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { error: req.flash('search-error') });
});
module.exports = router;