Make non-us itunes links work

This commit is contained in:
Jonathan Cremin 2014-12-06 21:12:52 +00:00
parent a471a984f8
commit e87dd27885
2 changed files with 27 additions and 9 deletions

View file

@ -12,9 +12,21 @@ module.exports.match = function(url, type) {
return parsed.host.match(/itunes.apple\.com$/);
};
module.exports.lookupId = function(id, type) {
module.exports.lookupId = function(id, type, cc) {
var deferred = Q.defer();
if (id.match(/^[a-z]{2}/)) {
cc = id.substr(0,2);
id = id.substr(2);
console.log(id)
}
var path = "/lookup?id=" + id;
if (cc) {
path = "/" + cc + path;
}
console.log(path)
request.get(apiRoot + path, function(res) {
var data = JSON.parse(res.text);
@ -29,7 +41,7 @@ module.exports.lookupId = function(id, type) {
var item = {
service: "itunes",
type: type,
id: result.collectionId,
id: cc + result.collectionId,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
@ -89,7 +101,7 @@ module.exports.search = function(data) {
var item = {
service: "itunes",
type: type,
id: result.collectionId,
id: "us" + result.collectionId,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
@ -113,10 +125,12 @@ 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(/[\/]?([\/]?[a-z]{2}?)?\/(album|track)[\/]+([^\/]+)[\/]+([^\?]+)/);
if (matches && matches[3]) {
module.exports.lookupId(matches[3].substr(2), matches[1]).then(deferred.resolve);
if (matches && matches[4]) {
module.exports.lookupId(matches[4].substr(2), matches[2], matches[1]).then(deferred.resolve, deferred.reject);
} else if (matches[3]) {
module.exports.lookupId(matches[3].substr(2), matches[1], "").then(deferred.resolve, deferred.reject);
}
return deferred.promise;
}