forked from tsg-ut/slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagol.ts
95 lines (88 loc) · 1.98 KB
/
anagol.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import scrapeIt from 'scrape-it';
export interface Problem {
url: string;
problemId: string;
}
export interface Submission {
rank: string;
user: string;
size: number;
time: number;
date: Date;
statistics: string;
url: string | null;
}
interface SubmissionsData {
languages: {id: string}[];
byLanguage: {submissions: Submission[]}[];
}
export const crawlStandings = async (problemId: string, languageId: string): Promise<Submission[]> => {
const url = `https://golf.shinh.org/p.rb?${problemId}`;
const {data} = await scrapeIt<SubmissionsData>(url, {
languages: {
listItem: 'body > h3',
data: {
id: {
selector: 'a:nth-child(1)',
attr: 'href',
convert: text => text.split('?')[1],
},
},
},
byLanguage: {
listItem: 'body > table',
data: {
submissions: {
listItem: 'tr:not(:first-child)',
data: {
rank: {
selector: 'td:nth-child(1)',
convert: text => parseInt(text),
},
user: {
selector: 'td:nth-child(2)',
},
size: {
selector: 'td:nth-child(3)',
convert: text => parseInt(text),
},
time: {
selector: 'td:nth-child(4)',
convert: text => Number(text),
},
date: {
selector: 'td:nth-child(5)',
convert: text => new Date(text),
},
statistics: {
selector: 'td:nth-child(6)',
},
url: {
selector: 'td:nth-child(2) > a',
attr: 'href',
convert: text => (text ? new URL(text, url).toString() : null),
},
},
},
},
},
});
const index = data.languages.findIndex(l => l.id === languageId);
if (index < 0) {
return [];
} else {
return data.byLanguage[index]?.submissions ?? [];
}
};
interface SubmissionData {
code: string | null;
}
export const crawlSourceCode = async (url: string): Promise<string | null> => {
const {data} = await scrapeIt<SubmissionData>(url, {
code: {
selector: 'body > pre',
convert: text => text || null,
},
});
return data.code;
};