From 404b861d5218362e67644721456d2f0f79dffde0 Mon Sep 17 00:00:00 2001 From: Toby Tso <7851076+tpnonthealps@users.noreply.github.com> Date: Sat, 21 Aug 2021 15:44:37 +0800 Subject: [PATCH] feat(route): add Font Story (DynaC TW) (#7565) --- docs/blog.md | 4 ++++ lib/router.js | 3 +++ lib/routes/fontstory/tw.js | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 lib/routes/fontstory/tw.js diff --git a/docs/blog.md b/docs/blog.md index bcd0c9529..8ba9896e9 100644 --- a/docs/blog.md +++ b/docs/blog.md @@ -201,3 +201,7 @@ pageClass: routes ### 文章 + +## 華康字型故事 + + diff --git a/lib/router.js b/lib/router.js index 43b550925..caebe2ee0 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4140,6 +4140,9 @@ router.get('/tongli/news/:type', lazyloadRouteHandler('./routes/tongli/news')); // OR router.get('/or/:id?', lazyloadRouteHandler('./routes/or')); +// 字型故事 +router.get('/fontstory', require('./routes/fontstory/tw')); + // HKEPC router.get('/hkepc/:category?', lazyloadRouteHandler('./routes/hkepc/index')); diff --git a/lib/routes/fontstory/tw.js b/lib/routes/fontstory/tw.js new file mode 100644 index 000000000..3dbb2e6e6 --- /dev/null +++ b/lib/routes/fontstory/tw.js @@ -0,0 +1,47 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +function getArticle(ctx, list) { + const now = list.map((line) => + ctx.cache.tryGet(line, async () => { + const a_r = await got.get(`https://www.dynacw.com.tw/fontstory/${line}`); + const $ = cheerio.load(a_r.data); + const t_r = $('h2.col-xs-12.tc-deeppink').first().text(); + const tag = $('div.date-txt.col-xs-8.tc-gray').first().text(); + $('main.container').find('ol.breadcrumb.hidden-xs').remove(); + $('main.container').find('section.row').first().remove(); + $('main.container').find('section.pages.row').remove(); + const desc = $('main.container').html(); + + return { + title: `${t_r}(${tag})`, + description: desc, + pubDate: timezone(parseDate($('div.date-txt.col-xs-4.tc-gray.text-right').text(), 'YYYY年MM月DD日'), +8), + link: `https://www.dynacw.com.tw/fontstory/${line}`, + }; + }) + ); + return Promise.all(now); +} + +module.exports = async (ctx) => { + const response = await got({ + method: 'get', + url: 'https://www.dynacw.com.tw/fontstory/fontstory.aspx', + }); + + const $ = cheerio.load(response.data); + const dom = $('div.font-item.col-xs-12'); + const relink = dom.map((_index, li) => $(li).find('a').first().attr('href')); + const list = relink.toArray(); + + const rss = await getArticle(ctx, list.slice(0, 10)); + ctx.state.data = { + title: '字型故事|華康字型', + link: 'https://www.dynacw.com.tw/fontstory/fontstory.aspx', + item: rss, + }; +};