feat: add 洛谷日报文章全文 (#5503)

This commit is contained in:
Ethan Shen 2020-08-24 19:23:28 +08:00 committed by GitHub
parent 7539783629
commit 757708ca95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -564,13 +564,11 @@ GitHub 官方也提供了一些 RSS:
### 日报
<Route author="LogicJake prnake" example="/luogu/daily" path="/luogu/daily/:id?" :paramsDesc="['年度日报所在帖子id可在 URL 中找到不填默认为2020年日报']">
</Route>
<Route author="LogicJake prnake nczitzk" example="/luogu/daily" path="/luogu/daily/:id?" :paramsDesc="['年度日报所在帖子id可在 URL 中找到不填默认为2020年日报']"/>
### 近期比赛
<Route author="prnake" example="/luogu/contest" path="/luogu/contest">
</Route>
<Route author="prnake" example="/luogu/contest" path="/luogu/contest"/>
## 码农俱乐部

View File

@ -13,16 +13,42 @@ module.exports = async (ctx) => {
.map(function () {
const info = {
title: $(this).find('strong').text() || $(this).text(),
description: $(this).html(),
link: $(this).find('a').attr('href'),
};
return info;
})
.get();
const items = await Promise.all(
out.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
const timeRegExp = new RegExp(/(?<=([1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d))/);
item.description = content('#article-content').html();
content('#article-content').remove();
const line = content
.html()
.split('\n')
.find((line) => timeRegExp.test(line));
item.pubDate = new Date(line.match(timeRegExp)[1] + ' GMT+8').toUTCString();
return item;
})
)
);
ctx.state.data = {
title: title,
link: link,
item: out,
item: items,
};
};