feat: ChinaFile (#6068)

This commit is contained in:
oppilate
2020-11-01 15:34:20 +00:00
committed by GitHub
parent c5a89f78c6
commit 4e10365544
4 changed files with 98 additions and 0 deletions

View File

@@ -108,6 +108,20 @@ Refer to [Chicago Tribune's feed page](https://www.chicagotribune.com/about/ct-c
</RouteEn>
## ChinaFile
### Reporting & Opinion
<RouteEn author="oppilate" example="/chinafile/all" path="/chinafile/:category?" :paramsDesc="['Category, by default `all`']">
Generates full-text feeds that the official feed doesn't provide.
| All | The China NGO Project |
| --- | --------------------- |
| all | ngo |
</RouteEn>
## NHK
### News Web Easy

View File

@@ -91,6 +91,18 @@ pageClass: routes
</Route>
## ChinaFile
<Route author="oppilate" example="/chinafile/all" path="/chinafile/:category?" :paramsDesc="['分类,默认 `all`']">
通过提取文章全文,以提供比官方源更佳的阅读体验。
| 全部 | The China NGO Project |
| ---- | --------------------- |
| all | ngo |
</Route>
## e 公司
### 快讯

View File

@@ -3444,4 +3444,7 @@ router.get('/gov/harbin/kjj', require('./routes/gov/harbin/kjj'));
// WSJ
router.get('/wsj/:lang/:category', require('./routes/wsj/index'));
// China File
router.get('/chinafile/:category?', require('./routes/chinafile/index'));
module.exports = router;

View File

@@ -0,0 +1,69 @@
const Parser = require('rss-parser');
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const category = ctx.params.category || 'all';
const rssUrl = `https://feeds.feedburner.com/chinafile/${category}`;
// The custom UA in @/utils/parser (mimic browser) results in a HTML page
// which cannot be parsed
const parser = new Parser();
const feed = await parser.parseURL(rssUrl);
const items = await Promise.all(
feed.items.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
let url = item.link;
const response = await got({ url });
const html = response.body;
const $ = cheerio.load(html);
const content = $('article');
// Cover
const cover = $('.view-featured-photo');
if (cover.length > 0) {
cover.insertBefore(content[0].childNodes[0]);
$(cover).remove();
}
// Summary
const summary = $('meta[name="description"]').attr('content');
const updatedAt = $('meta[name="og:updated_time"]').attr('content');
const categories = $('meta[name="news_keywords"]')
.attr('content')
.split(',')
.map((c) => c.trim());
url = $('link[rel="canonical"]').attr('href');
return {
title: item.title,
id: item.guid,
pubDate: item.pubDate,
updated: updatedAt,
author: item.creator,
link: url,
summary: summary,
description: content.html(),
category: categories,
icon: 'https://www.chinafile.com/sites/default/files/chinafile_favicon.png',
logo: 'https://www.chinafile.com/sites/all/themes/cftwo/assets/images/logos/logo-large.png',
};
})
)
);
ctx.state.data = {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
language: 'en-us',
icon: 'https://www.chinafile.com/sites/default/files/chinafile_favicon.png',
logo: 'https://www.chinafile.com/sites/all/themes/cftwo/assets/images/logos/logo-large.png',
};
};