feat: 德国新闻社卫健新闻 (#4186)

This commit is contained in:
howel52 2020-03-10 18:46:05 +08:00 committed by GitHub
parent 94de8e020f
commit 4f70770d68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 0 deletions

View File

@ -141,6 +141,12 @@ pageClass: routes
<Route author="xyqfer" example="/itjuzi/merge" path="/itjuzi/merge"/>
## Krankenkassen 德国新闻社卫健新闻
### dpa news
<Route author="howel52" example="/krankenkassen" path="/krankenkassen"/>
## Letterboxd
### User diary

View File

@ -2340,6 +2340,9 @@ router.get('/yzu/yjszs/:type', require('./routes/universities/yzu/yjszs'));
// 国家自然科学基金委员会
router.get('/nsfc/news/:type?', require('./routes/nsfc/news'));
// 德国新闻社卫健新闻
router.get('/krankenkassen', require('./routes/krankenkassen'));
// 桂林航天工业学院
router.get('/guat/news/:type?', require('./routes/guat/news'));

View File

@ -0,0 +1,54 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const response = await got({
method: 'get',
url: 'https://www.krankenkassen.de/dpa/',
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('.news td a');
const getContent = async (subLink) => {
const cacheKey = `krankenkassen_${subLink}`;
const cache = await ctx.cache.get(cacheKey);
if (cache) {
return JSON.parse(cache);
}
const articlePageRes = await got(subLink);
const articleHtml = articlePageRes.data;
const $ = cheerio.load(articleHtml);
const pubDate = $('.untertitel').text();
const description = $('#content p').text();
const result = {
pubDate,
description,
title: $('title').text(),
link: subLink,
};
ctx.cache.set(cacheKey, JSON.stringify(result));
return result;
};
const item = await Promise.all(
list
.slice(0, 30)
.map((index, dom) => {
dom = $(dom);
const link = `https://www.krankenkassen.de/dpa/${dom.attr('href')}`;
return getContent(link);
})
.get()
);
ctx.state.data = {
title: $('title').text(),
link: 'https://www.krankenkassen.de/dpa/',
description: '德国新闻社卫健新闻',
item,
};
};