Add support for sharing tracks

This commit is contained in:
Jonathan Cremin 2014-12-02 01:35:10 +00:00
parent 39a22f65d9
commit 7e6527855b
5 changed files with 72 additions and 37 deletions

View file

@ -7,3 +7,9 @@ You'll need to have Rdio API keys (`RDIO_API_KEY` and `RDIO_API_SHARED`) and Goo
To get started, first `npm install` and then run the app with `npm start` or tests with `npm test`. To get started, first `npm install` and then run the app with `npm start` or tests with `npm test`.
This is in super early development, has no design and only supports albums right now. This is in super early development, has no design and only supports albums right now.
On the immediate todo list:
* Add support for tracks, and maybe artists
* Use album release year for additional sanity check on matches
* Do some kind of a design, particularly for the share page

View file

@ -10,29 +10,51 @@ if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
pm.init({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}, function() {}); pm.init({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}, function() {});
module.exports.lookupId = function(id, type, next) { module.exports.lookupId = function(id, type, next) {
pm.getAlbum(id, true, function(album) { if (type == "album") {
next({ pm.getAlbum(id, true, function(album) {
id: album.albumId, next({
name: album.name, id: album.albumId,
url: "https://play.google.com/music/listen#/album/" + album.albumId, name: album.name,
artwork: album.albumArtRef.replace("http:", ""), url: "https://play.google.com/music/listen#/album/" + album.albumId,
artist: { artwork: album.albumArtRef.replace("http:", ""),
name: album.artist artist: {
}, name: album.artist
type: "album" },
type: type
});
}); });
}); } else if (type == "track") {
pm.getAllAccessTrack(id, function(track) {
next({
id: track.nid,
name: track.title,
url: "https://play.google.com/music/listen#/track/" + track.nid + "/" + track.albumId,
artwork: track.albumArtRef[0].url.replace("http:", ""),
artist: {
name: track.artist
},
type: type
});
});
}
} }
module.exports.search = function(query, type, next) { module.exports.search = function(query, type, next) {
pm.search(query, 5, function(data) { // max 5 results pm.search(query, 5, function(data) { // max 5 results
var result = data.entries.filter(function(result) { var result = data.entries.filter(function(result) {
return result.album; return result[type];
}).sort(function(a, b) { // sort by match score }).sort(function(a, b) { // sort by match score
return a.score < b.score; return a.score < b.score;
}).shift(); }).shift();
module.exports.lookupId(result.album.albumId, "album", next); var id;
if (result.album) {
id = result.album.albumId;
} else if (result.track) {
id = result.track.nid;
}
module.exports.lookupId(id, type, next);
}); });
} }
@ -56,6 +78,7 @@ module.exports.parseUrl = function(url, next) {
} }
} else if(path) { } else if(path) {
var matches = path.match(/\/music\/m\/([\w]+)/); var matches = path.match(/\/music\/m\/([\w]+)/);
return next({id:matches[1], type: "album"}); var type = matches[1][0] == "T" ? "track" : "album";
module.exports.lookupId(matches[1], type, next);
} }
} }

View file

@ -18,6 +18,7 @@ module.exports.lookupId = function(id, next) {
var result = JSON.parse(results).result; var result = JSON.parse(results).result;
var parsed = parse(result.shortUrl) var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", ""); var id = parsed.path.replace("/x/", "").replace("/", "");
var type = result.album ? "track" : "album";
next({ next({
id: id, id: id,
name: result.name, name: result.name,
@ -26,7 +27,7 @@ module.exports.lookupId = function(id, next) {
artist: { artist: {
name: result.artist name: result.artist
}, },
type: "album" type: type
}); });
}); });
}; };
@ -54,6 +55,7 @@ module.exports.lookupUrl = function(url, next) {
var result = JSON.parse(results).result; var result = JSON.parse(results).result;
var parsed = parse(result.shortUrl) var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", ""); var id = parsed.path.replace("/x/", "").replace("/", "");
var type = result.album ? "track" : "album";
next({ next({
id: id, id: id,
name: result.name, name: result.name,
@ -62,7 +64,7 @@ module.exports.lookupUrl = function(url, next) {
artist: { artist: {
name: result.artist name: result.artist
}, },
type: "album" type: type
}); });
}); });
}; };
@ -74,6 +76,9 @@ module.exports.search = function(query, type, next) {
types: type, types: type,
}, function(err, results) { }, function(err, results) {
var result = JSON.parse(results).result.results[0]; var result = JSON.parse(results).result.results[0];
if (!result) {
return next({});
}
var parsed = parse(result.shortUrl) var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", ""); var id = parsed.path.replace("/x/", "").replace("/", "");
next({ next({
@ -84,7 +89,7 @@ module.exports.search = function(query, type, next) {
artist: { artist: {
name: result.artist name: result.artist
}, },
type: "album" type: type
}); });
}); });
}; };

View file

