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

View file

@ -12,7 +12,7 @@ require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach
}
});
module.exports = function(req, res) {
module.exports = function(req, res, next) {
var url = parse(req.body.url);
var searching = false;
@ -35,13 +35,17 @@ module.exports = function(req, res) {
if (error.code == "ETIMEDOUT") {
error = new Error("Error talking to music service");
error.status = "502";
next(error);
} else if (!error.status) {
error = new Error("An unexpected error happenend");
error.status = 500;
next(error);
} else if (error.status == 404){
req.flash('search-error', 'No match found for this link');
res.redirect('/');
}
next(error);
});
});
break;
}
}