feat: add hedwig.pub (#4122)

This commit is contained in:
zz
2020-03-01 12:40:44 +08:00
committed by GitHub
parent 4a138503b0
commit 16f477c842
3 changed files with 75 additions and 0 deletions

View File

@@ -16,6 +16,16 @@ pageClass: routes
<Route author="hoilc" example="/google/sites/outlierseconomics" path="/google/sites/:id" :paramsDesc="['Site ID, 可在 URL 中找到']" radar="1" />
## Hedwig.pub
<Route author="zwithz" example="/blogs/hedwig/zmd" path="/blogs/hedwig/:type" :paramsDesc="['分类, 见下表']"/>
| 呆唯的 Newsletter | 0neSe7en 的技术周刊 | 地心引力 | 宪学宪卖 | Comeet 每周精选 | 无鸡之谈 | 我有一片芝麻地 |
| ----------------- | ------------------- | -------- | -------- | --------------- | -------- | -------------- |
| hirasawayui | se7en | walnut | themez | comeet | sunskyxh | zmd |
> 原则上只要是{type}.hedwig.pub 都可以匹配。
## Hexo
### Next 主题博客

View File

@@ -2305,6 +2305,9 @@ router.get('/wolley/host/:host', require('./routes/wolley/host'));
// 西安交大
router.get('/xjtu/dean/:subpath+', require('./routes/universities/xjtu/dean'));
// 我有一片芝麻地
router.get('/blogs/hedwig/:type', require('./routes/blogs/hedwig'));
// LoveHeaven
router.get('/loveheaven/update/:slug', require('./routes/loveheaven/update'));

View File

@@ -0,0 +1,62 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const type = ctx.params.type;
const url = 'https://' + `${type}` + '.hedwig.pub';
const res = await got({
method: 'get',
url: url,
});
const $ = cheerio.load(res.data);
const title = $('div[class="StyledBox-sc-13pk1d4-0 heWGCm"]')
.find('h1')
.text();
const list = $('div[class="StyledBox-sc-13pk1d4-0 czKiZd"]')
.find('div[class="StyledBox-sc-13pk1d4-0 czKiZd"]')
.find('div[class="StyledBox-sc-13pk1d4-0 czKiZd"]')
.map((i, e) => {
const element = $(e);
const title = element
.find('a')
.find('h3')
.text();
const link = url + element.find('a').attr('href');
return {
title: title,
description: '',
link: link,
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link);
const itemElement = cheerio.load(itemReponse.data);
// Remove duplicate title
itemElement('.StyledBox-sc-13pk1d4-0 .bpdfin')
.find('h1[class="StyledHeading-sc-1rdh4aw-0 kIYNxP"]')
.remove();
item.description = itemElement('.StyledBox-sc-13pk1d4-0 .bpdfin').html();
ctx.cache.set(link, JSON.stringify(item));
return Promise.resolve(item);
})
);
ctx.state.data = {
title: 'Ⓙ Hedwig - ' + title,
link: 'https://' + `${type}` + '.hedwig.pub',
item: result,
};
};