@ -13,8 +13,8 @@ module.exports.lookupId = function(id, type, next) {
next({ next({
id: data.id, id: data.id,
name: data.name, name: data.name,
url: "https://play.spotify.com/album/" + data.id, url: "https://play.spotify.com/" + type + "/" + data.id,
artwork: data.images[0].url.replace("http:", ""), artwork: data.images ? data.images[0].url.replace("http:", "") : data.album.images[0].url.replace("http:", ""),
artist: { artist: {
name: artist.name name: artist.name
} }
@ -29,16 +29,16 @@ module.exports.search = function(query, type, next) {
return; return;
} }
album = data.albums.items[0]; var item = data[type + "s"].items[0];
module.exports.lookupId(album.id, "album", next); module.exports.lookupId(item.id, type, next);
}); });
} }
module.exports.parseUrl = function(url, next) { module.exports.parseUrl = function(url, next) {
var matches = parse(url).path.match(/\/album[\/]+([^\/]+)/); var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
if (matches && matches[1]) { if (matches && matches[2]) {
next({id:matches[1], type: "album"}) next({id:matches[2], type: matches[1]})
} }
} }

View file

@ -1,3 +1,4 @@
var parse = require('url').parse;
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
@ -13,8 +14,8 @@ router.get('/:service/:type/:id', function(req, res) {
switch(service) { switch(service) {
case "spotify": case "spotify":
spotify.lookupId(id, type, function(spotifyAlbum) { spotify.lookupId(id, type, function(spotifyAlbum) {
googleplaymusic.search(spotifyAlbum.artist.name + " " + spotifyAlbum.name, "album", function(googleAlbum) { googleplaymusic.search(spotifyAlbum.artist.name + " " + spotifyAlbum.name, type, function(googleAlbum) {
rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) { rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, type, function(rdioAlbum) {
res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum});
}); });
}); });
@ -22,8 +23,8 @@ router.get('/:service/:type/:id', function(req, res) {
break; break;
case "google": case "google":
googleplaymusic.lookupId(id, type, function(googleAlbum) { googleplaymusic.lookupId(id, type, function(googleAlbum) {
spotify.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(spotifyAlbum) { spotify.search(googleAlbum.artist.name + " " + googleAlbum.name, type, function(spotifyAlbum) {
rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) { rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, type, function(rdioAlbum) {
res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum});
}); });
}); });
@ -31,8 +32,8 @@ router.get('/:service/:type/:id', function(req, res) {
break; break;
case "rdio": case "rdio":
rdio.lookupId(id, function(rdioAlbum) { rdio.lookupId(id, function(rdioAlbum) {
googleplaymusic.search(rdioAlbum.artist.name + " " + rdioAlbum.name, "album", function(googleAlbum) { googleplaymusic.search(rdioAlbum.artist.name + " " + rdioAlbum.name, type, function(googleAlbum) {
spotify.search(rdioAlbum.artist.name + " " + rdioAlbum.name, "album", function(spotifyAlbum) { spotify.search(rdioAlbum.artist.name + " " + rdioAlbum.name, type, function(spotifyAlbum) {
res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum});
}); });
}); });
@ -43,26 +44,26 @@ router.get('/:service/:type/:id', function(req, res) {
router.post('/search', function(req, res) { router.post('/search', function(req, res) {
// determine spotify or google music // determine spotify or google music
var url = req.body.url; var url = parse(req.body.url);
if (url.match(/rd\.io/) || url.match(/rdio\.com/)) { if (url.host.match(/rd\.io$/) || url.host.match(/rdio\.com$/)) {
rdio.lookupUrl(url, function(result) { rdio.lookupUrl(url.href, function(result) {
if (!result.id) { if (!result.id) {
req.flash('search-error', 'No match found for this link'); req.flash('search-error', 'No match found for this link');
res.redirect('/'); res.redirect('/');
} }
res.redirect("/rdio/" + result.type + "/" + result.id); res.redirect("/rdio/" + result.type + "/" + result.id);
}); });
} else if (url.match(/spotify\.com/)) { } else if (url.host.match(/spotify\.com$/)) {
spotify.parseUrl(url, function(result) { spotify.parseUrl(url.href, function(result) {
if (!result.id) { if (!result.id) {
req.flash('search-error', 'No match found for this link'); req.flash('search-error', 'No match found for this link');
res.redirect('/'); res.redirect('/');
} }
res.redirect("/spotify/" + result.type + "/" + result.id); res.redirect("/spotify/" + result.type + "/" + result.id);
}); });
} else if (url.match(/play\.google\.com/)) { } else if (url.host.match(/play\.google\.com$/)) {
googleplaymusic.parseUrl(url, function(result) { googleplaymusic.parseUrl(url.href, function(result) {
if (!result.id) { if (!result.id) {
req.flash('search-error', 'No match found for this link'); req.flash('search-error', 'No match found for this link');
res.redirect('/'); res.redirect('/');