diff --git a/lib/googleplaymusic.js b/lib/googleplaymusic.js index 96a0259..e1797e3 100644 --- a/lib/googleplaymusic.js +++ b/lib/googleplaymusic.js @@ -37,26 +37,22 @@ module.exports.search = function(query, type, next) { } module.exports.parseUrl = function(url, next) { - // https://play.google.com/music/listen#/album/B3lxthejqxjxja2bhzchcw5qaci - // https://play.google.com/music/listen#/album//Underworld/Everything%2C+Everything+(Live) var parsed = parse(url.replace(/\+/g, "%20")); var path = parsed.path; var hash = parsed.hash; - console.log(path) if (hash) { - var matches = hash.match(/\/album[\/]+([^\/]+)\/([^\/]+)/); + var parts = hash.split("/"); + var type = parts[1]; + var id = parts[2]; + var artist = decodeURIComponent(parts[3]); + var album = decodeURIComponent(parts[4]); - if (matches && matches[2]) { - var artist = decodeURIComponent(matches[1]); - var album = decodeURIComponent(matches[2]); + if (id.length > 0) { + return next({id: id, type: type}); + } else { module.exports.search(artist + " " + album, "album", function(googleAlbum) { next(googleAlbum); }); - } else { - var matches = hash.match(/\/album[\/]+([\w]+)/); - if (matches && matches[1]) { - return next({id:matches[1], type: "album"}); - } } } else if(path) { var matches = path.match(/\/music\/m\/([\w]+)/); diff --git a/lib/rdio.js b/lib/rdio.js new file mode 100644 index 0000000..1b50a31 --- /dev/null +++ b/lib/rdio.js @@ -0,0 +1,90 @@ +var parse = require('url').parse; + +if (!process.env.RDIO_API_KEY || !process.env.RDIO_API_SHARED) { + throw new Error("You need to set GOOGLE_EMAIL and GOOGLE_PASSWORD environment variables"); +} + + +var rdio = require('rdio')({ + rdio_api_key: process.env.RDIO_API_KEY, + rdio_api_shared: process.env.RDIO_API_SHARED, +}); + +module.exports.lookupId = function(id, next) { + rdio.api("", "", { + method: 'getObjectFromShortCode', + short_code: id, + }, function(err, results) { + var result = JSON.parse(results).result; + var parsed = parse(result.shortUrl) + var id = parsed.path.replace("/x/", "").replace("/", ""); + next({ + id: id, + name: result.name, + url: result.shortUrl, + artwork: result.icon.replace("http:", ""), + artist: { + name: result.artist + }, + type: "album" + }); + }); +}; + +module.exports.lookupUrl = function(url, next) { + var parsed = parse(url); + + var data; + + if (parsed.host == "rd.io") { + data = { + method: 'getObjectFromShortCode', + short_code: parsed.path.replace("/x/", "").replace("/", ""), + }; + } else if (parsed.host.match(/rdio\.com$/)) { + data = { + method: 'getObjectFromUrl', + url: parsed.path, + }; + } else { + return; + } + + rdio.api("", "", data, function(err, results) { + var result = JSON.parse(results).result; + var parsed = parse(result.shortUrl) + var id = parsed.path.replace("/x/", "").replace("/", ""); + next({ + id: id, + name: result.name, + url: result.shortUrl, + artwork: result.icon.replace("http:", ""), + artist: { + name: result.artist + }, + type: "album" + }); + }); +}; + +module.exports.search = function(query, type, next) { + rdio.api("", "", { + query: query, + method: 'search', + types: type, + }, function(err, results) { + var result = JSON.parse(results).result.results[0]; + var parsed = parse(result.shortUrl) + var id = parsed.path.replace("/x/", "").replace("/", ""); + next({ + id: id, + name: result.name, + url: result.shortUrl, + artwork: result.icon.replace("http:", ""), + artist: { + name: result.artist + }, + type: "album" + }); + }); +}; diff --git a/lib/spotify.js b/lib/spotify.js index 31dd387..5cf4f7a 100644 --- a/lib/spotify.js +++ b/lib/spotify.js @@ -36,11 +36,6 @@ module.exports.search = function(query, type, next) { } module.exports.parseUrl = function(url, next) { - // https://play.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0 - // https://play.spotify.com/track/3W3ENDBQMJ9bD2qmxWI2f0 - // https://open.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0 - // https://open.spotify.com/track/3W3ENDBQMJ9bD2qmxWI2f0 - var matches = parse(url).path.match(/\/album[\/]+([^\/]+)/); if (matches && matches[1]) { diff --git a/package.json b/package.json index fcbd6b1..3785679 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "express-session": "^1.9.2", "morgan": "~1.3.0", "playmusic": "^1.1.0", + "rdio": "^1.5.2", "serve-favicon": "~2.1.3", "spotify": "^0.3.0" } diff --git a/routes/index.js b/routes/index.js index b4294c0..9ba30c2 100644 --- a/routes/index.js +++ b/routes/index.js @@ -3,9 +3,7 @@ var router = express.Router(); var googleplaymusic = require('../lib/googleplaymusic'); var spotify = require('../lib/spotify'); - -//https://play.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0 -//https://play.google.com/music/listen#/album/B3lxthejqxjxja2bhzchcw5qaci +var rdio = require('../lib/rdio'); router.get('/:service/:type/:id', function(req, res) { var service = req.params.service; @@ -16,14 +14,27 @@ router.get('/:service/:type/:id', function(req, res) { case "spotify": spotify.lookupId(id, type, function(spotifyAlbum) { googleplaymusic.search(spotifyAlbum.artist.name + " " + spotifyAlbum.name, "album", function(googleAlbum) { - res.render('album', {googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); + rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) { + res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); + }); }); }); break; case "google": googleplaymusic.lookupId(id, type, function(googleAlbum) { spotify.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(spotifyAlbum) { - res.render('album', {googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); + 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}); + }); }); }); break; @@ -34,7 +45,15 @@ router.post('/search', function(req, res) { // determine spotify or google music var url = req.body.url; - if (url.match(/spotify\.com/)) { + 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/)) { spotify.parseUrl(url, function(result) { if (!result.id) { req.flash('search-error', 'No match found for this link'); diff --git a/views/album.ejs b/views/album.ejs index 99793ef..bf52066 100644 --- a/views/album.ejs +++ b/views/album.ejs @@ -22,6 +22,9 @@