Skip to content

Commit

Permalink
Enhancement: FT 中文网支持繁体输出 (#995)
Browse files Browse the repository at this point in the history
目前有两个待确定的问题:
- `channel` 参数是可选的,如果后面跟上的`site` 也是可选的会影响到前面的逻辑,所以暂时先用`query`参数(`?site=big5`)代替了。全局搜了下发现`query`参数好像只在中间件里用到,有些担心会冲突或者破坏一些先前约定什么的,这里需要请教下看看有啥更好的参数设计方案不
- 测试的时候网络有点小坑,没能完全测试完,方便的话麻烦看下繁体输出的会不会有问题

以上
  • Loading branch information
xyqfer authored and DIYgod committed Nov 1, 2018
1 parent 3ed583f commit 717d315
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 59 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Expand Up @@ -1534,7 +1534,7 @@ Category 列表:

### FT 中文网

<route name="FT 中文网" author="HenryQW" example="/ft/chinese/hotstoryby7day" path="/ft/chinese/:channel?" :paramsDesc="['频道, 缺省为每日更新']">
<route name="FT 中文网" author="HenryQW xyqfer" example="/ft/chinese/hotstoryby7day" path="/ft/:language/:channel?" :paramsDesc="['语言,简体`chinese`,繁体`traditional`', '频道, 缺省为每日更新']">

::: tip 提示

Expand Down
2 changes: 1 addition & 1 deletion router.js
Expand Up @@ -660,7 +660,7 @@ router.get('/nhk/news_web_easy', require('./routes/nhk/news_web_easy'));
router.get('/bbc/:channel?', require('./routes/bbc/index'));

// FT 中文网
router.get('/ft/chinese/:channel?', require('./routes/ft/chinese'));
router.get('/ft/:language/:channel?', require('./routes/ft/channel'));

// The Verge
router.get('/verge', require('./routes/verge/index'));
Expand Down
8 changes: 8 additions & 0 deletions routes/ft/channel.js
@@ -0,0 +1,8 @@
const utils = require('./utils');

module.exports = async (ctx) => {
ctx.state.data = await utils.getData({
site: ctx.params.language === 'chinese' ? 'www' : 'big5',
channel: ctx.params.channel,
});
};
55 changes: 0 additions & 55 deletions routes/ft/chinese.js

This file was deleted.

57 changes: 55 additions & 2 deletions routes/ft/utils.js
@@ -1,4 +1,9 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const Parser = require('rss-parser');

const ProcessFeed = ($, link) => {
const title = $('h1').text();
let content = $('div.story-container');

// 处理封面图片
Expand Down Expand Up @@ -27,9 +32,57 @@ const ProcessFeed = ($, link) => {
});
content = content.html();

return { content, author };
return { content, author, title };
};

const getData = async ({ site = 'www', channel }) => {
const parser = new Parser();
let feed;

if (channel) {
let channel = channel.toLowerCase();
channel = channel.split('-').join('/');

try {
feed = await parser.parseURL(`http://${site}.ftchinese.com/rss/${channel}`);
} catch (error) {
return {
title: `FT 中文网 ${channel} 不存在`,
description: `FT 中文网 ${channel} 不存在`,
};
}
} else {
feed = await parser.parseURL(`http://${site}.ftchinese.com/rss/feed`);
}

const items = await Promise.all(
feed.items.splice(0, 10).map(async (item) => {
const response = await axios({
method: 'get',
url: `${item.link}?full=y&archive`,
});

const $ = cheerio.load(response.data);
const result = ProcessFeed($, item.link);
const single = {
title: result.title,
description: result.content,
author: result.author,
pubDate: item.pubDate,
link: item.link,
};
return Promise.resolve(single);
})
);

return {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
};
};

module.exports = {
ProcessFeed,
getData,
};

0 comments on commit 717d315

Please sign in to comment.