diff --git a/lib/services/beats.js b/lib/services/beats.js index e69a976..164575e 100644 --- a/lib/services/beats.js +++ b/lib/services/beats.js @@ -34,7 +34,8 @@ module.exports.lookupId = function(id) { type: "album", id: result.id, name: result.title, - url: "https://listen.beatsmusic.com/albums/" + result.id, + streamUrl: "https://listen.beatsmusic.com/albums/" + result.id, + purchaseUrl: null, artwork: artwork, artist: { name: result.artist_display_name @@ -52,7 +53,8 @@ module.exports.lookupId = function(id) { type: "track", id: result.id, name: result.title, - url: "https://listen.beatsmusic.com/albums/" + result.refs.album.id + "/tracks/" + result.id, + streamUrl: "https://listen.beatsmusic.com/albums/" + result.refs.album.id + "/tracks/" + result.id, + purchaseUrl: null, artwork: artwork, artist: { name: result.artist_display_name diff --git a/lib/services/deezer.js b/lib/services/deezer.js index c3ba5b4..04e2443 100644 --- a/lib/services/deezer.js +++ b/lib/services/deezer.js @@ -27,7 +27,8 @@ module.exports.lookupId = function(id, type) { type: type, id: result.id, name: result.title, - url: result.link, + streamUrl: result.link, + purchaseUrl: null, artwork: artwork, artist: { name: result.artist.name diff --git a/lib/services/googleplaymusic.js b/lib/services/googleplaymusic.js index e8e6dfe..668faf6 100644 --- a/lib/services/googleplaymusic.js +++ b/lib/services/googleplaymusic.js @@ -28,7 +28,8 @@ module.exports.lookupId = function(id, type, next) { type: "album", id: album.albumId, name: album.name, - url: "https://play.google.com/music/listen#/album/" + album.albumId, + streamUrl: "https://play.google.com/music/listen#/album/" + album.albumId, + purchaseUrl: "https://play.google.com/store/music/album?id=" + album.albumId, artwork: album.albumArtRef.replace("http:", ""), artist: { name: album.artist @@ -42,7 +43,8 @@ module.exports.lookupId = function(id, type, next) { type: "track", id: track.nid, name: track.title, - url: "https://play.google.com/music/listen#/track/" + track.nid + "/" + track.albumId, + streamUrl: "https://play.google.com/music/listen#/track/" + track.nid + "/" + track.albumId, + purchaseUrl: "https://play.google.com/store/music/album?id=" + track.albumId, artwork: track.albumArtRef[0].url.replace("http:", ""), album: { name: track.album diff --git a/lib/services/itunes.js b/lib/services/itunes.js new file mode 100644 index 0000000..96f541c --- /dev/null +++ b/lib/services/itunes.js @@ -0,0 +1,106 @@ +"use strict"; +var parse = require('url').parse; +var request = require('superagent'); +var Q = require('q'); + +module.exports.id = "itunes"; + +var apiRoot = "https://itunes.apple.com"; + +module.exports.match = function(url, type) { + var parsed = parse(url); + return parsed.host.match(/itunes.apple\.com$/); +}; + +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; +} + +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({ + service: "itunes", + type: type, + id: result.collectionId, + name: result.collectionName, + streamUrl: null, + purchaseUrl: result.collectionViewUrl, + artwork: result.artworkUrl100.replace("100x100", "200x200"), + artist: { + name: result.artistName + } + }); + } + }); + + return deferred.promise; +} + +module.exports.parseUrl = function(url) { + var deferred = Q.defer(); + var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/); + + if (matches && matches[2]) { + module.exports.lookupId(matches[2], matches[1]).then(deferred.resolve); + } + return deferred.promise; +} diff --git a/lib/services/rdio.js b/lib/services/rdio.js index ddea295..fb71f63 100644 --- a/lib/services/rdio.js +++ b/lib/services/rdio.js @@ -34,7 +34,8 @@ module.exports.lookupId = function(id) { type: type, id: id, name: result.name, - url: result.shortUrl, + streamUrl: result.shortUrl, + purchaseUrl: null, artwork: result.icon.replace("http:", "").replace("square-200", "square-250"), artist: { name: result.artist @@ -74,7 +75,8 @@ module.exports.parseUrl = function(url) { type: type, id: id, name: result.name, - url: result.shortUrl, + streamUrl: result.shortUrl, + purchaseUrl: null, artwork: result.icon.replace("http:", "").replace("square-200", "square-250"), artist: { name: result.artist @@ -120,7 +122,8 @@ module.exports.search = function(data) { type: type, id: id, name: result.name, - url: result.shortUrl, + streamUrl: result.shortUrl, + purchaseUrl: null, artwork: result.icon.replace("http:", "").replace("square-200", "square-250"), artist: { name: result.artist diff --git a/lib/services/spotify.js b/lib/services/spotify.js index bea5a63..111a017 100644 --- a/lib/services/spotify.js +++ b/lib/services/spotify.js @@ -26,7 +26,8 @@ module.exports.lookupId = function(id, type) { type: type, id: data.id, name: data.name, - url: "https://play.spotify.com/" + type + "/" + data.id, + streamUrl: "https://play.spotify.com/" + type + "/" + data.id, + purchaseUrl: null, artwork: data.images ? data.images[1].url.replace("http:", "") : data.album.images[1].url.replace("http:", ""), artist: { name: artist.name @@ -38,7 +39,8 @@ module.exports.lookupId = function(id, type) { type: type, id: data.id, name: data.name, - url: "https://play.spotify.com/" + type + "/" + data.id, + streamUrl: "https://play.spotify.com/" + type + "/" + data.id, + purchaseUrl: null, artwork: data.images ? data.images[1].url.replace("http:", "") : data.album.images[1].url.replace("http:", ""), artist: { name: artist.name diff --git a/public/images/itunes.png b/public/images/itunes.png new file mode 100644 index 0000000..6260578 Binary files /dev/null and b/public/images/itunes.png differ diff --git a/routes/index.js b/routes/index.js index 5681c06..50db1be 100644 --- a/routes/index.js +++ b/routes/index.js @@ -38,8 +38,12 @@ router.get('/:service/:type/:id', function(req, res) { items.sort(function(a, b) { return !a.id || !b.id; + }).sort(function(a, b) { + return !a.streamUrl || b.streamUrl; }) + + items.unshift(item); res.render(type, {items: items}); diff --git a/views/album.ejs b/views/album.ejs index cedc15a..e03abaa 100644 --- a/views/album.ejs +++ b/views/album.ejs @@ -32,19 +32,24 @@
">
<%= i==0 ? "Found matches for this link" : "" %>
- <% if (album.url) { %> - + <% if (album.streamUrl) { %> + - <% } else { %> + <% } else if (album.purchaseUrl) { %> + + + <% }else { %> <% } %>
-
+ <% if((i+1)%4 == 0) { %>
<% } %> <% } %>