feat: add udn global (#6044)

This commit is contained in:
Enoch Ma
2020-10-29 17:55:54 +00:00
committed by GitHub
parent 282d9ec931
commit a68f6c6ac8
3 changed files with 70 additions and 0 deletions
+6
View File
@@ -185,6 +185,12 @@ Solidot 提供的 feed:
</Route>
## UDN
### 轉角國際
<Route author="emdoe" example="/udn/global/鏡頭背後" path="/udn/global/:tid" :paramsDesc="['標籤名稱,請在轉角國際首頁獲取;如果選擇輸入 `newest` 則輸出最新文章']">
## Yahoo
### 新聞
+3
View File
@@ -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'));
+61
View File
@@ -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,
};
};