-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcodeforces_contest_tracker.js
171 lines (145 loc) · 5.12 KB
/
codeforces_contest_tracker.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// It's not JS, its GS
function fetchLatestContestAndRanks() {
var sheetID = "1h5AXAPMB6t-eNESvXDloCx7WatqgWlOWQZG17QQDDE0"; // PB
var sheet = SpreadsheetApp.openById(sheetID).getSheetByName("CodeForce");
const data = sheet.getDataRange().getValues();
if (data.length < 2) {
SpreadsheetApp.getUi().alert(
"The sheet is empty or does not have enough data."
);
return;
}
const headerRow = data[0];
const idColIndex = headerRow.indexOf("CODEFORCES ID");
const nameColIndex = headerRow.indexOf("NAME");
if (idColIndex === -1 || nameColIndex === -1) {
SpreadsheetApp.getUi().alert(
"Ensure columns have 'CODEFORCES ID' and 'Name' headers."
);
return;
}
let latestContestName = "";
let latestContestId = "";
try {
const contestApiUrl = "https://codeforces.com/api/contest.list?gym=false";
const contestResponse = UrlFetchApp.fetch(contestApiUrl);
const contestResult = JSON.parse(contestResponse.getContentText());
if (contestResult.status === "OK") {
const contests = contestResult.result;
for (let contest of contests) {
if (contest.phase === "FINISHED") {
latestContestName = contest.name;
latestContestId = contest.id;
break;
}
}
} else {
throw new Error("Failed to fetch contests.");
}
} catch (error) {
SpreadsheetApp.getUi().alert(
"Failed to fetch the latest contest information."
);
return;
}
let latestContestColIndex = headerRow.indexOf(latestContestName);
if (latestContestColIndex === -1) {
latestContestColIndex = headerRow.length;
sheet.getRange(1, latestContestColIndex + 1).setValue(latestContestName);
}
const nonParticipants = [];
const nonParticipantsLink = [];
const nonParticipantsNames = [];
for (let i = 1; i < data.length; i++) {
let codeforcesID = data[i][idColIndex];
let userName = data[i][nameColIndex];
if (!codeforcesID) {
continue;
}
if (codeforcesID.startsWith("https://codeforces.com/profile/")) {
codeforcesID = codeforcesID.split("/profile/")[1];
}
try {
const rankApiUrl = `https://codeforces.com/api/contest.standings?contestId=${latestContestId}&handles=${codeforcesID}`;
const rankResponse = UrlFetchApp.fetch(rankApiUrl, {
muteHttpExceptions: true,
});
const rankResult = JSON.parse(rankResponse.getContentText());
const ratingApiUrl = `https://codeforces.com/api/user.rating?handle=${codeforcesID}`;
const ratingResponse = UrlFetchApp.fetch(ratingApiUrl, {
muteHttpExceptions: true,
});
const ratingResult = JSON.parse(ratingResponse.getContentText());
if (rankResult.status === "OK") {
let contestRank = "NO";
if (rankResult.result.rows.length > 0) {
contestRank = rankResult.result.rows[0].rank;
}
let userRating = "No Rating Data";
if (ratingResult.status === "OK" && ratingResult.result.length > 0) {
const latestRating =
ratingResult.result[ratingResult.result.length - 1];
userRating = latestRating.newRating;
}
let formattedResult;
if (contestRank === "NO") {
formattedResult = ` - `;
nonParticipants.push(userName);
nonParticipantsLink.push(
`https://codeforces.com/profile/${codeforcesID}`
);
} else {
formattedResult = `YES , S : (${contestRank}) Rank : (${userRating})`;
}
sheet
.getRange(i + 1, latestContestColIndex + 1)
.setValue(formattedResult);
} else {
sheet.getRange(i + 1, latestContestColIndex + 1).setValue(" - ");
}
} catch (error) {
sheet.getRange(i + 1, latestContestColIndex + 1).setValue("-");
}
}
if (nonParticipants.length > 0) {
sendEmail(nonParticipants, nonParticipantsLink, latestContestName);
}
}
/**
* Send email with a list of non-participants.
* @param {Array} nonParticipants - List of usernames who didn't participate.
* @param {Array} nonParticipantsLink - List of URLs to non-participants' profiles.
* @param {string} contestName - The name of the contest.
*/
function sendEmail(nonParticipants, nonParticipantsLink, contestName) {
var index = 0;
const emailBody = `<html>
<body>
<h3>Non-Participants in ${contestName}</h3>
<p>The following users did not participate in the latest contest:</p>
<ul>
${nonParticipants
.map((user) => {
const profileUrl = nonParticipantsLink[index];
index++;
return `<li><a href="${profileUrl}" target="_blank">${user}</a></li>`;
})
.join("")}
</ul>
</body>
</html>`;
const emailSubject = `Codeforces Non-Participants Report for ${contestName}`;
const emails = [
];
for (var i = 0; i < emails.length; i++) {
MailApp.sendEmail({
to: emails[i],
subject: emailSubject,
htmlBody: emailBody,
});
}
}