feat(route): add 聯合新聞網 (#10654)

* feat(route): add 聯合新聞網

* fix(route): concatenate the url correctly

* feat(route): fetch link name

* feat(route): description uses HTML instead of raw text

* feat(route): remove `from` search params from link

* feat(route): improve feed description readability

* fix(route): article was not properly parsed

* refactor(route): improve feed fetching
This commit is contained in:
miles
2022-08-31 10:16:56 +02:00
committed by GitHub
parent c1176174d4
commit 9e1796944f
6 changed files with 120 additions and 0 deletions
+12
View File
@@ -1376,6 +1376,18 @@ category 对应的关键词有
<Route author="shunf4" example="/zaobao/interactive-graphics" path="/zaobao/interactive-graphics" />
## 聯合新聞網
### 即時新聞
<Route author="miles170" example="/udn/news/breakingnews/99" path="/udn/news/breakingnews/:id" :paramsDesc="['类别']" radar="1">
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 99 |
| -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --- |
| 精選 | 要聞 | 社會 | 地方 | 兩岸 | 國際 | 財經 | 運動 | 娛樂 | 生活 | 股市 | 文教 | 數位 | 不分類 |
</Route>
## 路透社
::: warning 迁移说明
+85
View File
@@ -0,0 +1,85 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const cheerio = require('cheerio');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const id = ctx.params.id;
const link = `/news/breaknews/1/${id}#breaknews`;
const name = await getLinkName(ctx, link);
const rootUrl = 'https://udn.com';
const response = await got(`${rootUrl}/api/more?page=1&channelId=1&cate_id=${id}&type=breaknews`);
const items = await Promise.all(
response.data.lists.map((item) => {
let link = item.titleLink.indexOf('http://') === 0 || item.titleLink.indexOf('https://') === 0 ? item.titleLink : `${rootUrl}${item.titleLink}`;
const linkUrl = new URL(link);
// cleanup query paramter
linkUrl.query = linkUrl.search = '';
link = linkUrl.toString();
return ctx.cache.tryGet(link, async () => {
let result = await got(link);
// VIP article requires redirection
// e.g. https://udn.com/news/story/7331/6576320
const vip = result.data.match(/<script language=javascript>window\.location\.href="(https?:\/\/[^"]+")/);
if (vip) {
result = await got(vip[1]);
}
const $ = cheerio.load(result.data);
const metadata = $('script[type="application/ld+json"]').eq(0).text().trim();
const data = metadata.startsWith('[') ? JSON.parse(metadata)[0] : JSON.parse(metadata);
// e.g. https://udn.com/news/story/7331/6576320
const content = $('.article-content__editor');
// e.g. https://udn.com/news/story/7240/6576424
const body = $('.article-body__editor');
let description = '';
if (data.image) {
description += art(path.join(__dirname, 'templates/figure.art'), {
src: data.image.contentUrl,
alt: data.image.name,
});
}
if (content.length) {
description += content.html();
} else if (body.length) {
description += body.html();
}
return {
title: item.title,
author: data.author.name,
description,
pubDate: timezone(parseDate(item.time.date, 'YYYY-MM-DD HH:mm'), +8),
category: [data.articleSelection, ...data.keywords.split(',')],
link,
};
});
})
);
ctx.state.data = {
title: `即時${name} - 聯合新聞網`,
link: `${rootUrl}${link}`,
description: 'udn.com 提供即時新聞以及豐富的政治、社會、地方、兩岸、國際、財經、數位、運動、NBA、娛樂、生活、健康、旅遊新聞,以最即時、多元的內容,滿足行動世代的需求',
item: items,
};
};
const getLinkName = async (ctx, link) => {
const url = 'https://udn.com/news/breaknews';
const links = await ctx.cache.tryGet(url, async () => {
const result = await got(url);
const $ = cheerio.load(result.data);
const data = $('.cate-list__subheader a')
.get()
.map((item) => {
item = $(item);
return { [item.attr('href')]: item.text().trim() };
});
return Object.assign({}, ...data);
});
if (link in links) {
return links[link];
}
return '列表';
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/news/breakingnews/:id': ['miles170'],
};
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
'udn.com': {
_name: '聯合新聞網',
'.': [
{
title: '即時新聞',
docs: 'https://docs.rsshub.app/new-media.html#lian-he-xin-wen-wang-ji-shi-xin-wen',
source: '/news/breaknews/1/:id',
target: '/udn/news/breakingnews/:id',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/news/breakingnews/:id', require('./breaking-news'));
};
+4
View File
@@ -0,0 +1,4 @@
<figure>
<picture><img src={{ src }} alt={{ alt }}></picture>
<figurecaption>{{ alt }}</figurecaption>
</figure>