From a54608cba273284d45b0bf5a9482acc4cbce2706 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 2 Feb 2020 15:36:03 +0800 Subject: [PATCH] fix: /coronavirus/qq/fact --- lib/router.js | 2 +- lib/routes/coronavirus/qq-fact.js | 42 ---------------- lib/routes/tencent/factcheck/index.js | 70 ++++++++++----------------- 3 files changed, 26 insertions(+), 88 deletions(-) delete mode 100644 lib/routes/coronavirus/qq-fact.js diff --git a/lib/router.js b/lib/router.js index 0f3c0fefc9..1b2f3b141e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2162,7 +2162,7 @@ router.get('/coronavirus/dxy', require('./routes/coronavirus/dxy')); router.get('/coronavirus/scmp', require('./routes/coronavirus/scmp')); router.get('/coronavirus/nhc', require('./routes/coronavirus/nhc')); router.get('/coronavirus/mogov-2019ncov/:lang', require('./routes/coronavirus/mogov-2019ncov')); -router.get('/coronavirus/qq/fact', require('./routes/coronavirus/qq-fact')); +router.get('/coronavirus/qq/fact', require('./routes/tencent/factcheck')); // 南京林业大学教务处 router.get('/njfu/jwc/:category?', require('./routes/universities/njfu/jwc')); diff --git a/lib/routes/coronavirus/qq-fact.js b/lib/routes/coronavirus/qq-fact.js deleted file mode 100644 index dca2e7f860..0000000000 --- a/lib/routes/coronavirus/qq-fact.js +++ /dev/null @@ -1,42 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const response = await got({ - method: 'get', - url: 'https://vp.fact.qq.com/loadmore?artnum=0&page=0', - headers: { - Referer: 'https://vp.fact.qq.com/home', - }, - }); - - const data = response.data.content; - - const items = await Promise.all( - (data || []).map(async (item) => { - const link = `https://vp.fact.qq.com/article?id=${item.id}`; - const simple = { - title: `【${item.explain}】${item.title}`, - description: `${item.abstract}`, - pubDate: new Date(item.date).toUTCString(), - author: item.author, - link: link, - }; - - const details = await ctx.cache.tryGet(link, async () => { - const response = await got.get(link); - const $ = cheerio.load(response.data); - return { - description: `${$('.check_content_points').html()}`, - }; - }); - return Promise.resolve(Object.assign({}, simple, details)); - }) - ); - - ctx.state.data = { - title: '新型冠状病毒肺炎实时辟谣-腾讯新闻', - link: 'https://vp.fact.qq.com/home', - item: items, - }; -}; diff --git a/lib/routes/tencent/factcheck/index.js b/lib/routes/tencent/factcheck/index.js index 7ecbf620ac..2fc7ff1902 100644 --- a/lib/routes/tencent/factcheck/index.js +++ b/lib/routes/tencent/factcheck/index.js @@ -2,61 +2,41 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); module.exports = async (ctx) => { - const url = 'https://vp.fact.qq.com/loadmore?artnum=0&page=0&callback='; + const response = await got({ + method: 'get', + url: 'https://vp.fact.qq.com/loadmore?artnum=0&page=0', + headers: { + Referer: 'https://vp.fact.qq.com/home', + }, + }); - const list_res = await got.get(url); - const list = list_res.data.content; + const data = response.data.content; - const parseContent = (htmlString) => { - const $ = cheerio.load(htmlString); - let content = cheerio.load('
'); - content = content('div'); - $('.subtitle.text').appendTo(content); - $('.check_content.text').appendTo(content); - $('.information').appendTo(content); - content.find('.point_num').remove(); - content.find('.check_content_bottom').remove(); - content.find('li').removeAttr('style'); - return { - description: content.html(), - }; - }; - - const out = await Promise.all( - list.map(async (item) => { - const title = `${item.result}/${item.explain} - ${item.title}`; + const items = await Promise.all( + (data || []).map(async (item) => { const link = `https://vp.fact.qq.com/article?id=${item.id}`; - - const cache = await ctx.cache.get(link); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const rssitem = { - title: title, - link: link, + const simple = { + title: `【${item.explain}】${item.title}`, + description: `${item.abstract}`, + pubDate: new Date(item.date).toUTCString(), author: item.author, - pubDate: new Date(item.date), + link: link, }; - try { + const details = await ctx.cache.tryGet(link, async () => { const response = await got.get(link); - const result = parseContent(response.data); - if (!result.description) { - return Promise.resolve(''); - } - rssitem.description = result.description; - } catch (err) { - return Promise.resolve(''); - } - ctx.cache.set(link, JSON.stringify(rssitem)); - return Promise.resolve(rssitem); + const $ = cheerio.load(response.data); + return { + description: `${$('.check_content_points').html()}`, + }; + }); + return Promise.resolve(Object.assign({}, simple, details)); }) ); ctx.state.data = { - title: '腾讯新闻较真查证平台', - link: 'https://vp.fact.qq.com/', - item: out.filter((item) => item !== ''), + title: '较真查证平台-腾讯新闻', + link: 'https://vp.fact.qq.com/home', + item: items, }; };