Optimize getInfo

This commit is contained in:
Moe Poi ~ 2020-05-11 17:56:00 +00:00
parent e4e8524b9c
commit b7770e4483

View file

@ -7,30 +7,29 @@
const axios = require('axios'); const axios = require('axios');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
let links = []; const getInfo = url => {
return new Promise((resolve, reject) => {
const getInfo = (url) => { axios.get(url)
return new Promise((resolve, reject) => { .then(req => {
axios.get(url).then(function(req) { try {
let soup = cheerio.load(req.data); const links = [];
var title = soup("title").text(); let soup = cheerio.load(req.data);
soup('div.liner').each(function(i, e) { let title = soup("title").text();
soup(e).find('div.listlink').each(function(j, s) { soup('div.liner').each(function(i, e) {
links.push(soup(s).find('a').attr('href')) soup(e).find('div.listlink').each(function(j, s) {
}); links.push(soup(s).find('a').attr('href'))
}); });
var data = { });
"title": title, const data = {
"links": links "title": title,
}; "links": links
if (data == null) { };
reject("No result :("); resolve(data)
} else { } catch (err) {
var result = JSON.stringify(data, null, 2); reject('Error : ' + err)
resolve(result); }
} })
}); });
});
}; };
module.exports = getInfo; module.exports = getInfo;