feat: add typora/changelog router (#1122)

This commit is contained in:
cnzgray
2018-11-12 23:15:28 +08:00
committed by DIYgod
parent 9db7abafc5
commit 574bf39ac8
3 changed files with 77 additions and 0 deletions

View File

@@ -1089,6 +1089,10 @@ GitHub 官方也提供了一些 RSS:
<route name="应用更新" author="DIYgod" example="/xclient/app/sketch" path="/xclient/app/:name" :paramsDesc="['应用名, 可在应用页 URL 中找到']"/>
### Typora
<route name="Changelog" author="cnzgray" example="/typora/changelog" path="/typora/changelog"/>
## 大学通知
### 上海海事大学

View File

@@ -790,6 +790,9 @@ router.get('/eeo/:category?', require('./routes/eeo/index'));
// 腾讯视频
router.get('/tencentvideo/playlist/:id', require('./routes/tencent/video/playlist'));
// typora
router.get('/typora/changelog', require('./routes/typora/changelog'));
// TSSstatus
router.get('/tssstatus/:board/:build', require('./routes/tssstatus'));

View File

@@ -0,0 +1,70 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const host = 'https://support.typora.io/';
const response = await axios({
method: 'get',
url: host,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
const parseContent = async (link) => {
// Check cache
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await axios({
method: 'get',
url: link,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
const title = $('h1').text();
const pubDate = new Date($('.post-meta time').text());
// const author = $('.post-meta span').text();
const html = $('#pagecontainer').html();
const result = {
title: title,
link: link,
guid: link,
pubDate: pubDate,
description: html,
};
ctx.cache.set(link, JSON.stringify(result), 3 * 60 * 60);
return result;
};
const items = await Promise.all(
$('#content > ul:nth-child(2) > li')
.get()
.map(async (item) => {
const node = $('a', item);
const link = node.attr('href');
const result = await parseContent(link);
return Promise.resolve(result);
})
);
ctx.state.data = {
title: 'Typora Changelog',
link: host,
description: 'Typora Changelog',
item: items,
};
};