From 377c2c4d57de89efe065a2c55e7daf63753c671c Mon Sep 17 00:00:00 2001 From: kdan Date: Mon, 25 Apr 2022 10:37:48 +0800 Subject: [PATCH] feat(route): add chinafactcheck (#9611) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add(route): 首席经济学家论坛 * fix(route) : merge from FledgeXu * fix(route) : merge from FledgeXu * (fix) typo * feat(route): remove radar from radar-rules.js * Update lib/v2/chinacef/hot.js Co-authored-by: Tony * Update lib/v2/chinacef/router.js Co-authored-by: Tony * Update lib/v2/chinacef/maintainer.js Co-authored-by: Tony * Update lib/v2/chinacef/experts.js Co-authored-by: Tony * Update lib/v2/chinacef/experts.js Co-authored-by: Tony * Update hot.js * Update experts.js * docs: fix Route's path and example of chinacef * Revert "docs: fix Route's path and example of chinacef" This reverts commit 0ec9e7d9d6d1eff201482e38a26b255478861a5c. * docs: fix Route's path and example of chinacef * feat(route): add chinafactcheck Co-authored-by: linkai Co-authored-by: Tony --- docs/other.md | 6 ++ lib/v2/chinafactcheck/index.js | 38 ++++++++++++ lib/v2/chinafactcheck/maintainer.js | 3 + lib/v2/chinafactcheck/radar.js | 13 ++++ lib/v2/chinafactcheck/router.js | 3 + .../chinafactcheck/templates/description.art | 1 + lib/v2/chinafactcheck/utils.js | 60 +++++++++++++++++++ 7 files changed, 124 insertions(+) create mode 100644 lib/v2/chinafactcheck/index.js create mode 100644 lib/v2/chinafactcheck/maintainer.js create mode 100644 lib/v2/chinafactcheck/radar.js create mode 100644 lib/v2/chinafactcheck/router.js create mode 100644 lib/v2/chinafactcheck/templates/description.art create mode 100644 lib/v2/chinafactcheck/utils.js diff --git a/docs/other.md b/docs/other.md index 83031aff7..74000e038 100644 --- a/docs/other.md +++ b/docs/other.md @@ -971,6 +971,12 @@ type 为 all 时,category 参数不支持 cost 和 free +## 有据 + +### 最新文章列表 + + + ## 源仓库 ### 源仓库更新 diff --git a/lib/v2/chinafactcheck/index.js b/lib/v2/chinafactcheck/index.js new file mode 100644 index 000000000..86ccfa262 --- /dev/null +++ b/lib/v2/chinafactcheck/index.js @@ -0,0 +1,38 @@ +const utils = require('./utils'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const rssTitle = '有据|国际新闻事实核查'; + + const response = await got('https://chinafactcheck.com'); + const $ = cheerio.load(response.data); + + const articlesLinkListNode = $('.post-info-box .post-thumb a') + .map((_, item) => $(item).attr('href')) + .get(); + const articlesLinkList = Array.from(new Set(articlesLinkListNode)); + + const articles = await Promise.all( + articlesLinkList.map(async (item) => { + const articlesLink = item; + + const detail = await ctx.cache.tryGet(articlesLink, async () => await utils.getArticleDetail(articlesLink)); + + const element = { + title: detail.title, + author: detail.author, + pubDate: detail.time, + description: detail.description, + link: articlesLink, + }; + return element; + }) + ); + + ctx.state.data = { + title: rssTitle, + link: utils.siteLink, + item: articles, + }; +}; diff --git a/lib/v2/chinafactcheck/maintainer.js b/lib/v2/chinafactcheck/maintainer.js new file mode 100644 index 000000000..414dc1939 --- /dev/null +++ b/lib/v2/chinafactcheck/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/': ['kdanfly'], +}; diff --git a/lib/v2/chinafactcheck/radar.js b/lib/v2/chinafactcheck/radar.js new file mode 100644 index 000000000..3a8208f38 --- /dev/null +++ b/lib/v2/chinafactcheck/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'chinafactcheck.com': { + _name: '有据', + '.': [ + { + title: '最新文章列表', + docs: 'https://docs.rsshub.app/other.html#you-ju-zui-xin-wen-zhang-lie-biao', + source: ['/'], + target: '/chinafactcheck', + }, + ], + }, +}; diff --git a/lib/v2/chinafactcheck/router.js b/lib/v2/chinafactcheck/router.js new file mode 100644 index 000000000..20c52b09d --- /dev/null +++ b/lib/v2/chinafactcheck/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/', require('./index')); +}; diff --git a/lib/v2/chinafactcheck/templates/description.art b/lib/v2/chinafactcheck/templates/description.art new file mode 100644 index 000000000..29add8424 --- /dev/null +++ b/lib/v2/chinafactcheck/templates/description.art @@ -0,0 +1 @@ +{{@ desc.article }} diff --git a/lib/v2/chinafactcheck/utils.js b/lib/v2/chinafactcheck/utils.js new file mode 100644 index 000000000..6b3d18312 --- /dev/null +++ b/lib/v2/chinafactcheck/utils.js @@ -0,0 +1,60 @@ +const { art } = require('@/utils/render'); +const path = require('path'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +const siteLink = 'https://chinafactcheck.com'; + +const renderDesc = (desc) => + art(path.join(__dirname, 'templates/description.art'), { + desc, + }); + +const cleanDom = (dom) => { + dom('*[style]').removeAttr('style'); + dom('br').remove(); + dom('span:empty').remove(); + dom('span').each((_, el) => { + if (dom(el).html().trim() === ' ') { + dom(el).remove(); + } + }); + dom('p:empty').remove(); + dom('p').each((_, el) => { + if (dom(el).html().trim() === '') { + dom(el).remove(); + } + }); + return dom; +}; + +const getArticleDetail = async (link) => { + const response = await got(link); + const $ = cleanDom(cheerio.load(response.data)); + + const title = $('.content-head h2').text().trim(); + const author = $('.content-persons p span:last').text().trim(); + const time = parseDate($('.content-time').text().trim(), 'YYYY-MM-DD'); + + const description = { + article: $('div[class=content-list-box]').html(), + }; + return new ArticleDetail(title, author, time, renderDesc(description)); +}; +class ArticleDetail { + constructor(title, author, time, description) { + this.title = title; + this.author = author; + this.time = time; + this.description = description; + } +} + +module.exports = { + siteLink, + renderDesc, + cleanDom, + getArticleDetail, + ArticleDetail, +};