Merge pull request #6595 from zphw/feature/technologyreview

feat: add homepage route for Technology Review
This commit is contained in:
NeverBehave 2021-01-10 21:58:06 -05:00 committed by GitHub
commit dd9bb40c79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View File

@ -90,6 +90,10 @@ The parameter id in the route is the id in the URL of the user s Google Schol
## MIT Technology Review
<RouteEn author="zphw" example="/technologyreview" path="/technologyreview" />
### Topics
<RouteEn author="laampui" example="/technologyreview/humans-and-technology" path="/technologyreview/:category_name" :paramsDesc="['see below']" />

View File

@ -497,8 +497,10 @@ router.get('/oschina/user/:id', require('./routes/oschina/user'));
router.get('/oschina/u/:id', require('./routes/oschina/u'));
router.get('/oschina/topic/:topic', require('./routes/oschina/topic'));
// MIT technology review
// MIT Technology Review
router.get('/technologyreview', require('./routes/technologyreview/index'));
router.get('/technologyreview/:category_name', require('./routes/technologyreview/topic'));
// 安全客
router.get('/aqk/vul', require('./routes/aqk/vul'));
router.get('/aqk/:category', require('./routes/aqk/category'));

View File

@ -0,0 +1,43 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const response = await got.get('https://wp.technologyreview.com/wp-json/irving/v1/data/the_feed?page=1');
const result = await Promise.all(
response.data
.filter((item) => item.config.permalink)
.map(
async (item) =>
await ctx.cache.tryGet(item.config.permalink, async () => {
const article = await got.get(item.config.permalink);
const $ = cheerio.load(article.data);
// remove annoyances
$('.sliderAd__wrapper--1vLJw').remove();
$('.newsletter__wrap').remove();
$('.related__wrap').remove();
$('aside').remove();
$('svg.monogramTLogo').remove();
$('.image__placeholder--1CF-F').remove();
const description = ($('.contentHeader__image--1pFzw').html() || $('.contentHeader--fullBleed__image--2jdH0').html() || '') + ($('.contentHeader__deck--3A9FE').html() || '') + $('#content--body').html();
const author = $('.byline__name--2MpUW').clone().children().remove().end().text();
return {
title: item.config.title || item.config.name || '',
author,
description,
link: item.config.permalink,
pubDate: new Date($('.contentHeader__publishDate--37zcW').text()).toISOString(),
};
})
)
);
ctx.state.data = {
title: 'MIT Technology Review',
link: 'https://www.technologyreview.com/',
item: result,
};
};