From e87dd278853044abb0c20b75e8529c6d63d9ee1f Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sat, 6 Dec 2014 21:12:52 +0000 Subject: [PATCH] Make non-us itunes links work --- lib/services/itunes.js | 26 ++++++++++++++++++++------ routes/search.js | 10 +++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/services/itunes.js b/lib/services/itunes.js index 7e219e0..6b4f229 100644 --- a/lib/services/itunes.js +++ b/lib/services/itunes.js @@ -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; } diff --git a/routes/search.js b/routes/search.js index 3994975..dbedcd7 100644 --- a/routes/search.js +++ b/routes/search.js @@ -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; } }