From 3060ca63deea7cd77a9d798f745fb58d75caa215 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Mon, 30 Nov 2020 19:30:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=B1=82=E6=98=AF=E7=BD=91=20(#6?= =?UTF-8?q?272)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 12 ++++ lib/router.js | 3 + lib/routes/qstheory/index.js | 117 +++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 lib/routes/qstheory/index.js diff --git a/docs/new-media.md b/docs/new-media.md index 7a4b6cbe4..118a98a17 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1277,6 +1277,18 @@ column 为 third 时可选的 category: +## 求是网 + +### 分类 + + + +| 网评 | 视频 | 原创 | 经济 | 政治 | 文化 | 社会 | 党建 | 科教 | 生态 | 国防 | 国际 | 图书 | 学习笔记 | +| ---- | ---- | ------ | ------- | -------- | ------- | ------- | ---- | ------- | ------- | ------- | ------------- | ----- | -------- | +| qswp | qssp | qslgxd | economy | politics | culture | society | cpc | science | zoology | defense | international | books | xxbj | + + + ## 趣头条 ### 分类 diff --git a/lib/router.js b/lib/router.js index f266c4d46..4a71ff401 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3516,6 +3516,9 @@ router.get('/uisdc/news', require('./routes/uisdc/news')); router.get('/uisdc/zt/:title?', require('./routes/uisdc/zt')); router.get('/uisdc/topic/:title?/:sort?', require('./routes/uisdc/topic')); +// 求是网 +router.get('/qstheory/:category?', require('./routes/qstheory/index')); + // 生命时报 router.get('/lifetimes/:category?', require('./routes/lifetimes/index')); diff --git a/lib/routes/qstheory/index.js b/lib/routes/qstheory/index.js new file mode 100644 index 000000000..16116e196 --- /dev/null +++ b/lib/routes/qstheory/index.js @@ -0,0 +1,117 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const rootUrl = 'http://www.qstheory.cn/'; + +const config = { + toutiao: { + title: '头条', + url: `${rootUrl}/v9zhuanqu/toutiao/index.htm`, + }, + qswp: { + title: '网评', + url: `${rootUrl}/qswp.htm`, + }, + qssp: { + title: '视频', + url: `${rootUrl}/qssp/index.htm`, + }, + qslgxd: { + title: '原创', + url: `${rootUrl}/qslgxd/index.htm`, + }, + economy: { + title: '经济', + url: `${rootUrl}/economy/index.htm`, + }, + politics: { + title: '政治', + url: `${rootUrl}/politics/index.htm`, + }, + culture: { + title: '文化', + url: `${rootUrl}/culture/index.htm`, + }, + society: { + title: '社会', + url: `${rootUrl}/society/index.htm`, + }, + cpc: { + title: '党建', + url: `${rootUrl}/cpc/index.htm`, + }, + science: { + title: '科教', + url: `${rootUrl}/science/index.htm`, + }, + zoology: { + title: '生态', + url: `${rootUrl}/zoology/index.htm`, + }, + defense: { + title: '国防', + url: `${rootUrl}/defense/index.htm`, + }, + international: { + title: '国际', + url: `${rootUrl}/international/index.htm`, + }, + books: { + title: '图书', + url: `${rootUrl}/books/index.htm`, + }, + xxbj: { + title: '学习笔记', + url: `${rootUrl}/qszq/xxbj/index.htm`, + }, +}; + +module.exports = async (ctx) => { + const category = ctx.params.category || 'toutiao'; + + const currentUrl = config[category].url; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.list-style1 ul li a, .text h2 a, .no-pic ul li a') + .slice(0, 15) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: item.attr('href'), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + content('.fs-text, .fs-pinglun, .hidden-xs').remove(); + + item.author = content('.appellation').text(); + item.description = content('.highlight, .text').html() || content('.content').html(); + item.pubDate = new Date(content('.puttime_mobi, .pubtime, .headtitle span').text().replace('发表于', '').replace(/(年|月)/g, '-').replace('日', '')).toUTCString(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};