diff --git a/fixmissing.js b/fixmissing.js index ac8b5ab..13a2040 100644 --- a/fixmissing.js +++ b/fixmissing.js @@ -20,7 +20,7 @@ const query = { { model: models.match }, ], order: [ - ['updatedAt', 'DESC'], + ['createdAt', 'DESC'], ], }; diff --git a/lib/services/amazon/index.js b/lib/services/amazon/index.js index f42dc05..c18c66a 100644 --- a/lib/services/amazon/index.js +++ b/lib/services/amazon/index.js @@ -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' }; } diff --git a/worker.js b/worker.js index 0ebc273..bf46725 100644 --- a/worker.js +++ b/worker.js @@ -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); }); }