From 7e6527855b01cf69a9cdc7ae0750eff138684329 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Tue, 2 Dec 2014 01:35:10 +0000 Subject: [PATCH] Add support for sharing tracks --- README.md | 6 +++++ lib/googleplaymusic.js | 51 ++++++++++++++++++++++++++++++------------ lib/rdio.js | 11 ++++++--- lib/spotify.js | 14 ++++++------ routes/index.js | 27 +++++++++++----------- 5 files changed, 72 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index b2a3a14..6f559e1 100644 --- a/README.md +++ b/README.md @@ -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`. 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 diff --git a/lib/googleplaymusic.js b/lib/googleplaymusic.js index e1797e3..2cc3111 100644 --- a/lib/googleplaymusic.js +++ b/lib/googleplaymusic.js @@ -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() {}); module.exports.lookupId = function(id, type, next) { - pm.getAlbum(id, true, function(album) { - next({ - id: album.albumId, - name: album.name, - url: "https://play.google.com/music/listen#/album/" + album.albumId, - artwork: album.albumArtRef.replace("http:", ""), - artist: { - name: album.artist - }, - type: "album" + if (type == "album") { + pm.getAlbum(id, true, function(album) { + next({ + id: album.albumId, + name: album.name, + url: "https://play.google.com/music/listen#/album/" + album.albumId, + artwork: album.albumArtRef.replace("http:", ""), + artist: { + name: album.artist + }, + 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) { pm.search(query, 5, function(data) { // max 5 results var result = data.entries.filter(function(result) { - return result.album; + return result[type]; }).sort(function(a, b) { // sort by match score return a.score < b.score; }).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) { 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); } } diff --git a/lib/rdio.js b/lib/rdio.js index e2e926f..7a6b496 100644 --- a/lib/rdio.js +++ b/lib/rdio.js @@ -18,6 +18,7 @@ module.exports.lookupId = function(id, next) { var result = JSON.parse(results).result; var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); + var type = result.album ? "track" : "album"; next({ id: id, name: result.name, @@ -26,7 +27,7 @@ module.exports.lookupId = function(id, next) { 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 parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); + var type = result.album ? "track" : "album"; next({ id: id, name: result.name, @@ -62,7 +64,7 @@ module.exports.lookupUrl = function(url, next) { artist: { name: result.artist }, - type: "album" + type: type }); }); }; @@ -74,6 +76,9 @@ module.exports.search = function(query, type, next) { types: type, }, function(err, results) { var result = JSON.parse(results).result.results[0]; + if (!result) { + return next({}); + } var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); next({ @@ -84,7 +89,7 @@ module.exports.search = function(query, type, next) { artist: { name: result.artist }, - type: "album" + type: type }); }); }; diff --git a/lib/spotify.js b/lib/spotify.js index 7149d69..9ac7d98 100644 --- a/lib/spotify.js +++ b/lib/spotify.js @@ -13,8 +13,8 @@ module.exports.lookupId = function(id, type, next) { next({ id: data.id, name: data.name, - url: "https://play.spotify.com/album/" + data.id, - artwork: data.images[0].url.replace("http:", ""), + url: "https://play.spotify.com/" + type + "/" + data.id, + artwork: data.images ? data.images[0].url.replace("http:", "") : data.album.images[0].url.replace("http:", ""), artist: { name: artist.name } @@ -29,16 +29,16 @@ module.exports.search = function(query, type, next) { 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) { - var matches = parse(url).path.match(/\/album[\/]+([^\/]+)/); + var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/); - if (matches && matches[1]) { - next({id:matches[1], type: "album"}) + if (matches && matches[2]) { + next({id:matches[2], type: matches[1]}) } } diff --git a/routes/index.js b/routes/index.js index 9ba30c2..edff907 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,3 +1,4 @@ +var parse = require('url').parse; var express = require('express'); var router = express.Router(); @@ -13,8 +14,8 @@ router.get('/:service/:type/:id', function(req, res) { switch(service) { case "spotify": spotify.lookupId(id, type, function(spotifyAlbum) { - googleplaymusic.search(spotifyAlbum.artist.name + " " + spotifyAlbum.name, "album", function(googleAlbum) { - rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) { + googleplaymusic.search(spotifyAlbum.artist.name + " " + spotifyAlbum.name, type, function(googleAlbum) { + rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, type, function(rdioAlbum) { res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); }); }); @@ -22,8 +23,8 @@ router.get('/:service/:type/:id', function(req, res) { break; case "google": googleplaymusic.lookupId(id, type, function(googleAlbum) { - spotify.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(spotifyAlbum) { - rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, "album", function(rdioAlbum) { + spotify.search(googleAlbum.artist.name + " " + googleAlbum.name, type, function(spotifyAlbum) { + rdio.search(googleAlbum.artist.name + " " + googleAlbum.name, type, function(rdioAlbum) { res.render('album', {rdioAlbum: rdioAlbum, googleAlbum: googleAlbum, spotifyAlbum: spotifyAlbum}); }); }); @@ -31,8 +32,8 @@ router.get('/:service/:type/:id', function(req, res) { 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) { + googleplaymusic.search(rdioAlbum.artist.name + " " + rdioAlbum.name, type, function(googleAlbum) { + spotify.search(rdioAlbum.artist.name + " " + rdioAlbum.name, type, function(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) { // 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/)) { - rdio.lookupUrl(url, function(result) { + if (url.host.match(/rd\.io$/) || url.host.match(/rdio\.com$/)) { + rdio.lookupUrl(url.href, 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) { + } else if (url.host.match(/spotify\.com$/)) { + spotify.parseUrl(url.href, 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) { + } else if (url.host.match(/play\.google\.com$/)) { + googleplaymusic.parseUrl(url.href, function(result) { if (!result.id) { req.flash('search-error', 'No match found for this link'); res.redirect('/');