From b7770e4483db41194af5fa14deb9afd552e5317d Mon Sep 17 00:00:00 2001 From: Moe Poi ~ Date: Mon, 11 May 2020 17:56:00 +0000 Subject: [PATCH] Optimize getInfo --- NekopoiScrapper/getInfo.js | 47 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/NekopoiScrapper/getInfo.js b/NekopoiScrapper/getInfo.js index 457ee26..f3912b8 100644 --- a/NekopoiScrapper/getInfo.js +++ b/NekopoiScrapper/getInfo.js @@ -7,30 +7,29 @@ const axios = require('axios'); const cheerio = require('cheerio'); -let links = []; - -const getInfo = (url) => { - return new Promise((resolve, reject) => { - axios.get(url).then(function(req) { - let soup = cheerio.load(req.data); - var title = soup("title").text(); - soup('div.liner').each(function(i, e) { - soup(e).find('div.listlink').each(function(j, s) { - links.push(soup(s).find('a').attr('href')) - }); +const getInfo = url => { + return new Promise((resolve, reject) => { + axios.get(url) + .then(req => { + try { + const links = []; + let soup = cheerio.load(req.data); + let title = soup("title").text(); + soup('div.liner').each(function(i, e) { + soup(e).find('div.listlink').each(function(j, s) { + links.push(soup(s).find('a').attr('href')) }); - var data = { - "title": title, - "links": links - }; - if (data == null) { - reject("No result :("); - } else { - var result = JSON.stringify(data, null, 2); - resolve(result); - } - }); - }); + }); + const data = { + "title": title, + "links": links + }; + resolve(data) + } catch (err) { + reject('Error : ' + err) + } + }) + }); }; -module.exports = getInfo; \ No newline at end of file +module.exports = getInfo;