Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ce97a3

Browse files
author
msingh
committed
commit all code for fetch current date submissions and add to notion db
1 parent 0684696 commit 6ce97a3

File tree

7 files changed

+440
-0
lines changed

7 files changed

+440
-0
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

‎config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"leetcodeUsername": "msn2106",
3+
"notionApiKey": "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",
4+
"notionParentPageId": "your_notion_parent_page_id",
5+
"NOTION_DATABASE_NAME": "LeetCode Problems"
6+
}

‎index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
const config = require("./config.json");
3+
const { fetchLeetCodeSubmissions, filterTodaySubmissions } = require('./leetcode.js');
4+
const { getOrCreateNotionDatabase, addSubmissionsToNotion } = require("./notion.js");
5+
6+
async function main() {
7+
const submissions = await fetchLeetCodeSubmissions();
8+
console.log(`Recent AC Submissions found : ${submissions.length}`);
9+
const currentDaySubmissions = filterTodaySubmissions(submissions);
10+
console.log(`Current Day AC Submissions found : ${currentDaySubmissions.length}`);
11+
if (!currentDaySubmissions.length) {
12+
console.log("No submissions found.");
13+
return;
14+
}
15+
16+
const databaseId = await getOrCreateNotionDatabase();
17+
await addSubmissionsToNotion(databaseId, currentDaySubmissions);
18+
console.log(`Current Day Submissions added to Notion. Submissions: ${JSON.stringify(currentDaySubmissions)}`);
19+
}
20+
21+
main();

‎leetcode.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const axios = require("axios");
2+
const config = require("./config.json");
3+
4+
async function fetchProblemDetails(titleSlug) {
5+
const graphqlUrl = "https://leetcode.com/graphql";
6+
const query = `
7+
query getProblemDetails($titleSlug: String!) {
8+
question(titleSlug: $titleSlug) {
9+
questionId
10+
title
11+
difficulty
12+
topicTags {
13+
name
14+
}
15+
}
16+
}
17+
`;
18+
19+
try {
20+
const response = await axios.post(graphqlUrl, {
21+
query,
22+
variables: { titleSlug },
23+
});
24+
25+
const question = response.data.data.question;
26+
27+
return {
28+
id: question.questionId,
29+
name: question.title,
30+
difficulty: question.difficulty,
31+
tags: question.topicTags.map((tag) => tag.name),
32+
};
33+
} catch (error) {
34+
console.error("Error fetching problem details:", error.message);
35+
return null;
36+
}
37+
}
38+
39+
async function fetchLeetCodeSubmissions() {
40+
const graphqlUrl = "https://leetcode.com/graphql";
41+
const query = `
42+
query recentAcSubmissions($username: String!) {
43+
recentAcSubmissionList(username: $username) {
44+
title
45+
titleSlug
46+
timestamp
47+
}
48+
}
49+
`;
50+
51+
try {
52+
const response = await axios.post(graphqlUrl, {
53+
query,
54+
variables: {
55+
username: config.leetcodeUsername,
56+
},
57+
});
58+
59+
const submissions = response.data.data.recentAcSubmissionList;
60+
61+
// Fetch additional details for each submission
62+
const detailedSubmissions = [];
63+
for (const submission of submissions) {
64+
const details = await fetchProblemDetails(submission.titleSlug);
65+
if (details) {
66+
detailedSubmissions.push({
67+
name: details.name,
68+
link: `https://leetcode.com/problems/${submission.titleSlug}`,
69+
date: new Date(submission.timestamp * 1000).toISOString(),
70+
status: "Accepted",
71+
difficulty: details.difficulty,
72+
tags: details.tags,
73+
problemNumber: details.id,
74+
});
75+
}
76+
}
77+
78+
return detailedSubmissions;
79+
} catch (error) {
80+
console.error("Error fetching LeetCode submissions:", error.message);
81+
return [];
82+
}
83+
}
84+
85+
function filterTodaySubmissions(submissions) {
86+
const startOfToday = new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), new Date().getUTCDate()));
87+
const endOfToday = new Date(startOfToday);
88+
endOfToday.setUTCDate(startOfToday.getUTCDate() + 1);
89+
90+
const resp = submissions.filter((submission) => {
91+
const submissionDate = new Date(submission.date); // LeetCode timestamps are in seconds
92+
return submissionDate >= startOfToday && submissionDate < endOfToday;
93+
});
94+
return resp;
95+
}
96+
97+
98+
module.exports = {
99+
fetchLeetCodeSubmissions,
100+
filterTodaySubmissions
101+
}

