feat(route): add 聯合新聞網轉角國際 (#10938)

This commit is contained in:
Ethan Shen 2022-09-29 22:03:19 +08:00 committed by GitHub
parent d1b4a05105
commit 66c884bd85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 157 additions and 69 deletions

View File

@ -444,12 +444,6 @@ Solidot 提供的 feed:
</Route>
## UDN
### 轉角國際
<Route author="emdoe" example="/udn/global/鏡頭背後" path="/udn/global/:tid" :paramsDesc="['標籤名稱,請在轉角國際首頁獲取;如果選擇輸入 `newest` 則輸出最新文章']">
## Voice of America (VOA)
透過提取全文,以獲得更好的閱讀體驗
@ -1362,6 +1356,25 @@ category 对应的关键词有
</Route>
### 轉角國際 - 首頁
<Route author="emdoe nczitzk" example="/udn/global" path="/udn/global/:category?" :paramsDesc="['分类,见下表,默认为首頁']">
| 首頁 | 最新文章 | 熱門文章 |
| -- | ---- | ---- |
| | new | hot |
</Route>
### 轉角國際 - 標籤
<Route author="nczitzk" example="/udn/global/tag/過去24小時" path="/udn/global/tag/:tag?" :paramsDesc="['标签,可在对应标签页 URL 中找到']">
| 過去 24 小時 | 鏡頭背後 | 深度專欄 | 重磅廣播 |
| -------- | ---- | ---- | ---- |
</Route>
## 路透社
::: warning 迁移说明

View File

@ -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'));

View File

@ -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,
};
};

View File

@ -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,
};
};

51
lib/v2/udn/global/tag.js Normal file
View File

@ -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,
};
};

View File

@ -1,3 +1,5 @@
module.exports = {
'/global/:category?': ['nczitzk'],
'/global/tag/:tag?': ['emdoe', 'nczitzk'],
'/news/breakingnews/:id': ['miles170'],
};

View File

@ -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?',
},
],
},
};

View File

@ -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'));
};