RSSHub/lib/routes/codeforces/contests.tsx

76 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dayjs/locale/zh-cn.js';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration.js';
import localizedFormat from 'dayjs/plugin/localizedFormat.js';
import relativeTime from 'dayjs/plugin/relativeTime.js';
import { renderToString } from 'hono/jsx/dom/server';
import type { Route } from '@/types';
import ofetch from '@/utils/ofetch';
dayjs.extend(localizedFormat);
dayjs.extend(duration);
dayjs.extend(relativeTime);
dayjs.locale('zh-cn');
const sec2str = (sec) => dayjs.duration(Number.parseInt(sec), 'seconds').humanize();
const contestAPI = 'https://codeforces.com/api/contest.list';
export const route: Route = {
path: '/contests',
categories: ['programming'],
example: '/codeforces/contests',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.codeforces.com/contests'],
},
],
name: 'Latest contests',
maintainers: ['Fatpandac'],
handler,
url: 'www.codeforces.com/contests',
};
async function handler() {
const contestsData = await ofetch(contestAPI);
const contests = contestsData.result;
const items = contests
.filter((contests) => contests.phase === 'BEFORE')
.map((contest) => {
const title = String(contest.name);
const date = dayjs.unix(Number.parseInt(contest.startTimeSeconds));
const description = renderToString(
<>
<p>{title}</p>
<p>{date.format('LL LT')}</p>
<p>{sec2str(contest.durationSeconds)}</p>
<p>{contest.type}</p>
</>
);
return {
title,
description,
link: 'https://codeforces.com/contests/' + contest.id,
};
});
return {
title: 'Codeforces - Contests',
link: 'https://codeforces.com/contests',
item: items,
};
}