From 1c6a4acbb8aa9a7b2b4ccd4ad97537183b866798 Mon Sep 17 00:00:00 2001 From: 1813927768 Date: Mon, 1 Jun 2020 12:08:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E7=99=BE=E5=BA=A6=E7=9F=A5?= =?UTF-8?q?=E9=81=93-=E6=97=A5=E6=8A=A5=E7=B2=BE=E9=80=89=20(#4883)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 6 +++++ lib/router.js | 1 + lib/routes/baidu/daily.js | 55 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 lib/routes/baidu/daily.js diff --git a/docs/new-media.md b/docs/new-media.md index 77b9c4925..93f3d174f 100755 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -343,6 +343,12 @@ Supported sub-sites: +## 百度知道日报 + +### 精选 + + + ## 白鲸出海 ### 首页最新帖子 diff --git a/lib/router.js b/lib/router.js index 6835eba9d..ddcb92c86 100644 --- a/lib/router.js +++ b/lib/router.js @@ -975,6 +975,7 @@ router.get('/geekpark/breakingnews', require('./routes/geekpark/breakingnews')); // 百度 router.get('/baidu/doodles', require('./routes/baidu/doodles')); router.get('/baidu/topwords/:boardId?', require('./routes/baidu/topwords')); +router.get('/baidu/daily', require('./routes/baidu/daily')); // 搜狗 router.get('/sogou/doodles', require('./routes/sogou/doodles')); diff --git a/lib/routes/baidu/daily.js b/lib/routes/baidu/daily.js new file mode 100644 index 000000000..79735134b --- /dev/null +++ b/lib/routes/baidu/daily.js @@ -0,0 +1,55 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const host = `https://zhidao.baidu.com/daily?fr=daohang`; + + const response = await got.get(host, { + responseType: 'buffer', + }); + + response.data = iconv.decode(response.data, 'gbk'); + const $ = cheerio.load(response.data); + + const list = $("li[class=' clearfix']").get(); + + const items = list.map((item) => { + item = $(item); + + return { + title: item.find('div.daily-cont-top > h2 > a').text(), + description: item.find('div.daily-cont-top > div > a').text(), + link: `https://zhidao.baidu.com/${item.find('div.daily-cont-top > h2 > a').attr('href')}`, + }; + }); + + const result = await Promise.all( + items.map(async (item) => { + const link = item.link; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const itemReponse = await got.get(link, { + responseType: 'buffer', + }); + const data = iconv.decode(itemReponse.data, 'gbk'); + const itemElement = cheerio.load(data); + + item.description = itemElement('.detail-wp').html(); + + ctx.cache.set(link, JSON.stringify(item)); + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `知道日报`, + link: host, + description: `每天都知道一点`, + item: result, + }; +};