Skip to content

Commit

Permalink
Return multiple results from the api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovyerus committed Sep 27, 2017
1 parent 41f9e90 commit 335a181
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lib/Sagiri.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @file API wrapper for SauceNAO, capable of submitting files and urls.
* @author Capuccino
* @author Ovyerus
* @todo Finish off url list and make lib return as many results as wanted
*/

const FormData = require('form-data');
Expand Down Expand Up @@ -166,22 +165,24 @@ class SauceHandler {
sendForm(form).then(res => {
if (res.header.status !== 0) throw new Error('An error occurred. (will be more info soon)');

res = res.results;
let result;
if (res.results.length === 0) throw new Error('No results.');

if (res.length > 1) result = res.sort((a, b) => Number(b.header.similarity) - Number(a.header.similarity))[0];
else if (res.length === 1) result = res[0];
else throw new Error('No results.');
let results = res.results.sort((a, b) => Number(b.header.similarity) - Number(a.header.similarity));
let returnData = [];

let data = resolveSauceData(result);
for (let result of results) {
let data = resolveSauceData(result);

return {
url: data.url,
site: data.name,
index: data.id,
similarity: Number(result.header.similarity),
original: result
};
returnData.push({
url: data.url,
site: data.name,
index: data.id,
similarity: Number(result.header.similarity),
original: result
});
}

return returnData;
}).then(resolve).catch(reject);
}
});
Expand Down

0 comments on commit 335a181

Please sign in to comment.