From 6170505217dbf680ef99e265cf9b4880ad959711 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 26 Oct 2024 18:18:22 +0530 Subject: [PATCH 1/8] Update index.js Update to add a call for question note saved by user, and update the readme accordingly --- index.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 07c7b08..38fcc0e 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,14 @@ const mdTemplate = ` \`\`\`{{lang}} {{code}} \`\`\` +##Note +\`\`\` +{{question_note}} +\`\`\` `; const header = ''; const footer = ''; -const waitTime = 200; +const waitTime = 300; const onlyFetchFirstPage = false; // config end @@ -34,6 +38,27 @@ async function getSubmission(page) { }); } +async function getNoteForQuestion(questionSlug) { + var url = `/graphql`; + return new Promise((resolve, reject) => { + $.ajax({ + url: url, + type: "POST", + contentType: "application/json", + data: JSON.stringify({ + query: "query questionNote($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n note\n }\n}\n", + variables: { "titleSlug": questionSlug } + }), + success: function (data) { + resolve(data); + }, + error: function () { + resolve('failed'); + }, + }); + }); +} + async function getSolution(url) { return new Promise((resolve) => { $.ajax({ @@ -90,12 +115,20 @@ for (let i = 0; i < accepts.length; i++) { codeObj = eval(content.slice(start, end)); console.log(codeObj); + let note_content = await getNoteForQuestion(item.titleSlug); + while (response == 'failed') { + await pause(waitTime); + note_content = await getNoteForQuestion(item.titleSlug); + } + let question_note = response.data.question.note; + solutions.push({ title: item.title, code: codeObj.submissionCode, url: `https://leetcode.com${codeObj.editCodeUrl}description/`, questionId: codeObj.questionId, lang: item.lang, + question_note: question_note }); } solutions.sort((a, b) => parseInt(a.questionId) - parseInt(b.questionId)) From 03cdb279a0cdd573761f5381c2cad3267482e31e Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 26 Oct 2024 18:21:38 +0530 Subject: [PATCH 2/8] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 38fcc0e..c376b6d 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,7 @@ for (let i = 0; i < accepts.length; i++) { await pause(waitTime); note_content = await getNoteForQuestion(item.titleSlug); } - let question_note = response.data.question.note; + let question_note = note_content.data.question.note; solutions.push({ title: item.title, From a361c703034d67791e6f6167a8d8867105c00035 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 26 Oct 2024 18:22:02 +0530 Subject: [PATCH 3/8] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c376b6d..ac415e8 100644 --- a/index.js +++ b/index.js @@ -116,7 +116,7 @@ for (let i = 0; i < accepts.length; i++) { console.log(codeObj); let note_content = await getNoteForQuestion(item.titleSlug); - while (response == 'failed') { + while (note_content == 'failed') { await pause(waitTime); note_content = await getNoteForQuestion(item.titleSlug); } From 6f021ea11350c78cff3a9ed59d91e24843ddb75b Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 26 Oct 2024 18:27:31 +0530 Subject: [PATCH 4/8] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ac415e8..ae4357d 100644 --- a/index.js +++ b/index.js @@ -115,10 +115,10 @@ for (let i = 0; i < accepts.length; i++) { codeObj = eval(content.slice(start, end)); console.log(codeObj); - let note_content = await getNoteForQuestion(item.titleSlug); + let note_content = await getNoteForQuestion(item.title_slug); while (note_content == 'failed') { await pause(waitTime); - note_content = await getNoteForQuestion(item.titleSlug); + note_content = await getNoteForQuestion(item.title_slug); } let question_note = note_content.data.question.note; From f38b540749924f95403f2b8cb186185811a73c47 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sun, 27 Oct 2024 18:41:06 +0530 Subject: [PATCH 5/8] Update index.js --- index.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ae4357d..58c769b 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,10 @@ const mdTemplate = ` \`\`\` {{question_note}} \`\`\` +##QuestionContent +\`\`\` +{{question_content}} +\`\`\` `; const header = ''; const footer = ''; @@ -59,6 +63,27 @@ async function getNoteForQuestion(questionSlug) { }); } +async function getQuestionContent(questionSlug) { + var url = `/graphql`; + return new Promise((resolve, reject) => { + $.ajax({ + url: url, + type: "POST", + contentType: "application/json", + data: JSON.stringify({ + query: "query questionContent($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n content\n }\n}\n", + variables: { "titleSlug": questionSlug } + }), + success: function (data) { + resolve(data); + }, + error: function () { + resolve('failed'); + }, + }); + }); +} + async function getSolution(url) { return new Promise((resolve) => { $.ajax({ @@ -122,13 +147,22 @@ for (let i = 0; i < accepts.length; i++) { } let question_note = note_content.data.question.note; + let q_content = await getQuestionContent(item.title_slug); + while (q_content == 'failed') { + await pause(waitTime); + q_content = await getQuestionContent(item.title_slug); + } + let question_content = q_content.data.question.content; + question_content = question_content.replace(/\n/g, ' ').replace(/\t/g, ' '); + solutions.push({ title: item.title, code: codeObj.submissionCode, url: `https://leetcode.com${codeObj.editCodeUrl}description/`, questionId: codeObj.questionId, lang: item.lang, - question_note: question_note + question_note: question_note, + question_content: question_content }); } solutions.sort((a, b) => parseInt(a.questionId) - parseInt(b.questionId)) From d876b0faed06d5e4c4343be44ad85e53bf395396 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sun, 10 Nov 2024 02:55:03 +0530 Subject: [PATCH 6/8] Update index.js to output a json --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 58c769b..e40c773 100644 --- a/index.js +++ b/index.js @@ -191,4 +191,6 @@ const saveData = (function () { }; }()); -saveData(content, 'README.md') +// saveData(content, 'README.md') +saveData(JSON.stringify(solutions), 'LEETCODE.json') + From 23ba24aae37da10cfb24e8bb18fc0edf7e1f4006 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Wed, 12 Feb 2025 01:11:32 +0530 Subject: [PATCH 7/8] Add longer backoff on failed request. --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e40c773..4dcf82c 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ const mdTemplate = ` const header = ''; const footer = ''; const waitTime = 300; +const backOffTime = 10_000; // wait 10 seconds to backoff on rate-limit const onlyFetchFirstPage = false; // config end @@ -103,16 +104,24 @@ let lastkey = ''; const submissions = []; for (let i = 0; onlyFetchFirstPage ? i < 1 : true; i++) { await pause(waitTime); + let retry_count = 0; let data = await getSubmission(i); while (data == 'failed') { - console.log('retry'); - await pause(waitTime); + console.log('retry after 10s'); + await pause(backOffTime); data = await getSubmission(i); + retry_count+=1; + if (retry_count > 20){break} } - + console.log('success'); [].push.apply(submissions, data.submissions_dump); + if (retry_count > 20) { + console.log('Quitting, retried max times with failed result'); + break; + } + if (!data.has_next) { break; } From c6be9547b7248dd13358326f7db7278c5bf95fdf Mon Sep 17 00:00:00 2001 From: Kashyap Date: Mon, 17 Feb 2025 17:40:22 +0530 Subject: [PATCH 8/8] Update index.js --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4dcf82c..beb1b82 100644 --- a/index.js +++ b/index.js @@ -72,7 +72,7 @@ async function getQuestionContent(questionSlug) { type: "POST", contentType: "application/json", data: JSON.stringify({ - query: "query questionContent($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n content\n }\n}\n", + query: "query questionContent($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n content\n difficulty\n topicTags{\n name\n id\n slug}\n }\n}\n", variables: { "titleSlug": questionSlug } }), success: function (data) { @@ -162,6 +162,8 @@ for (let i = 0; i < accepts.length; i++) { q_content = await getQuestionContent(item.title_slug); } let question_content = q_content.data.question.content; + let question_difficulty = q_content.data.question.difficulty; + let topic_tags = q_content.data.question.topicTags.map(i => i.slug); question_content = question_content.replace(/\n/g, ' ').replace(/\t/g, ' '); solutions.push({ @@ -171,7 +173,9 @@ for (let i = 0; i < accepts.length; i++) { questionId: codeObj.questionId, lang: item.lang, question_note: question_note, - question_content: question_content + question_content: question_content, + question_difficulty: question_difficulty, + question_topics: topic_tags }); } solutions.sort((a, b) => parseInt(a.questionId) - parseInt(b.questionId))