Fix itunes matching

This commit is contained in:
Jonathan Cremin 2017-10-23 22:47:14 +01:00
parent 62b7121301
commit 7b36e8bf0c
6 changed files with 91 additions and 38 deletions

View file

@ -11,14 +11,19 @@ export function* parseUrl(url) {
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
const query = querystring.parse(parsed.query);
let itunesId = matches[3];
if (matches) {
let type = 'album';
let id = matches[3].substr(2);
if (query.i) {
type = 'track';
id = query.i;
if (matches[3].match(/^id/)) {
itunesId = matches[3].substr(2);
if (query.i) {
type = 'track';
itunesId = query.i;
}
}
return yield module.exports.lookupId(id, type, matches[1] || 'us');
return yield module.exports.lookupId(itunesId, type, matches[1] || 'us');
}
throw new Error();
}
@ -30,15 +35,14 @@ export function* lookupId(possibleId, type, countrycode) {
cc = possibleId.substr(0, 2);
id = possibleId.substr(2);
}
console.log(id)
let path = `/lookup?id=${id}`;
if (cc) {
path = `/${cc}${path}`;
}
const response = yield request.get(apiRoot + path);
let result = JSON.parse(response.text);
console.log(result);
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
const error = new Error('Not Found');
error.status = 404;