fetch question detail from lc's api
(question_title_slug)
| 24 | |
| 25 | |
| 26 | def get_question_detail(question_title_slug): |
| 27 | """fetch question detail from lc's api""" |
| 28 | form_data = { |
| 29 | "operationName": "globalData", |
| 30 | "query": "query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n " |
| 31 | "discuss\n mockInterview\n contest\n store\n book\n chinaProblemDiscuss\n " |
| 32 | "socialProviders\n studentFooter\n cnJobs\n enableLsp\n enableWs\n " |
| 33 | "enableDebugger\n enableDebuggerAdmin\n enableDarkMode\n tasks\n " |
| 34 | "leetbook\n __typename\n }\n userStatus {\n isSignedIn\n isAdmin\n " |
| 35 | "isStaff\n isSuperuser\n isTranslator\n isPremium\n isVerified\n " |
| 36 | "isPhoneVerified\n isWechatVerified\n checkedInToday\n username\n " |
| 37 | "realName\n userSlug\n groups\n avatar\n optedIn\n " |
| 38 | "requestRegion\n region\n activeSessionId\n permissions\n notificationStatus {\n " |
| 39 | "lastModified\n numUnread\n __typename\n }\n completedFeatureGuides\n " |
| 40 | "useTranslation\n accountStatus {\n isFrozen\n inactiveAfter\n __typename\n " |
| 41 | "}\n __typename\n }\n siteRegion\n chinaHost\n websocketUrl\n userBannedInfo {\n " |
| 42 | "bannedData {\n endAt\n bannedType\n __typename\n }\n __typename\n }\n}\n", |
| 43 | "variables": {}, |
| 44 | } |
| 45 | headers = { |
| 46 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " |
| 47 | "Chrome/77.0.3865.120 Safari/537.36", |
| 48 | "Connection": "keep-alive", |
| 49 | "Content-Type": "application/json", |
| 50 | "Referer": "https://leetcode.cn/problems/" + question_title_slug, |
| 51 | # lc-cn cookie here |
| 52 | "cookie": "", |
| 53 | } |
| 54 | requests.post( |
| 55 | url="https://leetcode.cn/graphql", |
| 56 | data=json.dumps(form_data), |
| 57 | headers=headers, |
| 58 | timeout=10, |
| 59 | verify=False, |
| 60 | ) |
| 61 | |
| 62 | form_data = { |
| 63 | "operationName": "questionData", |
| 64 | "variables": {"titleSlug": question_title_slug}, |
| 65 | "query": "query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n " |
| 66 | "questionId\n questionFrontendId\n categoryTitle\n boundTopicId\n title\n " |
| 67 | "titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n " |
| 68 | "difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n " |
| 69 | "contributors {\n username\n profileUrl\n avatarUrl\n __typename\n " |
| 70 | "}\n langToValidPlayground\n topicTags {\n name\n slug\n " |
| 71 | "translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n " |
| 72 | "lang\n langSlug\n code\n __typename\n }\n stats\n hints\n " |
| 73 | "solution {\n id\n canSeeDetail\n __typename\n }\n status\n " |
| 74 | "sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n " |
| 75 | "enableRunCode\n envInfo\n book {\n id\n bookName\n pressName\n " |
| 76 | "source\n shortDescription\n fullDescription\n bookImgUrl\n " |
| 77 | "pressImgUrl\n productUrl\n __typename\n }\n isSubscribed\n " |
| 78 | "isDailyQuestion\n dailyRecordStatus\n editorType\n ugcQuestionId\n style\n " |
| 79 | "exampleTestcases\n __typename\n }\n}\n", |
| 80 | } |
| 81 | |
| 82 | # get question detail |
| 83 | resp = requests.post( |