From aeca3bd1cfe1073bbee76bc6b3bff57c7f61554e Mon Sep 17 00:00:00 2001 From: Cloud Date: Wed, 25 Sep 2019 11:59:00 +0800 Subject: [PATCH] feat: huodongxing get fulltext (#3130) --- lib/routes/hdx/explore.js | 65 +++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/lib/routes/hdx/explore.js b/lib/routes/hdx/explore.js index 6c846a7f55..14a798926d 100644 --- a/lib/routes/hdx/explore.js +++ b/lib/routes/hdx/explore.js @@ -1,5 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const URL = require('url'); module.exports = async (ctx) => { const response = await got({ @@ -10,35 +11,47 @@ module.exports = async (ctx) => { const data = response.data; const $ = cheerio.load(data); - const list = $('div.search-tab-content-item'); + const list = $('div.search-tab-content-item').get(); + + const ProcessFeed = async (link) => { + const response = await got({ + method: 'get', + url: link, + }); + const $ = cheerio.load(response.data); + const content = $('#event_desc_page'); + + return content.html(); + }; + + const host = 'https://www.huodongxing.com'; + + const items = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const $a = $('.item-title'); + const link = URL.resolve(host, $a.attr('href')); + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const single = { + title: $a.text(), + description: await ProcessFeed(link), + link: link, + author: $('.user-name').text(), + }; + + ctx.cache.set(link, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); ctx.state.data = { title: '活动行', link: 'https://www.huodongxing.com/eventlist', - item: - list && - list - .map((index, item) => { - item = $(item); - return { - title: item - .find('.item-title') - .text() - .trim(), - link: item.find('.item-title').attr('href'), - description: - item.find('.item-data').text() + - ' ' + - item - .find('.item-dress') - .text() - .trim(), - author: item - .find('.user-name') - .text() - .trim(), - }; - }) - .get(), + item: items, }; };