diff --git a/package.json b/package.json index 5634e2a..6db1010 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "hasbin": "^1.2.0", "lodash": "^4.0.0", "ncp": "^2.0.0", + "page-icon": "^0.1.0", "request": "^2.67.0", "sanitize-filename": "^1.5.3", "shelljs": "^0.5.3", diff --git a/src/infer/inferIcon.js b/src/infer/inferIcon.js index 6e482dd..bdd5dac 100644 --- a/src/infer/inferIcon.js +++ b/src/infer/inferIcon.js @@ -1,4 +1,4 @@ -import request from 'request'; +import pageIcon from 'page-icon'; import path from 'path'; import fs from 'fs'; import tmp from 'tmp'; @@ -14,34 +14,13 @@ const BEST_ICON_API = 'http://45.55.116.63:8080/icon'; */ function inferIconFromUrlToPath(targetUrl, outDir, callback) { const outfilePath = path.join(outDir, '/icon.png'); - request({ - url: BEST_ICON_API, - qs: { - url: targetUrl, - size: 57, - formats: 'png' - }, - encoding: null - }, (error, response, body) => { - if (error) { - callback(error); - return; - } - - try { - const parsedError = JSON.parse(body).error; - callback(parsedError); - } catch (exception) { - if (//i.test(body)) { - callback('BestIcon server 502 error'); - return; - } - // body is an image - fs.writeFile(outfilePath, body, error => { + pageIcon(targetUrl, {ext: 'png'}) + .then(icon => { + fs.writeFile(outfilePath, icon.data, error => { callback(error, outfilePath); }); - } - }); + }) + .catch(callback); } /**