Handle errors in Amazon API

This commit is contained in:
Jonathan Cremin 2018-04-08 17:22:19 +01:00
parent b0fcf55071
commit 3428986268
3 changed files with 70 additions and 58 deletions

View file

@ -20,7 +20,7 @@ const query = {
{ model: models.match }, { model: models.match },
], ],
order: [ order: [
['updatedAt', 'DESC'], ['createdAt', 'DESC'],
], ],
}; };

View file

@ -1,8 +1,12 @@
import { parse } from 'url'; import { parse } from 'url';
import { inspect } from 'util'; import { inspect } from 'util';
import amazon from 'amazon-product-api'; import amazon from 'amazon-product-api';
import debuglog from 'debug';
import urlMatch from './url'; import urlMatch from './url';
const debug = debuglog('combine.fm:youtube');
const client = amazon.createClient({ const client = amazon.createClient({
awsId: process.env.AWS_ACCESS_KEY_ID, awsId: process.env.AWS_ACCESS_KEY_ID,
awsSecret: process.env.AWS_SECRET_ACCESS_KEY, awsSecret: process.env.AWS_SECRET_ACCESS_KEY,
@ -10,70 +14,76 @@ const client = amazon.createClient({
}); });
export function* lookupId(id, type) { export function* lookupId(id, type) {
try {
const results = yield client.itemLookup({
itemId: id,
responseGroup: 'ItemAttributes,Images,ItemIds',
});
const results = yield client.itemLookup({ const result = results[0];
itemId: id,
responseGroup: 'ItemAttributes,Images,ItemIds',
});
const result = results[0]; if (!result || !result.Error) {
return { service: 'amazon' };
}
if (!result) { if (type === 'album') {
return { service: 'amazon' }; return {
} service: 'amazon',
type: 'album',
if (type === 'album') { id: result.ASIN[0],
return { name: result.ItemAttributes[0].Title[0],
service: 'amazon', streamUrl: result.DetailPageURL[0],
type: 'album', purchaseUrl: result.DetailPageURL[0],
id: result.ASIN[0], artwork: {
name: result.ItemAttributes[0].Title[0], small: result.SmallImage[0].URL[0],
streamUrl: result.DetailPageURL[0], large: result.LargeImage[0].URL[0],
purchaseUrl: result.DetailPageURL[0], },
artwork: { artist: {
small: result.SmallImage[0].URL[0], name: result.ItemAttributes[0].Creator[0]._,
large: result.LargeImage[0].URL[0], },
}, };
artist: { } else if (type === 'track') {
name: result.ItemAttributes[0].Creator[0]._, return {
}, service: 'amazon',
}; type: 'track',
} else if (type === 'track') { id: result.ASIN[0],
return { name: result.ItemAttributes[0].Title[0],
service: 'amazon', streamUrl: result.DetailPageURL[0],
type: 'track', purchaseUrl: result.DetailPageURL[0],
id: result.ASIN[0], artwork: {
name: result.ItemAttributes[0].Title[0], small: result.SmallImage[0].URL[0],
streamUrl: result.DetailPageURL[0], large: result.LargeImage[0].URL[0],
purchaseUrl: result.DetailPageURL[0], },
artwork: { album: {
small: result.SmallImage[0].URL[0], name: result.ItemAttributes[0],
large: result.LargeImage[0].URL[0], },
}, artist: {
album: { name: result.ItemAttributes[0].Creator[0]._,
name: result.ItemAttributes[0], },
}, };
artist: { }
name: result.ItemAttributes[0].Creator[0]._, } catch (err) {
}, debug(inspect(err, { depth: 4 }));
};
} }
return { service: 'amazon' }; return { service: 'amazon' };
} }
export function* search(data, original = {}) { export function* search(data, original = {}) {
try {
const type = data.type;
const results = yield client.itemSearch({
author: data.artist.name.normalize(),
title: data.name.normalize(),
searchIndex: 'MP3Downloads',
responseGroup: 'ItemAttributes,Tracks,Images,ItemIds',
});
const type = data.type; const result = results[0];
const results = yield client.itemSearch({
author: data.artist.name,
title: data.name,
searchIndex: 'MP3Downloads',
responseGroup: 'ItemAttributes,Tracks,Images,ItemIds',
});
const result = results[0]; if (!result || !result.Error) {
return { service: 'amazon' };
}
if (result) {
if (type === 'album') { if (type === 'album') {
return { return {
service: 'amazon', service: 'amazon',
@ -110,8 +120,9 @@ export function* search(data, original = {}) {
}, },
}; };
} }
} catch (err) {
debug(inspect(err, { depth: 4 }));
} }
return { service: 'amazon' }; return { service: 'amazon' };
} }

View file

@ -2,6 +2,7 @@ import co from 'co';
import kue from 'kue'; import kue from 'kue';
import raven from 'raven'; import raven from 'raven';
import debuglog from 'debug'; import debuglog from 'debug';
import { inspect } from 'util';
import models from './models'; import models from './models';
import services from './lib/services'; import services from './lib/services';
@ -51,16 +52,16 @@ function search(data, done) {
} catch (err) { } catch (err) {
debug(`Error searching on: ${service.id}`); debug(`Error searching on: ${service.id}`);
debug(share); debug(share);
debug(err); debug(inspect(err, { depth: 5 }));
raven.captureException(err); raven.captureException(err);
return done(err); return done(err);
} }
}).catch((err) => { }).catch((err) => {
debug(`Error searching on: ${service.id}`); debug(`Error searching on: ${service.id}`);
debug(share); debug(share);
debug(err); debug(inspect(err, { depth: 5 }));
raven.captureException(err); raven.captureException(err);
return done(); return done(err);
}); });
} }