Add rdio support

This commit is contained in:
Jonathan Cremin 2014-12-01 11:53:25 +00:00
parent aca2eea4dd
commit 493482386e
7 changed files with 129 additions and 25 deletions

View file

@ -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]+)/);

90
lib/rdio.js Normal file
View file

@ -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"
});
});
};

View file

@ -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]) {

View file

@ -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"
}

View file

@ -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');

View file

@ -22,6 +22,9 @@
<div class="row">
<img src="<%= googleAlbum.artwork %>" height=200 class="img-rounded" /> <%= googleAlbum.id %> <a href="<%= googleAlbum.url %>">Play on Google Music</a>
</div>
<div class="row">
<img src="<%= rdioAlbum.artwork %>" height=200 class="img-rounded" /> <%= rdioAlbum.id %> <a href="<%= rdioAlbum.url %>">Play on Rdio</a>
</div>
</div>
</body>
</html>

View file

@ -25,13 +25,13 @@
<form role="form" method="post" action="/search" class="form-inline">
<div class="form-group form-group-lg">
<input type="text" name="url" placeholder="Paste link here" class="form-control">
<input type="submit" class="btn btn-default btn-lg" value="Find Matches">
<input type="submit" class="btn btn-default btn-lg" value="Share Music">
</div>
</form>
</div>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<p>Make sharing music from subscription services better. Give us one link and we'll match it with other services and give you back a link with all of them. <a href="/google/album/B3qrtsvk5s3piwyla76sk6qyxny">Here's an example</a>.</p>
<p>Make sharing music from subscription services better. Give us one link (Rdio, Spotify or Google Music) and we'll match it with other services and give you back a link with all of them. <a href="/google/album/B3qrtsvk5s3piwyla76sk6qyxny">Here's an example</a>.</p>
</div>
</div>
</div>