-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathleetcode_contest_tracker.js
99 lines (83 loc) · 3.2 KB
/
leetcode_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
function updateLeetCodeRatings() {
var sheetID = "1h5AXAPMB6t-eNESvXDloCx7WatqgWlOWQZG17QQDDE0" // PB
const spreadsheet = SpreadsheetApp.openById(sheetID)
const sheet = spreadsheet.getSheetByName("Leetcode");
if (!sheet) {
Logger.log("Sheet named 'Leetcode' not found!");
return;
}
const dataRange = sheet.getDataRange();
const data = dataRange.getValues();
const lastColumnIndex = sheet.getLastColumn();
const newContestName = `Weekly Contest ${429 + lastColumnIndex - 3}`;
const newContestColumnIndex = lastColumnIndex + 1;
sheet.getRange(1, newContestColumnIndex).setValue(newContestName);
const nonParticipants = [];
const nonParticipantsLinks = [];
for (let i = 1; i < data.length; i++) {
const profileLink = data[i][2]; // URL is in column C
const previousRating = data[i][lastColumnIndex - 1] ? data[i][lastColumnIndex - 1].toString().trim() : null;
let username = "";
if (profileLink) {
username = extractUsernameFromUrl(profileLink);
}
let currentRating = "";
if (username) {
currentRating = getLeetCodeRating(username);
}
sheet.getRange(i + 1, newContestColumnIndex).setValue(currentRating || "No rating");
if (currentRating && currentRating === previousRating) {
nonParticipants.push(username || "Unknown User");
nonParticipantsLinks.push(profileLink);
}
}
if (nonParticipants.length > 0) {
sendNonParticipationEmail(nonParticipants, nonParticipantsLinks);
}
}
function extractUsernameFromUrl(url) {
try {
const parts = url.split("/");
return parts[parts.length - 2] || "";
} catch (error) {
Logger.log(`Error extracting username from URL (${url}): ${error}`);
return "";
}
}
function getLeetCodeRating(username) {
const url = `https://pb-cp-tracker.onrender.com/leetcode?username=${username}`;
try {
const response = UrlFetchApp.fetch(url);
const rating = response.getContentText().trim();
// Handle "unrated" or valid ratings
return rating === "unrated" ? "Unrated" : rating;
} catch (error) {
Logger.log(`Error fetching rating for ${username}: ${error}`);
return "Error fetching data";
}
}
function sendNonParticipationEmail(nonParticipants, nonParticipantsLinks) {
const emailBody = `
<html>
<body>
<h3>Non-Participants in Latest Contest</h3>
<p>The following users did not participate in the latest contest:</p>
<ul>
${nonParticipants.map((user, index) => {
const profileUrl = nonParticipantsLinks[index];
return `<li><a href="${profileUrl}" target="_blank">${user}</a></li>`;
}).join('')}
</ul>
</body>
</html>
`;
const emailSubject = "LeetCode Non-Participants Report";
const recipientEmails = ['[email protected]' ,'[email protected]', '[email protected]','[email protected]'];
for (const email of recipientEmails) {
MailApp.sendEmail({
to: email,
subject: emailSubject,
htmlBody: emailBody
});
}
}