feat(route): add AtCoder (#8825)

This commit is contained in:
Ethan Shen
2022-01-23 04:05:10 +08:00
committed by GitHub
parent 548136c56a
commit d95705b4db
7 changed files with 220 additions and 0 deletions

64
lib/v2/atcoder/contest.js Normal file
View File

@@ -0,0 +1,64 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const status = ['action', 'upcoming', 'recent'];
const language = ctx.params.language ?? 'en';
let rated = ctx.params.rated ?? '0';
const category = ctx.params.category ?? '0';
const keyword = ctx.params.keyword ?? '';
rated = rated === 'active' ? 'action' : rated;
const isStatus = status.includes(rated);
const rootUrl = 'https://atcoder.jp';
const currentUrl = `${rootUrl}/contests${isStatus ? `?lang=${language}` : `/archive?lang=${language}&ratedType=${rated}&category=${category}${keyword ? `&keyword=${keyword}` : ''}`}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $(isStatus ? `#contest-table-${rated}` : '.row')
.find('tr')
.slice(1, ctx.query.limit ? parseInt(ctx.query.limit) : 20)
.toArray()
.map((item) => {
item = $(item).find('td a').eq(1);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}?lang=${language}`,
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content(`.lang-${language}`).html();
item.pubDate = parseDate(content('.fixtime-full').first().text());
return item;
})
)
);
ctx.state.data = {
title: String(isStatus ? `${$(`#contest-table-${rated} h3`).text()} - AtCoder` : $('title').text()),
link: currentUrl,
item: items,
allowEmpty: true,
};
};

View File

@@ -0,0 +1,4 @@
module.exports = {
'/post/:language?/:keyword?': ['nczitzk'],
'/contest/:language?/:rated?/:category?/:keyword?': ['nczitzk'],
};

38
lib/v2/atcoder/post.js Normal file
View File

@@ -0,0 +1,38 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const language = ctx.params.language ?? 'en';
const keyword = ctx.params.keyword ?? '';
const rootUrl = 'https://atcoder.jp';
const currentUrl = `${rootUrl}/posts?lang=${language}${keyword ? `&keyword=${keyword}` : ''}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const items = $('.panel')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.find('.panel-title').text(),
description: item.find('.panel-body').html(),
link: `${rootUrl}${item.find('.panel-title a').attr('href')}`,
pubDate: timezone(parseDate(item.find('.timeago').attr('datetime')), +9),
};
});
ctx.state.data = {
title: `${keyword ? `[${keyword}] - ` : ''}${$('title').text()}`,
link: currentUrl,
item: items,
};
};

22
lib/v2/atcoder/radar.js Normal file
View File

@@ -0,0 +1,22 @@
module.exports = {
'atcoder.jp': {
_name: 'AtCoder',
'.': [
{
title: 'Posts',
docs: 'https://docs.rsshub.app/programming.html#atcoder-posts',
source: ['/posts', '/'],
target: (params, url) => `/atcoder/post/${new URL(url).searchParams.get('lang') ?? 'en'}/${new URL(url).searchParams.get('keyword') ?? ''}`,
},
{
title: 'Contests',
docs: 'https://docs.rsshub.app/programming.html#atcoder-contests',
source: ['/contests/archive', '/contests', ''],
target: (params, url) =>
`/atcoder/content/${new URL(url).searchParams.get('lang') ?? 'en'}/${new URL(url).searchParams.get('ratedType') ?? '0'}/${new URL(url).searchParams.get('category') ?? '0'}/${
new URL(url).searchParams.get('keyword') ?? ''
}`,
},
],
},
};

4
lib/v2/atcoder/router.js Normal file
View File

@@ -0,0 +1,4 @@
module.exports = function (router) {
router.get('/post/:language?/:keyword?', require('./post'));
router.get('/contest/:language?/:rated?/:category?/:keyword?', require('./contest'));
};