Handle errors in Amazon API
This commit is contained in:
parent
b0fcf55071
commit
3428986268
3 changed files with 70 additions and 58 deletions
|
@ -20,7 +20,7 @@ const query = {
|
|||
{ model: models.match },
|
||||
],
|
||||
order: [
|
||||
['updatedAt', 'DESC'],
|
||||
['createdAt', 'DESC'],
|
||||
],
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import { parse } from 'url';
|
||||
import { inspect } from 'util';
|
||||
import amazon from 'amazon-product-api';
|
||||
import debuglog from 'debug';
|
||||
import urlMatch from './url';
|
||||
|
||||
const debug = debuglog('combine.fm:youtube');
|
||||
|
||||
|
||||
const client = amazon.createClient({
|
||||
awsId: process.env.AWS_ACCESS_KEY_ID,
|
||||
awsSecret: process.env.AWS_SECRET_ACCESS_KEY,
|
||||
|
@ -10,70 +14,76 @@ const client = amazon.createClient({
|
|||
});
|
||||
|
||||
export function* lookupId(id, type) {
|
||||
try {
|
||||
const results = yield client.itemLookup({
|
||||
itemId: id,
|
||||
responseGroup: 'ItemAttributes,Images,ItemIds',
|
||||
});
|
||||
|
||||
const results = yield client.itemLookup({
|
||||
itemId: id,
|
||||
responseGroup: 'ItemAttributes,Images,ItemIds',
|
||||
});
|
||||
const result = results[0];
|
||||
|
||||
const result = results[0];
|
||||
if (!result || !result.Error) {
|
||||
return { service: 'amazon' };
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
return { service: 'amazon' };
|
||||
}
|
||||
|
||||
if (type === 'album') {
|
||||
return {
|
||||
service: 'amazon',
|
||||
type: 'album',
|
||||
id: result.ASIN[0],
|
||||
name: result.ItemAttributes[0].Title[0],
|
||||
streamUrl: result.DetailPageURL[0],
|
||||
purchaseUrl: result.DetailPageURL[0],
|
||||
artwork: {
|
||||
small: result.SmallImage[0].URL[0],
|
||||
large: result.LargeImage[0].URL[0],
|
||||
},
|
||||
artist: {
|
||||
name: result.ItemAttributes[0].Creator[0]._,
|
||||
},
|
||||
};
|
||||
} else if (type === 'track') {
|
||||
return {
|
||||
service: 'amazon',
|
||||
type: 'track',
|
||||
id: result.ASIN[0],
|
||||
name: result.ItemAttributes[0].Title[0],
|
||||
streamUrl: result.DetailPageURL[0],
|
||||
purchaseUrl: result.DetailPageURL[0],
|
||||
artwork: {
|
||||
small: result.SmallImage[0].URL[0],
|
||||
large: result.LargeImage[0].URL[0],
|
||||
},
|
||||
album: {
|
||||
name: result.ItemAttributes[0],
|
||||
},
|
||||
artist: {
|
||||
name: result.ItemAttributes[0].Creator[0]._,
|
||||
},
|
||||
};
|
||||
if (type === 'album') {
|
||||
return {
|
||||
service: 'amazon',
|
||||
type: 'album',
|
||||
id: result.ASIN[0],
|
||||
name: result.ItemAttributes[0].Title[0],
|
||||
streamUrl: result.DetailPageURL[0],
|
||||
purchaseUrl: result.DetailPageURL[0],
|
||||
artwork: {
|
||||
small: result.SmallImage[0].URL[0],
|
||||
large: result.LargeImage[0].URL[0],
|
||||
},
|
||||
artist: {
|
||||
name: result.ItemAttributes[0].Creator[0]._,
|
||||
},
|
||||
};
|
||||
} else if (type === 'track') {
|
||||
return {
|
||||
service: 'amazon',
|
||||
type: 'track',
|
||||
id: result.ASIN[0],
|
||||
name: result.ItemAttributes[0].Title[0],
|
||||
streamUrl: result.DetailPageURL[0],
|
||||
purchaseUrl: result.DetailPageURL[0],
|
||||
artwork: {
|
||||
small: result.SmallImage[0].URL[0],
|
||||
large: result.LargeImage[0].URL[0],
|
||||
},
|
||||
album: {
|
||||
name: result.ItemAttributes[0],
|
||||
},
|
||||
artist: {
|
||||
name: result.ItemAttributes[0].Creator[0]._,
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
debug(inspect(err, { depth: 4 }));
|
||||
}
|
||||
return { service: 'amazon' };
|
||||
}
|
||||
|
||||
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 results = yield client.itemSearch({
|
||||
author: data.artist.name,
|
||||
title: data.name,
|
||||
searchIndex: 'MP3Downloads',
|
||||
responseGroup: 'ItemAttributes,Tracks,Images,ItemIds',
|
||||
});
|
||||
const result = results[0];
|
||||
|
||||
const result = results[0];
|
||||
if (!result || !result.Error) {
|
||||
return { service: 'amazon' };
|
||||
}
|
||||
|
||||
if (result) {
|
||||
if (type === 'album') {
|
||||
return {
|
||||
service: 'amazon',
|
||||
|
@ -110,8 +120,9 @@ export function* search(data, original = {}) {
|
|||
},
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
debug(inspect(err, { depth: 4 }));
|
||||
}
|
||||
|
||||
return { service: 'amazon' };
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import co from 'co';
|
|||
import kue from 'kue';
|
||||
import raven from 'raven';
|
||||
import debuglog from 'debug';
|
||||
import { inspect } from 'util';
|
||||
|
||||
import models from './models';
|
||||
import services from './lib/services';
|
||||
|
@ -51,16 +52,16 @@ function search(data, done) {
|
|||
} catch (err) {
|
||||
debug(`Error searching on: ${service.id}`);
|
||||
debug(share);
|
||||
debug(err);
|
||||
debug(inspect(err, { depth: 5 }));
|
||||
raven.captureException(err);
|
||||
return done(err);
|
||||
}
|
||||
}).catch((err) => {
|
||||
debug(`Error searching on: ${service.id}`);
|
||||
debug(share);
|
||||
debug(err);
|
||||
debug(inspect(err, { depth: 5 }));
|
||||
raven.captureException(err);
|
||||
return done();
|
||||
return done(err);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue