feat: Add Google News (#3953)

This commit is contained in:
zoenglinghou
2020-02-11 10:50:39 +01:00
committed by GitHub
parent 621f3e9539
commit a49f217e9e
4 changed files with 122 additions and 4 deletions

View File

@@ -60,6 +60,10 @@ Support major channels, refer to [BBC RSS feeds](https://www.bbc.co.uk/news/1062
</RouteEn>
## Google News
<Route author="zoenglinghou" example="/google/news/Headlines/hl=en-US&gl=US&ceid=US:en" path="/google/news/:category/:locale" :paramsDesc="['Category Title', 'locales, could be found behind `?`, including `hl`, `gl`, and `ceid` as parameters']"/>
## NHK
### News Web Easy
@@ -128,12 +132,12 @@ Provides a better reading experience (full text articles) over the official one.
`Region`
| Hong Kong | Taiwan | US |
| -- | -- | -- |
| --------- | ------ | --- |
| hk | tw | en |
`Category`
| All | World | Business | Entertainment | Sports | Health |
| -- | -- | -- | -- | -- | -- |
| ------- | ----- | -------- | ------------- | ------ | ------ |
| (Empty) | world | business | entertainment | sports | health |
</Route>

View File

@@ -104,12 +104,12 @@ Solidot 提供的 feed:
`地区`
| 香港 | 台灣 | 美國 |
| -- | -- | -- |
| ---- | ---- | ---- |
| hk | tw | en |
`类別`
| 新聞總集 | 兩岸國際 | 財經 | 娛樂 | 體育 | 健康 |
| -- | -- | -- | -- | -- | -- |
| -------- | -------- | -------- | ------------- | ------ | ------ |
| (空) | world | business | entertainment | sports | health |
</Route>
@@ -188,6 +188,10 @@ Category 列表:
<Route author="HenryQW" example="/dwnews/rank" path="/dwnews/rank"/>
## 谷歌新闻
<Route author="zoenglinghou" example="/google/news/要闻/hl=zh-CN&gl=CN&ceid=CN:zh-Hans" path="/google/news/:category/:locale" :paramsDesc="['子分类标题', '地区语言设置,在地址栏 `?` 后,包含 `hl``gl`,以及 `ceid` 参数']"/>
## 华尔街见闻
### 华尔街见闻

View File

@@ -2228,4 +2228,7 @@ router.get('/cowlevel/element/:id', require('./routes/cowlevel/element'));
// 2048
router.get('/2048/bbs/:fid', require('./routes/2048/bbs'));
// Google News
router.get('/google/news/:category/:locale', require('./routes/google/news'));
module.exports = router;

107
lib/routes/google/news.js Normal file
View File

@@ -0,0 +1,107 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
module.exports = async (ctx) => {
const category = ctx.params.category;
const locale = ctx.params.locale;
const category_url = await ctx.cache.tryGet([locale, category], async () => {
const front_page = await got({
method: 'get',
url: `https://news.google.com/?${locale}`,
});
const front_data = front_page.data;
const $ = cheerio.load(front_data);
const category_list = $('a.wmzpFf.yETrXb');
const category_text = [];
category_list.each((index, item) => {
category_text.push($(item).text());
});
return url.resolve('https://news.google.com', category_list.eq(category_text.indexOf(category)).attr('href'));
});
const response = await got({
method: 'get',
url: category_url,
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('div.xrnccd');
let itemPicUrl;
let meta;
let description;
let link;
ctx.state.data = {
title: $('title').text(),
link: category_url,
item:
list &&
list
.map((index, item) => {
item = $(item);
itemPicUrl = item.find('img').attr('src');
meta = item.find('div.SVJrMe').first();
description = item
.find('h4.ipQwMb')
.removeAttr('class')
.each((index, item) => {
$(item)
.children()
.removeAttr('class')
.attr(
'href',
url.resolve(
'https://news.google.com',
$(item)
.children()
.attr('href')
)
);
})
.toString();
link = url.resolve(
'https://news.google.com',
item
.find('a.VDXfz')
.first()
.attr('href')
);
if (itemPicUrl !== undefined) {
description = `${description}<img src="${itemPicUrl}">`;
}
if (description === null) {
description = link;
}
return {
title: item
.find('h3')
.children()
.text(),
description: description,
pubDate: meta
.find('time')
.first()
.attr('datetime'),
author: meta
.find('a.wEwyrc')
.first()
.text(),
link: link,
};
})
.get(),
};
};