From 9e5baaa7d47196977221067e4f23dec75ace5723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E6=A5=BD=E5=9D=82=E3=81=BF=E3=81=9A=E3=81=8D?= <45122329+CokeMine@users.noreply.github.com> Date: Tue, 25 May 2021 21:41:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=B8=9C=E7=AB=8B?= =?UTF-8?q?=E5=87=BA=E7=89=88=20(#7516)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: NeverBehave --- docs/reading.md | 6 ++++++ lib/router.js | 3 +++ lib/routes/tongli/news.js | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 lib/routes/tongli/news.js diff --git a/docs/reading.md b/docs/reading.md index e6d649c6b..663b32ec3 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -182,6 +182,12 @@ count 的取值范围为 1-12,为防止请求次数过多,推荐设置为 5 +## 东立出版 + +### NEWS 资讯 + + + ## 飞地 ### 分类 diff --git a/lib/router.js b/lib/router.js index dee0cf35a..6f11e24cd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4089,6 +4089,9 @@ router.get('/bandcamp/tag/:tag?', require('./routes/bandcamp/tag')); // Hugo 更新日志 router.get('/hugo/releases', require('./routes/hugo/releases')); +// 东立出版 +router.get('/tongli/news/:type', require('./routes/tongli/news')); + // OR router.get('/or/:id?', require('./routes/or')); diff --git a/lib/routes/tongli/news.js b/lib/routes/tongli/news.js new file mode 100644 index 000000000..a3d8ad2f8 --- /dev/null +++ b/lib/routes/tongli/news.js @@ -0,0 +1,45 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +module.exports = async (ctx) => { + const { type } = ctx.params; + const baseURL = 'https://www.tongli.com.tw/'; + const link = `${baseURL}TNews_List.aspx?Type=${type}&Page=1`; + const res = await got.get(link); + const $ = cheerio.load(res.data); + const title = $('entry_title .n1').text(); + const _list = $('.news_list ul li') + .map(function () { + let link = $(this).find('.title a').attr('href'); + /^https?:\/\//.test(link) || (link = baseURL + link); + return { + title: $(this).find('.title a').text(), + link, + pubDate: new Date($(this).find('.date').text()), + }; + }) + .get(); + const list = await Promise.all( + _list.map(async (item) => { + const { title, link, pubDate } = item; + const description = await ctx.cache.tryGet(link, async () => { + const res = await got.get(link); + const $ = cheerio.load(res.data); + if (/^https:\/\/tonglinv\.pixnet\.net/.test(link)) { + return $('.article-content-inner').html(); + } else if (/^https?:\/\/blog\.xuite\.net\//.test(link)) { + return $('#content_all').html(); + } else if (/TNews_View\.aspx/.test(link)) { + return $('#ContentPlaceHolder1_TNewsContent').html(); + } else { + return ''; + } + }); + return Promise.resolve({ title, link, description, pubDate }); + }) + ); + ctx.state.data = { + title, + link, + item: list, + }; +};