From dcca32c99846e28ab0c0534b41a172b4c7d6052d Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Thu, 4 Dec 2014 21:53:50 +0000 Subject: [PATCH] iTunes tracks, iTunes url lookups --- lib/services/googleplaymusic.js | 6 +- lib/services/itunes.js | 124 ++++++++++++++++---------------- lib/services/rdio.js | 2 +- routes/index.js | 2 +- views/album.ejs | 28 ++++---- views/track.ejs | 47 ++++++------ 6 files changed, 110 insertions(+), 99 deletions(-) diff --git a/lib/services/googleplaymusic.js b/lib/services/googleplaymusic.js index 668faf6..d0550a1 100644 --- a/lib/services/googleplaymusic.js +++ b/lib/services/googleplaymusic.js @@ -69,7 +69,11 @@ module.exports.search = function(data) { query = data.artist.name + " " + data.album.name + " " + data.name; } - pm.search(query, 5, function(data) { // max 5 results + pm.search(query, 5, function(data) { + if (!data.entries) { + deferred.resolve({service: "googleplaymusic"}); + return; + } var result = data.entries.filter(function(result) { return result[type]; }).sort(function(a, b) { // sort by match score diff --git a/lib/services/itunes.js b/lib/services/itunes.js index 96f541c..f9299b4 100644 --- a/lib/services/itunes.js +++ b/lib/services/itunes.js @@ -14,81 +14,83 @@ module.exports.match = function(url, type) { module.exports.lookupId = function(id, type) { var deferred = Q.defer(); - if (type == "album") { - request.get(apiRoot + "/albums/" + id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).end(function(res) { - var artwork = res.headers.location; - request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key, function(res) { - var result = res.body.data; - deferred.resolve({ - service: "itunes", - type: "album", - id: result.id, - name: result.title, - url: "https://listen.beatsmusic.com/albums/" + result.id, - artwork: artwork, - artist: { - name: result.artist_display_name - } - }); - }); - }); - } else if (type == "track") { - request.get(apiRoot + "/tracks/" + id + "?client_id=" + credentials.key, function(res) { - var result = res.body.data; - request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).end(function(res) { - var artwork = res.headers.location; - deferred.resolve({ - service: "itunes", - type: "track", - id: result.id, - name: result.title, - purchaseUrl: "https://listen.beatsmusic.com/albums/" + result.refs.album.id + "/tracks/" + result.id, - streamUrl: null, - artwork: artwork, - artist: { - name: result.artist_display_name - }, - album: { - name: result.refs.album.display - } - }); - }); - }); - } - return deferred.promise; -} + var path = "/lookup?id=" + id; -module.exports.search = function(data) { - var deferred = Q.defer(); - var query = ""; - var type = data.type; - - if (type == "album") { - query = data.artist.name + " " + data.name; - } else if (type == "track") { - query = data.artist.name + " " + data.album.name + " " + data.name; - } - - var path = "/search?term=" + encodeURIComponent(query) + "&media=music&entity=album" request.get(apiRoot + path, function(res) { var data = JSON.parse(res.text); + if (!data.results[0].collectionId) { deferred.resolve({service: "itunes"}); } else { var result = data.results[0]; - deferred.resolve({ + var item = { service: "itunes", type: type, id: result.collectionId, - name: result.collectionName, + name: result.trackName ? result.trackName : result.collectionName, streamUrl: null, purchaseUrl: result.collectionViewUrl, artwork: result.artworkUrl100.replace("100x100", "200x200"), artist: { name: result.artistName } - }); + }; + + if (type == "track") { + item.album = { + name: result.collectionName + }; + } + + deferred.resolve(item); + } + }); + return deferred.promise; +} + +module.exports.search = function(data) { + var deferred = Q.defer(); + var query = ""; + var entity = ""; + var type = data.type; + + if (type == "album") { + query = data.artist.name + " " + data.name; + entity = "album"; + } else if (type == "track") { + query = data.artist.name + " " + data.album.name + " " + data.name; + entity = "musicTrack"; + } + + var path = "/search?term=" + encodeURIComponent(query) + "&media=music&entity=" + entity; + request.get(apiRoot + path, function(res) { + var data = JSON.parse(res.text); + + if (!data.results[0].collectionId) { + deferred.resolve({service: "itunes"}); + } else { + var result = data.results[0]; + + var item = { + service: "itunes", + type: type, + id: result.collectionId, + name: result.trackName ? result.trackName : result.collectionName, + streamUrl: null, + purchaseUrl: result.collectionViewUrl, + artwork: result.artworkUrl100.replace("100x100", "200x200"), + artist: { + name: result.artistName + } + }; + + if (type == "track") { + item.album = { + name: result.collectionName + }; + } + deferred.resolve(item); } }); @@ -97,10 +99,10 @@ module.exports.search = function(data) { module.exports.parseUrl = function(url) { var deferred = Q.defer(); - var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/); + var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)[\/]+([^\?]+)/); - if (matches && matches[2]) { - module.exports.lookupId(matches[2], matches[1]).then(deferred.resolve); + if (matches && matches[3]) { + module.exports.lookupId(matches[3].substr(2), matches[1]).then(deferred.resolve); } return deferred.promise; } diff --git a/lib/services/rdio.js b/lib/services/rdio.js index fb71f63..e53598f 100644 --- a/lib/services/rdio.js +++ b/lib/services/rdio.js @@ -113,7 +113,7 @@ module.exports.search = function(data) { }).shift(); if (!result) { - return next({service: "rdio"}); + return deferred.resolve({service: "rdio"}); } var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); diff --git a/routes/index.js b/routes/index.js index 50db1be..7d7c89b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -40,7 +40,7 @@ router.get('/:service/:type/:id', function(req, res) { return !a.id || !b.id; }).sort(function(a, b) { return !a.streamUrl || b.streamUrl; - }) + }); diff --git a/views/album.ejs b/views/album.ejs index e03abaa..36159ac 100644 --- a/views/album.ejs +++ b/views/album.ejs @@ -24,7 +24,7 @@
-

<%= items[0].artist.name %> - <%= items[0].name %>

+

<%= items[0].artist.name %> - <%= items[0].name %>

@@ -33,20 +33,20 @@
">
<%= i==0 ? "Found matches for this link" : "" %>
<% if (album.streamUrl) { %> - - + + <% } else if (album.purchaseUrl) { %> - - - <% }else { %> - - + + + <% }else { %> + + <% } %>
<% if((i+1)%4 == 0) { %>
<% } %> diff --git a/views/track.ejs b/views/track.ejs index e2e24a1..ee60391 100644 --- a/views/track.ejs +++ b/views/track.ejs @@ -29,27 +29,32 @@
<% for (var i=0;i < items.length;i++) { var track = items[i]; %> -
-
"> -
<%= i==0 ? "Found matches for this link" : "" %>
- <% if (track.url) { %> - - - <% } else { %> - - - <% } %> +
+
"> +
<%= i==0 ? "Found matches for this link" : "" %>
+ <% if (track.streamUrl) { %> + + + <% } else if (track.purchaseUrl) { %> + + + <% }else { %> + + + <% } %>
- <% } %> -
- +
<% if((i+1)%4 == 0) { %>
<% } %> + <% } %>
- - + +
+ +