/
contests.ts
77 lines (67 loc) 路 2.23 KB
/
contests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import got from '@/utils/got';
import path from 'node:path';
import { art } from '@/utils/render';
import dayjs from 'dayjs';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import duration from 'dayjs/plugin/duration';
import relativeTime from 'dayjs/plugin/relativeTime';
import 'dayjs/locale/zh-cn';
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 got.get(contestAPI).json();
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 = art(path.join(__dirname, 'templates/contest.art'), {
title,
startTime: date.format('LL LT'),
durationTime: sec2str(contest.durationSeconds),
// relativeTime: sec2str(contest.relativeTimeSeconds),
type: contest.type,
});
return {
title,
description,
link: 'https://codeforces.com/contests/' + contest.id,
};
});
return {
title: 'Codeforces - Contests',
link: 'https://codeforces.com/contests',
item: items,
};
}