iTunes tracks, iTunes url lookups

This commit is contained in:
Jonathan Cremin 2014-12-04 21:53:50 +00:00
parent da5daeeffb
commit dcca32c998
6 changed files with 110 additions and 99 deletions

View file

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

View file

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

View file

@ -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("/", "");

View file

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

View file

@ -24,7 +24,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h2><%= items[0].artist.name %> - <%= items[0].name %></h2>
<h2><%= items[0].artist.name %> - <%= items[0].name %></h2>
</div>
</div>
<div class="row">
@ -33,20 +33,20 @@
<div class="service <%= i==0 ? "source-service" : "" %>">
<div class="matching-from"><%= i==0 ? "Found matches for this link" : "" %></div>
<% 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.streamUrl %>"><img src="/images/<%= album.service %>.png" class="img-rounded"></a>
</div>
<a href="<%= album.streamUrl %>"><img src="<%= album.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<div class="service-link">
<a href="<%= album.streamUrl %>"><img src="/images/<%= album.service %>.png" class="img-rounded"></a>
</div>
<% } 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>
<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><% if((i+1)%4 == 0) { %></div><div class="row"><% } %>

View file

@ -29,27 +29,32 @@
</div>
<div class="row">
<% for (var i=0;i < items.length;i++) { var track = items[i]; %>
<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 (track.url) { %>
<a href="<%= track.url %>"><img src="<%= track.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<div class="service-link">
<a href="<%= track.url %>"><img src="/images/<%= track.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/<%= track.service %>.png" class="img-rounded not-found">
</div>
<% } %>
<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 (track.streamUrl) { %>
<a href="<%= track.streamUrl %>"><img src="<%= track.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<div class="service-link">
<a href="<%= track.streamUrl %>"><img src="/images/<%= track.service %>.png" class="img-rounded"></a>
</div>
<% } else if (track.purchaseUrl) { %>
<a href="<%= track.purchaseUrl %>"><img src="<%= track.artwork %>" class="img-rounded album-artwork" width="100%"></a>
<div class="service-link">
<a href="<%= track.purchaseUrl %>"><img src="/images/<%= track.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/<%= track.service %>.png" class="img-rounded not-found">
</div>
<% } %>
</div>
<% } %>
</div>
<div class="row share">
</div>
</div><% if((i+1)%4 == 0) { %></div><div class="row"><% } %>
<% } %>
</div>
</body>
</html>
<div class="row share">
</div>
</div>
</body>
</html>