From a68f6c6ac82c5dfde077095b64e3ceda7423efe7 Mon Sep 17 00:00:00 2001 From: Enoch Ma Date: Fri, 30 Oct 2020 01:55:54 +0800 Subject: [PATCH] feat: add udn global (#6044) --- docs/traditional-media.md | 6 ++++ lib/router.js | 3 ++ lib/routes/udn/global.js | 61 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/routes/udn/global.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index a75e2a88a..1d39075a6 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -185,6 +185,12 @@ Solidot 提供的 feed: +## UDN + +### 轉角國際 + + + ## Yahoo ### 新聞 diff --git a/lib/router.js b/lib/router.js index b890fd178..af053803e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1410,6 +1410,9 @@ router.get('/wegene/newest', require('./routes/wegene/newest')); // instapaper router.get('/instapaper/person/:name', require('./routes/instapaper/person')); +// UDN Global +router.get('/udn/global/:tid', require('./routes/udn/global')); + // UI 中国 router.get('/ui-cn/article', require('./routes/ui-cn/article')); router.get('/ui-cn/user/:id', require('./routes/ui-cn/user')); diff --git a/lib/routes/udn/global.js b/lib/routes/udn/global.js new file mode 100644 index 000000000..e132ac54d --- /dev/null +++ b/lib/routes/udn/global.js @@ -0,0 +1,61 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + let filter, url, tag; + if (ctx.params.tid !== 'newest') { + url = `https://global.udn.com/search/tagging/1020/${ctx.params.tid}`; + filter = 'li.contentli'; + tag = ctx.params.tid; + } else { + url = `https://global.udn.com/global_vision/index`; + filter = `.news_cards > ul > li`; + tag = '最新文章'; + } + const res = await got.get(url); + const $ = cheerio.load(res.data); + const list = $(filter).get(); + + const out = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + let title; + let address = $('a').attr('href'); + if (ctx.params.tid === 'newest') { + address = `https://global.udn.com` + address; + title = $('h3').text(); + } else { + title = $('h2').text(); + } + const time = $('span.date').text(); + const author = $('span.author').text(); + const cache = await ctx.cache.get(address); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const res = await got.get(address); + const capture = cheerio.load(res.data); + capture('div.social_bar').remove(); + capture('div.area').remove(); + capture('.story_art_title').remove(); + capture('.story_bady_info_author').remove(); + capture('.photo_pop').remove(); + const contents = capture('div.story_body_content').html(); + const single = { + title: title, + author: author, + description: contents, + link: address, + guid: address, + pubDate: new Date(time).toUTCString(), + }; + ctx.cache.set(address, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + ctx.state.data = { + title: `轉角國際 | ${tag}`, + link: url, + item: out, + }; +};