combine.fm/routes/index.js

84 lines
2.9 KiB
JavaScript
Raw Normal View History

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-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;
switch(service) {
case "spotify":
spotify.lookupId(id, type, function(spotifyAlbum) {
googleplaymusic.search(spotifyAlbum.artist.name + " " + spotifyAlbum.name, "album", function(googleAlbum) {
2014-12-01 11:53:25 +00:00
rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) {
res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum});
});
2014-11-30 12:21:03 +00:00
});
});
break;
case "google":
googleplaymusic.lookupId(id, type, function(googleAlbum) {
spotify.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(spotifyAlbum) {
2014-12-01 11:53:25 +00:00
rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) {
res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum});
});
});
});
break;
case "rdio":
rdio.lookupId(id, function(rdioAlbum) {
googleplaymusic.search(rdioAlbum.artist.name + " " + rdioAlbum.name, "album", function(googleAlbum) {
spotify.search(rdioAlbum.artist.name + " " + rdioAlbum.name, "album", function(spotifyAlbum) {
res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum});
});
2014-11-30 12:21:03 +00:00
});
});
break;
}
});
router.post('/search', function(req, res) {
// determine spotify or google music
var url = req.body.url;
2014-12-01 11:53:25 +00:00
if (url.match(/rd\.io/) || url.match(/rdio\.com/)) {
rdio.lookupUrl(url, function(result) {
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/rdio/" + result.type + "/" + result.id);
});
} else if (url.match(/spotify\.com/)) {
2014-11-30 12:21:03 +00:00
spotify.parseUrl(url, function(result) {
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/spotify/" + result.type + "/" + result.id);
});
} else if (url.match(/play\.google\.com/)) {
googleplaymusic.parseUrl(url, function(result) {
if (!result.id) {
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
res.redirect("/google/" + result.type + "/" + result.id);
});
} 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;