From c10b43bf78ccd54929e372ecf82173c1ff2b3eae Mon Sep 17 00:00:00 2001 From: Toby Tso <7851076+tpnonthealps@users.noreply.github.com> Date: Tue, 29 Dec 2020 01:00:42 +0800 Subject: [PATCH] feat: Caixin latest (#6539) --- docs/traditional-media.md | 8 +++++ lib/router.js | 3 ++ lib/routes/caixin/latest.js | 71 +++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 lib/routes/caixin/latest.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index c9347c4f1..94db156ae 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -311,6 +311,14 @@ Category 列表: +### 最新文章 + + + +说明:此 RSS feed 会自动抓取财新网的最新文章,但不包含 FM 及视频内容。 + + + ## 第一财经 ### 直播区 diff --git a/lib/router.js b/lib/router.js index 2da2ee3d3..e797e1e7f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3766,4 +3766,7 @@ router.get('/sciencenet/blog/:type?/:time?/:sort?', require('./routes/sciencenet // DailyArt router.get('/dailyart/:language?', require('./routes/dailyart/index')); +// Caixin Latest +router.get('/caixin/latest', require('./routes/caixin/latest')); + module.exports = router; diff --git a/lib/routes/caixin/latest.js b/lib/routes/caixin/latest.js new file mode 100644 index 000000000..8b5f9f042 --- /dev/null +++ b/lib/routes/caixin/latest.js @@ -0,0 +1,71 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const li_r = await got({ + method: 'get', + url: 'http://finance.caixin.com/2020-11-03/101622309.html', + }); + + const $ = cheerio.load(li_r.data); + const list = $('div.columnBox a[name="new_artical"]~ul.list li'); + + const tasks = []; + + list.map((_index, li) => $(li).find('a').first().attr('href')) + .filter((_index, link) => link.indexOf('fm.caixin.com') === -1 && link.indexOf('video.caixin.com') === -1) // content filter + .each((_index, link) => { + const entry = ctx.cache.tryGet(link, async () => { + const entry_r = await got.get(link); + const $ = cheerio.load(entry_r.data); + // title + const h1 = $('div#conTit h1').text(); + // desc items + const subhead = $('div#subhead.subhead').text(); + let pic_url = $('img.cx-img-loader').attr('src'); + if (pic_url === undefined) { + pic_url = $('img.cx-img-loader').attr('data-src'); + } + const pic_alt = $('dl.media_pic dd').text(); + const content = $('div#Main_Content_Val.text').html(); + + /* + const [, count] = + $('a.box_titlecontent.cost-uibtn') + .text() + .match(/本文共计(\d+)字/) || []; + if (count !== 0 && count !== undefined) { + content = `${content}

此乃财新通收费文章,全文共计${count}字。

`; + } + */ + // desc + let desc; + if (pic_url === undefined) { + desc = `

${subhead}

${content}`; + } else { + desc = `

${subhead}

${pic_alt}${content}`; + } + // time + const [, year, month, day, hour, minute] = $('div#artInfo.artInfo') + .text() + .match(/(\d+)年(\d+)月(\d+)日 *(\d+):(\d+)/); + const time = new Date(`${year}-${month}-${day}T${hour}:${minute}:00+0800`).toUTCString(); + + return { + title: h1, + description: desc, + pubDate: time, + link: link, + }; + }); + tasks.push(entry); + }); + + const rss = await Promise.all(tasks); + + ctx.state.data = { + title: '财新网 - 最新文章', + link: 'http://www.caixin.com/', + item: rss, + }; +};