fix(route): google news (#11662)

This commit is contained in:
Tony
2023-01-21 01:30:13 +08:00
committed by GitHub
parent cf6dff4305
commit ba89e298bb
7 changed files with 58 additions and 69 deletions
+1 -1
View File
@@ -340,7 +340,7 @@ Country
### News
<RouteEn 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']"/>
<RouteEn author="zoenglinghou" example="/google/news/Top stories/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']"/>
## Grub Street
+1 -1
View File
@@ -2260,7 +2260,7 @@ Type 栏目:
### 新闻
<Route author="zoenglinghou" example="/google/news/闻/hl=zh-CN&gl=CN&ceid=CN:zh-Hans" path="/google/news/:category/:locale" :paramsDesc="['子分类标题', '地区语言设置,在地址栏 `?` 后,包含 `hl``gl`,以及 `ceid` 参数']"/>
<Route author="zoenglinghou" example="/google/news/焦点新闻/hl=zh-CN&gl=CN&ceid=CN:zh-Hans" path="/google/news/:category/:locale" :paramsDesc="['子分类标题', '地区语言设置,在地址栏 `?` 后,包含 `hl``gl`,以及 `ceid` 参数']"/>
## 观察者网
+1 -1
View File
@@ -2455,7 +2455,7 @@ router.get('/cowlevel/element/:id', lazyloadRouteHandler('./routes/cowlevel/elem
// router.get('/2048/bbs/:fid', lazyloadRouteHandler('./routes/2048/bbs'));
// Google News
router.get('/google/news/:category/:locale', lazyloadRouteHandler('./routes/google/news'));
// router.get('/google/news/:category/:locale', lazyloadRouteHandler('./routes/google/news'));
// 虛詞
router.get('/p-articles/section/:section', lazyloadRouteHandler('./routes/p-articles/section'));
+1
View File
@@ -3,6 +3,7 @@ module.exports = {
'/citations/:id': ['KellyHwong'],
'/doodles/:language?': ['xyqfer'],
'/fonts/:sort?': ['Fatpandac'],
'/news/:category/:locale': ['zoenglinghou'],
'/scholar/:query': ['HenryQW'],
'/sites/:id': ['hoilc'],
'/sites/recentChanges/:id': ['nczitzk'],
+48 -66
View File
@@ -1,84 +1,66 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
const baseUrl = 'https://news.google.com';
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 categoryUrls = await ctx.cache.tryGet(`google:news:${locale}`, async () => {
const { data: front_data } = await got(`${baseUrl}/?${locale}`);
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'));
return [
...$('a.brSCsc')
.toArray()
.slice(3) // skip Home, For you and Following
.map((item) => {
item = $(item);
return {
category: item.text(),
url: new URL(item.attr('href'), baseUrl).href,
};
}),
...$('a.aqvwYd') // Home
.toArray()
.map((item) => {
item = $(item);
return {
category: item.text(),
url: new URL(item.attr('href'), baseUrl).href,
};
}),
];
});
const categoryUrl = categoryUrls.find((item) => item.category === category).url;
const response = await got({
method: 'get',
url: category_url,
});
const data = response.data;
const { data } = await got(categoryUrl);
const $ = cheerio.load(data);
const list = $('div.xrnccd');
let itemPicUrl;
let meta;
let description;
let link;
const list = [...$('.UwIKyb'), ...$('.IBr9hb'), ...$('.IFHyqb')]; // 3 rows of news, 3-rows-wide news, single row news
const items = list.map((item) => {
item = $(item);
const title = item.find('h4').text();
return {
title,
description: art(path.join(__dirname, 'templates/news.art'), {
img: item.find('img.Quavad').attr('src'),
brief: title,
}),
pubDate: parseDate(item.find('time').attr('datetime')),
author: item.find('.oovtQ').text(),
link: new URL(item.find('a.WwrzSb').first().attr('href'), baseUrl).href,
};
});
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,
pubDate: meta.find('time').first().attr('datetime'),
author: meta.find('a.wEwyrc').first().text(),
link,
};
})
.get(),
link: categoryUrl,
item: items,
};
};
+1
View File
@@ -3,6 +3,7 @@ module.exports = function (router) {
router.get('/citations/:id', require('./citations'));
router.get('/doodles/:language?', require('./doodles'));
router.get('/fonts/:sort?', require('./fonts'));
router.get('/news/:category/:locale', require('./news'));
router.get('/scholar/:query', require('./scholar'));
router.get('/sites/recentChanges/:id', require('./sitesRecentChanges'));
router.get('/sites/:id', require('./sites'));
+5
View File
@@ -0,0 +1,5 @@
{{ if img }}
<img src="{{ img }}">
<br>
{{ /if }}
{{ brief }}