‎notion.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
const config = require("./config.json");
3+
const { Client } = require("@notionhq/client");
4+
5+
const notion = new Client({ auth: config.notionApiKey });
6+
7+
async function getOrCreateNotionDatabase() {
8+
try {
9+
// List databases to check existence
10+
const databases = await notion.search({
11+
query: config.NOTION_DATABASE_NAME,
12+
});
13+
if (databases.results.length > 0) return databases.results[0].id;
14+
15+
// Create new database
16+
const response = await notion.databases.create({
17+
parent: { type: "page_id", page_id: config.notionParentPageId },
18+
title: [{ type: "text", text: { content: NOTION_DATABASE_NAME } }],
19+
properties: {
20+
Name: { title: {} },
21+
Link: { url: {} },
22+
Status: {
23+
select: {
24+
options: [
25+
{ name: "Accepted", color: "green" },
26+
{ name: "Wrong Answer", color: "red" },
27+
],
28+
},
29+
},
30+
Date: { date: {} },
31+
},
32+
});
33+
34+
return response.id;
35+
} catch (error) {
36+
console.error("Error setting up Notion database:", error);
37+
throw error;
38+
}
39+
}
40+
41+
async function addSubmissionToNotion(databaseId, submission) {
42+
try {
43+
await notion.pages.create({
44+
parent: { database_id: databaseId },
45+
properties: {
46+
// Correctly define the Problem Name field (as a title)
47+
"Problem Name": {
48+
title: [
49+
{
50+
text: { content: submission.name || "Untitled Problem" }, // Provide fallback if name is missing
51+
},
52+
],
53+
},
54+
// Correctly define the Problem Number field (as a number)
55+
"Problem Number": {
56+
number: parseInt(submission.problemNumber) || null, // Ensure null if no problem number is provided
57+
},
58+
// Correctly define the Link field (as a URL)
59+
Link: {
60+
url: submission.link || null, // Ensure null if no link is provided
61+
},
62+
// Correctly define the Submission Date field (as a date)
63+
"Submission Date": {
64+
date: submission.date
65+
? { start: submission.date } // Ensure date is properly structured
66+
: null,
67+
},
68+
// Correctly define the Tags field (as a multi-select)
69+
Tags: {
70+
multi_select: submission.tags
71+
? submission.tags.map((tag) => ({ name: tag }))
72+
: [],
73+
},
74+
// Correctly define the Difficulty field (as a select)
75+
Difficulty: {
76+
select: submission.difficulty
77+
? { name: submission.difficulty }
78+
: null,
79+
},
80+
// Correctly define the Status field (as a select)
81+
Status: {
82+
select: submission.status ? { name: submission.status } : null,
83+
},
84+
},
85+
});
86+
87+
console.log(`Added "${submission.name}" to Notion successfully.`);
88+
} catch (error) {
89+
console.error("Error adding submission to Notion:", error.message);
90+
}
91+
}
92+
93+
async function addSubmissionsToNotion(databaseId, submissions) {
94+
for (const submission of submissions) {
95+
await addSubmissionToNotion(databaseId, submission);
96+
}
97+
}
98+
99+
module.exports = {
100+
getOrCreateNotionDatabase,
101+
addSubmissionsToNotion
102+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /