增加`gitea/blog`路由 (#1126)

This commit is contained in:
cnzgray 2018-11-13 11:06:17 +08:00 committed by DIYgod
parent d1c50b0c7e
commit 049d6024be
3 changed files with 87 additions and 0 deletions

View File

@ -745,6 +745,14 @@ GitHub 官方也提供了一些 RSS:
> GitChat 需要付费订阅, RSS 仅做更新提醒, 不含付费内容.
### Gitea
<route name="博客" author="cnzgray" example="/gitea/blog" path="/gitea/blog">
> gitea 博客一般发布最新的 release 信息,路由选择用 blog 名称主要因为其地址名为 blog而非 changlog慎重起见还是用 blog 命名。
</route>
## 直播
### 哔哩哔哩直播

View File

@ -808,6 +808,9 @@ router.get('/tssstatus/:board/:build', require('./routes/tssstatus'));
router.get('/anime1/anime/:time/:name', require('./routes/anime1/anime'));
router.get('/anime1/search/:keyword', require('./routes/anime1/search'));
// gitea
router.get('/gitea/blog', require('./routes/gitea/blog'));
// iDownloadBlog
router.get('/idownloadblog', require('./routes/idownloadblog/index'));

76
routes/gitea/blog.js Normal file
View File

@ -0,0 +1,76 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const host = 'https://blog.gitea.io/';
const response = await axios({
method: 'get',
url: host,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
const loadContent = async (link) => {
// Check cache
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(cache);
}
const response = await axios({
method: 'get',
url: link,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
$('section .content script').remove();
$('section .content #discourse-comments').remove();
const html = $('section .content').html();
ctx.cache.set(link, html, 3 * 60 * 60);
return html;
};
const items = await Promise.all(
$('.card .card-content')
.get()
.map(async (item) => {
const title = $('.media .media-content .title', item).text();
const subtitle = $('.media .media-content .subtitle', item)
.text()
.replace(/\t/g, '')
.replace(/\n/g, ' ')
.trim();
const matchs = /(.*) by (.*)/.exec(subtitle);
const pubDate = matchs && matchs.length === 3 ? new Date(matchs[1]) : new Date();
const author = matchs && matchs.length === 3 ? matchs[2] : '';
const link = $(item)
.children('a')
.attr('href');
const desc = await loadContent(link);
const single = {
title: title,
link: link,
guid: link,
pubDate: pubDate,
description: `<p>${author}</p>${desc}`,
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'Gitea Blog',
link: host,
description: 'Gitea Blog',
item: items,
};
};