From fa9181fcf1a4cc92fcb5fbe1efc6297117c7b875 Mon Sep 17 00:00:00 2001 From: Laam Pui Date: Mon, 30 Nov 2020 19:36:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E9=81=A0=E8=A6=8B=20gvm.com.tw?= =?UTF-8?q?=20(#6282)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 9 ++++ lib/router.js | 3 ++ lib/routes/gvm/index.js | 95 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 lib/routes/gvm/index.js diff --git a/docs/new-media.md b/docs/new-media.md index 118a98a17c6530..39cb2817bd58d4 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1749,6 +1749,15 @@ column 为 third 时可选的 category: +## 遠見 + + + +| 最新文章 | 你可能會喜歡 | 名家專欄 | 專題 | 時事熱點 | 政治 | 社會 | 人物報導 | 國際 | 全球焦點 | 兩岸 | 金融理財 | 投資理財 | 保險規劃 | 退休理財 | 金融 Fintech | 房地產 | 總體經濟 | 科技 | 科技趨勢 | 能源 | 產經 | 傳產 | 消費服務 | 生技醫藥 | 傳承轉型 | 創業新創 | 管理 | 農業 | 教育 | 高教 | 技職 | 親子教育 | 國際文教 | 體育 | 好享生活 | 時尚設計 | 心靈成長 | 藝文影視 | 旅遊 | 環境生態 | 健康 | 美食 | 職場生涯 | 調查 | 縣市 | CSR | +| -------- | ------------ | -------- | ----- | -------- | -------- | ------- | -------- | ----- | ----------- | --------------------- | -------- | ---------- | --------- | -------- | ------------ | ----------- | -------- | ---- | ---------- | ------ | -------- | -------- | -------- | -------- | -------------------------- | -------- | ---------- | ----------- | --------- | ---------------- | ------------- | -------- | --------------- | ------ | -------- | -------- | ----------- | -------- | ------ | ----------- | ------ | ---- | -------- | ------ | ------ | --- | +| newest | recommend | opinion | topic | news | politics | society | figure | world | world_focus | cross_strait_politics | money | investment | insurance | retire | fintech | real_estate | economy | tech | tech_trend | energy | business | industry | service | medical | family_business_succession | startup | management | agriculture | education | higher_education | technological | parent | world_education | sports | life | art | self_growth | film | travel | environment | health | food | career | survey | county | csr | + + ## 装备前线 ### 首页最新帖子 diff --git a/lib/router.js b/lib/router.js index 300a527d25b595..ebc9b39ec396d0 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3588,6 +3588,9 @@ router.get('/youdao/latest', require('./routes/youdao/latest')); router.get('/yinxiang/note', require('./routes/yinxiang/note')); router.get('/yinxiang/card/:id?', require('./routes/yinxiang/card')); +// 遠見 gvm.com.tw +router.get('/gvm/index/:category?', require('./routes/gvm/index')); + // 触乐 router.get('/chuapp/index/:category?', require('./routes/chuapp/index')); diff --git a/lib/routes/gvm/index.js b/lib/routes/gvm/index.js new file mode 100644 index 00000000000000..252cf79a22b39e --- /dev/null +++ b/lib/routes/gvm/index.js @@ -0,0 +1,95 @@ +const got = require('got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const category = ctx.params.category || 'newest'; + let suffix = '/newest'; + + const options = { + newest: '最新文章', + recommend: '你可能會喜歡', + opinion: '名家專欄', + topic: '專題', + news: '時事熱點', + politics: '政治', + society: '社會', + figure: '人物報導', + world: '國際', + world_focus: '全球焦點', + cross_strait_politics: '兩岸', + money: '金融理財', + investment: '投資理財', + insurance: '保險規劃', + retire: '退休理財', + fintech: '金融Fintech', + real_estate: '房地產', + economy: '總體經濟', + tech: '科技', + tech_trend: '科技趨勢', + energy: '能源', + business: '產經', + industry: '傳產', + service: '消費服務', + medical: '生技醫藥', + family_business_succession: '傳承轉型', + startup: '創業新創', + management: '管理', + agriculture: '農業', + education: '教育', + higher_education: '高教', + technological: '技職', + parent: '親子教育', + world_education: '國際文教', + sports: '體育', + life: '好享生活', + art: '時尚設計', + self_growth: '心靈成長', + film: '藝文影視', + travel: '旅遊', + environment: '環境生態', + health: '健康', + food: '美食', + career: '職場生涯', + survey: '調查', + county: '縣市', + csr: 'CSR', + }; + + if (category !== 'newest' && category !== 'recommend') { + suffix = `/category/${category}`; + } + + const response = await got({ + method: 'get', + url: 'https://www.gvm.com.tw' + suffix, + }); + + const $ = cheerio.load(response.body); + + const articles = $('#article_list .article-list-item .article-list-item__intro') + .map((index, ele) => ({ + title: $('a', ele).text(), + link: $('a', ele).attr('href'), + pubDate: new Date($('.time', ele).text()), + author: $('.author', ele).text(), + })) + .get(); + + const item = await Promise.all( + articles.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got.get(item.link); + const content = cheerio.load(res.body); + item.description = content('.article-content').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: `遠見 - ${options[category]}`, + link: 'https://www.gvm.com.tw' + suffix, + item, + }; +};