Created
May 9, 2018 04:49
-
-
Save OldPanda/85c2a9ccf153c6f7bac23fe8dbdc504f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import psycopg2 | |
from lxml import html | |
from requests import session | |
DB_CONN = { | |
"host": "<your-db-host>", | |
"port": "<your-db-port>", | |
"user": "<your-username>", | |
"password": "<your-password>", | |
"database": "<your-database>" | |
} | |
headers = { | |
'cookie': '<your-cookie>' | |
} | |
s = session() | |
def parse_all_questions_page(): | |
url = "https://leetcode.com/api/problems/all/" | |
r = s.get(url, headers=headers) | |
questions = json.loads(r.text)["stat_status_pairs"] | |
for question in questions: | |
question_title = question['stat']['question__title_slug'] | |
write_to_db(*parse_question_page(question_title)) | |
def parse_question_page(question_title): | |
url = "https://leetcode.com/graphql?query=query%20getQuestionDetail(%24titleSlug%3A%20String!)%20%7B%0A%20%20isCurrentUserAuthenticated%0A%20%20question(titleSlug%3A%20%24titleSlug)%20%7B%0A%20%20%20%20questionId%0A%20%20%20%20questionFrontendId%0A%20%20%20%20questionTitle%0A%20%20%20%20translatedTitle%0A%20%20%20%20questionTitleSlug%0A%20%20%20%20content%0A%20%20%20%20translatedContent%0A%20%20%20%20difficulty%0A%20%20%20%20stats%0A%20%20%20%20contributors%0A%20%20%20%20similarQuestions%0A%20%20%20%20discussUrl%0A%20%20%20%20mysqlSchemas%0A%20%20%20%20randomQuestionUrl%0A%20%20%20%20sessionId%0A%20%20%20%20categoryTitle%0A%20%20%20%20submitUrl%0A%20%20%20%20interpretUrl%0A%20%20%20%20codeDefinition%0A%20%20%20%20sampleTestCase%0A%20%20%20%20enableTestMode%0A%20%20%20%20metaData%0A%20%20%20%20enableRunCode%0A%20%20%20%20enableSubmit%0A%20%20%20%20judgerAvailable%0A%20%20%20%20infoVerified%0A%20%20%20%20envInfo%0A%20%20%20%20urlManager%0A%20%20%20%20article%0A%20%20%20%20questionDetailUrl%0A%20%20%20%20discussCategoryId%0A%20%20%20%20discussSolutionCategoryId%0A%20%20%20%20libraryUrl%0A%20%20%20%20companyTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%20%20translatedName%0A%20%20%20%20%7D%0A%20%20%20%20topicTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%20%20translatedName%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20interviewed%20%7B%0A%20%20%20%20interviewedUrl%0A%20%20%20%20companies%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%7D%0A%20%20%20%20timeOptions%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%20%20stageOptions%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20subscribeUrl%0A%20%20isPremium%0A%20%20loginUrl%0A%7D%0A&operationName=getQuestionDetail&variables=%7B%22titleSlug%22%3A%22{name}%22%7D".format(name=question_title) | |
r = s.get(url, headers=headers) | |
question = json.loads(r.text) | |
question_id = int(question["data"]["question"]["questionId"]) | |
question_title = question["data"]["question"]["questionTitle"] | |
question_desc = question["data"]["question"]["content"] | |
difficulty = question["data"]["question"]["difficulty"].lower() | |
return question_id, question_title, question_desc, difficulty | |
def write_to_db(question_id, question_title, question_desc, difficulty): | |
with psycopg2.connect(**DB_CONN) as conn: | |
cur = conn.cursor() | |
cur.execute(""" | |
INSERT INTO questions (question_id, question_title, question_desc, difficulty) | |
VALUES (%s, %s, %s, %s)""", | |
(question_id, question_title, question_desc, difficulty)) | |
parse_all_questions_page() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment