Files
RSSHub/lib/routes/gitea/blog.js
2018-12-26 18:35:10 +08:00

77 lines
2.2 KiB
JavaScript

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,
};
};