diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 0830a5f2c..e91dc64f4 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -308,6 +308,12 @@ category 对应的关键词有 +## 文汇报 + +### 分类 + + + ## 香港 01 ### 热门 diff --git a/lib/router.js b/lib/router.js index 5a6bea433..a26214957 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1935,6 +1935,9 @@ router.get('/gov/cnca/hydt', require('./routes/gov/cnca/hydt')); router.get('/gov/cnca/zxtz', require('./routes/gov/cnca/zxtz')); +// 文汇报 +router.get('/whb/:category', require('./routes/whb/zhuzhan')); + // 三界异次元 router.get('/3ycy/home', require('./routes/3ycy/home.js')); diff --git a/lib/routes/whb/zhuzhan.js b/lib/routes/whb/zhuzhan.js new file mode 100644 index 000000000..db3a2b06f --- /dev/null +++ b/lib/routes/whb/zhuzhan.js @@ -0,0 +1,77 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const category = ctx.params.category; + + const base = 'http://www.whb.cn'; + const url = `${base}/zhuzhan/${category}/index.html`; + + const list_response = await got.get(url); + const $ = cheerio.load(list_response.data); + + const category_name = $('.title_jingpinhui > a') + .first() + .text(); + const list = $('.info_jingpinhui').toArray(); + + const parseContent = (htmlString) => { + const $ = cheerio.load(htmlString); + + const date_and_author = $('.content_other').text(); + + const matched_date = /(\d+)年(\d+)月(\d+)日 (\d+):(\d+):(\d+)/.exec(date_and_author); + const date = new Date(parseInt(matched_date[1]), parseInt(matched_date[2]) - 1, parseInt(matched_date[3]), parseInt(matched_date[4]), parseInt(matched_date[5]), parseInt(matched_date[6])); + + const author = date_and_author.split(':').slice(-1)[0]; + + const content = $('.content_info'); + + return { + author: author, + description: content.html(), + pubDate: date, + }; + }; + + const out = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const title = $('.title > a'); + const path = title.attr('href'); + const link = `${base}${path}`; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const rssitem = { + title: title.text().trim(), + link: link, + }; + + try { + const response = await got.get(link); + const result = parseContent(response.data); + if (!result) { + return Promise.resolve(''); + } + + rssitem.author = result.author; + rssitem.description = result.description; + rssitem.pubDate = result.pubDate; + } catch (err) { + return Promise.resolve(''); + } + ctx.cache.set(link, JSON.stringify(rssitem)); + return Promise.resolve(rssitem); + }) + ); + + ctx.state.data = { + title: `文汇报 - ${category_name}`, + link: url, + item: out.filter((item) => item !== ''), + }; +};