From 66c884bd856ecc77b15dc0a07f85e56435ff6f6a Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Thu, 29 Sep 2022 22:03:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E8=81=AF=E5=90=88?= =?UTF-8?q?=E6=96=B0=E8=81=9E=E7=B6=B2=E8=BD=89=E8=A7=92=E5=9C=8B=E9=9A=9B?= =?UTF-8?q?=20(#10938)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/traditional-media.md | 25 ++++++++++---- lib/router.js | 2 +- lib/routes/udn/global.js | 61 ---------------------------------- lib/v2/udn/global/index.js | 67 ++++++++++++++++++++++++++++++++++++++ lib/v2/udn/global/tag.js | 51 +++++++++++++++++++++++++++++ lib/v2/udn/maintainer.js | 2 ++ lib/v2/udn/radar.js | 16 ++++++++- lib/v2/udn/router.js | 2 ++ 8 files changed, 157 insertions(+), 69 deletions(-) delete mode 100644 lib/routes/udn/global.js create mode 100644 lib/v2/udn/global/index.js create mode 100644 lib/v2/udn/global/tag.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 6a0b39e62..44b077b92 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -444,12 +444,6 @@ Solidot 提供的 feed: -## UDN - -### 轉角國際 - - - ## Voice of America (VOA) 透過提取全文,以獲得更好的閱讀體驗 @@ -1362,6 +1356,25 @@ category 对应的关键词有 +### 轉角國際 - 首頁 + + + +| 首頁 | 最新文章 | 熱門文章 | +| -- | ---- | ---- | +| | new | hot | + + + +### 轉角國際 - 標籤 + + + +| 過去 24 小時 | 鏡頭背後 | 深度專欄 | 重磅廣播 | +| -------- | ---- | ---- | ---- | + + + ## 路透社 ::: warning 迁移说明 diff --git a/lib/router.js b/lib/router.js index fd198a0f0..8b311c766 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1035,7 +1035,7 @@ router.get('/anime1/anime/:time/:name', lazyloadRouteHandler('./routes/anime1/an router.get('/anime1/search/:keyword', lazyloadRouteHandler('./routes/anime1/search')); // Global UDN -router.get('/udn/global/:tid', lazyloadRouteHandler('./routes/udn/global')); +// router.get('/udn/global/:tid', lazyloadRouteHandler('./routes/udn/global')); // gitea router.get('/gitea/blog', lazyloadRouteHandler('./routes/gitea/blog')); diff --git a/lib/routes/udn/global.js b/lib/routes/udn/global.js deleted file mode 100644 index 1dba7d806..000000000 --- a/lib/routes/udn/global.js +++ /dev/null @@ -1,61 +0,0 @@ -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, - 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, - }; -}; diff --git a/lib/v2/udn/global/index.js b/lib/v2/udn/global/index.js new file mode 100644 index 000000000..b37db82b2 --- /dev/null +++ b/lib/v2/udn/global/index.js @@ -0,0 +1,67 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const category = ctx.params.category ?? ''; + + const start = category === 'hot' ? 6 : 0; + const end = category === 'new' ? 6 : 12; + + const rootUrl = 'https://global.udn.com'; + const currentUrl = `${rootUrl}/global_vision/index${category ? `/${category}` : ''}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + $('.topic').remove(); + + let items = $('.news_cards ul li a') + .toArray() + .slice(start, end) + .concat(category === '' ? $('.last24, h2').find('a').toArray() : []) + .map((item) => { + item = $(item); + + const link = item.attr('href'); + + return { + title: item.find('h3').text() || item.text(), + link: /^http/.test(link) ? link : `${rootUrl}${item.attr('href')}`, + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + content('#story_art_title, #story_bady_info, #story_also').remove(); + content('.social_bar, .photo_pop, .only_mobile, .area').remove(); + + item.description = content('#tags').prev().html(); + item.author = content('#story_author_name').text(); + item.pubDate = timezone(parseDate(content('meta[name="date"]').attr('content')), +8); + item.category = content('meta[name="news_keywords"]').attr('content').split(','); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/udn/global/tag.js b/lib/v2/udn/global/tag.js new file mode 100644 index 000000000..f14c07835 --- /dev/null +++ b/lib/v2/udn/global/tag.js @@ -0,0 +1,51 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const tag = ctx.params.tag ?? '過去24小時'; + + const rootUrl = 'https://global.udn.com'; + const currentUrl = `${rootUrl}/search/tagging/1020/${tag}`; + const apiUrl = `${rootUrl}/search/ajax_tag/1020/${tag}`; + + const response = await got({ + method: 'get', + url: apiUrl, + }); + + let items = response.data.articles.map((item) => ({ + title: item.TITLE, + author: item.AUTHOR, + pubDate: parseDate(item.TIMESTAMP * 1000), + category: item.TAG.map((t) => t.tag), + link: `${rootUrl}/global_vision/story/${item.CATE_ID}/${item.ART_ID}`, + })); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + content('#story_art_title, #story_bady_info, #story_also').remove(); + content('.social_bar, .photo_pop, .only_mobile, .area').remove(); + + item.author = content('#story_author_name').text(); + item.description = content('#tags').prev().html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `轉角國際 udn Global - ${tag}`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/udn/maintainer.js b/lib/v2/udn/maintainer.js index a9599ed9b..e5e0f0907 100644 --- a/lib/v2/udn/maintainer.js +++ b/lib/v2/udn/maintainer.js @@ -1,3 +1,5 @@ module.exports = { + '/global/:category?': ['nczitzk'], + '/global/tag/:tag?': ['emdoe', 'nczitzk'], '/news/breakingnews/:id': ['miles170'], }; diff --git a/lib/v2/udn/radar.js b/lib/v2/udn/radar.js index 09f9c35ba..931c47234 100644 --- a/lib/v2/udn/radar.js +++ b/lib/v2/udn/radar.js @@ -5,9 +5,23 @@ module.exports = { { title: '即時新聞', docs: 'https://docs.rsshub.app/new-media.html#lian-he-xin-wen-wang-ji-shi-xin-wen', - source: '/news/breaknews/1/:id', + source: ['/news/breaknews/1/:id', '/'], target: '/udn/news/breakingnews/:id', }, ], + global: [ + { + title: '轉角國際 - 首頁', + docs: 'https://docs.rsshub.app/new-media.html#lian-he-xin-wen-wang-zhuan-jiao-guo-ji-shou-ye', + source: ['/global_vision/index/:category', '/'], + target: '/udn/global/:category?', + }, + { + title: '轉角國際 - 標籤', + docs: 'https://docs.rsshub.app/new-media.html#lian-he-xin-wen-wang-zhuan-jiao-guo-ji-biao-qian', + source: ['/search/tagging/1020/:tag', '/'], + target: '/udn/global/tag/:tag?', + }, + ], }, }; diff --git a/lib/v2/udn/router.js b/lib/v2/udn/router.js index c6cfe33c4..2e0d731cf 100644 --- a/lib/v2/udn/router.js +++ b/lib/v2/udn/router.js @@ -1,3 +1,5 @@ module.exports = (router) => { + router.get('/global/tag/:tag?', require('./global/tag')); + router.get('/global/:category?', require('./global/index')); router.get('/news/breakingnews/:id', require('./breaking-news')); };