diff --git a/index.js b/index.js index 07c7b08..beb1b82 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,19 @@ const mdTemplate = ` \`\`\`{{lang}} {{code}} \`\`\` +##Note +\`\`\` +{{question_note}} +\`\`\` +##QuestionContent +\`\`\` +{{question_content}} +\`\`\` `; const header = ''; const footer = ''; -const waitTime = 200; +const waitTime = 300; +const backOffTime = 10_000; // wait 10 seconds to backoff on rate-limit const onlyFetchFirstPage = false; // config end @@ -34,6 +43,48 @@ 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 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 difficulty\n topicTags{\n name\n id\n slug}\n }\n}\n", + variables: { "titleSlug": questionSlug } + }), + success: function (data) { + resolve(data); + }, + error: function () { + resolve('failed'); + }, + }); + }); +} + async function getSolution(url) { return new Promise((resolve) => { $.ajax({ @@ -53,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; } @@ -90,12 +149,33 @@ for (let i = 0; i < accepts.length; i++) { codeObj = eval(content.slice(start, end)); console.log(codeObj); + let note_content = await getNoteForQuestion(item.title_slug); + while (note_content == 'failed') { + await pause(waitTime); + note_content = await getNoteForQuestion(item.title_slug); + } + 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; + 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({ title: item.title, code: codeObj.submissionCode, url: `https://leetcode.com${codeObj.editCodeUrl}description/`, questionId: codeObj.questionId, lang: item.lang, + question_note: question_note, + question_content: question_content, + question_difficulty: question_difficulty, + question_topics: topic_tags }); } solutions.sort((a, b) => parseInt(a.questionId) - parseInt(b.questionId)) @@ -124,4 +204,6 @@ const saveData = (function () { }; }()); -saveData(content, 'README.md') +// saveData(content, 'README.md') +saveData(JSON.stringify(solutions), 'LEETCODE.json') +