Add support for iTunes purchases

This commit is contained in:
Jonathan Cremin 2014-12-04 21:11:55 +00:00
parent 1b2c568ac6
commit da5daeeffb
9 changed files with 140 additions and 15 deletions

View file

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

View file

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

View file

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

106
lib/services/itunes.js Normal file
View file

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

View file

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

View file

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

BIN
public/images/itunes.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

View file

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

View file

@ -32,19 +32,24 @@
<div class="col-md-3 col-xs-6">
<div class="service <%= i==0 ? "source-service" : "" %>">
<div class="matching-from"><%= i==0 ? "Found matches for this link" : "" %></div>
<% if (album.url) { %>
<a href="<%= album.url %>"><img src="<%= album.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<% if (album.streamUrl) { %>
<a href="<%= album.streamUrl %>"><img src="<%= album.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<div class="service-link">
<a href="<%= album.url %>"><img src="/images/<%= album.service %>.png" class="img-rounded"></a>
<a href="<%= album.streamUrl %>"><img src="/images/<%= album.service %>.png" class="img-rounded"></a>
</div>
<% } else { %>
<% } else if (album.purchaseUrl) { %>
<a href="<%= album.purchaseUrl %>"><img src="<%= album.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<div class="service-link">
<a href="<%= album.purchaseUrl %>"><img src="/images/<%= album.service %>.png" class="img-rounded"></a>
</div>
<% }else { %>
<img src="<%= items[0].artwork %>" class="img-rounded album-artwork not-found" width="100%"></a>
<div class="service-link">
<img src="/images/<%= album.service %>.png" class="img-rounded not-found">
</div>
<% } %>
</div>
</div>
</div><% if((i+1)%4 == 0) { %></div><div class="row"><% } %>
<% } %>
</div>
<div class="row share">