Skip to content

Commit d6a38c2

Browse files
committed
Add get_url_of_login_page to LeetCodeService
To enable logging in to LeetCode using https://github.com/online-judge-tools/oj/
1 parent 7f390f5 commit d6a38c2

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

onlinejudge/service/leetcode.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def from_url(cls, url: str) -> Optional['LeetCodeService']:
5555
return None
5656
return cls()
5757

58+
def get_url_of_login_page(self) -> str:
59+
return f'{self.get_url()}accounts/login/'
60+
5861
def is_logged_in(self, *, session: Optional[requests.Session] = None) -> bool:
5962
session = self._set_request_header(session)
6063
json_body = {

request_test.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import json
2+
import requests
3+
4+
s = requests.Session()
5+
6+
cookies = {
7+
'LEETCODE_SESSION': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfYXV0aF91c2VyX2lkIjoiMzQ4NjE5MSIsIl9hdXRoX3VzZXJfYmFja2VuZCI6ImRqYW5nby5jb250cmliLmF1dGguYmFja2VuZHMuTW9kZWxCYWNrZW5kIiwiX2F1dGhfdXNlcl9oYXNoIjoiYjBhNDA5NmNhMWFkZTc4ZDk1ZDA2ZTgzZDhiMmI2YjYzOWIwNzE1ZiIsImlkIjozNDg2MTkxLCJlbWFpbCI6Imx0LWNsaS1kZXZAc3V0ZS5qcCIsInVzZXJuYW1lIjoibHQtY2xpLWRldiIsInVzZXJfc2x1ZyI6Imx0LWNsaS1kZXYiLCJhdmF0YXIiOiJodHRwczovL3d3dy5ncmF2YXRhci5jb20vYXZhdGFyLzZiM2QyMmRkZGY5NjlhYjNjOTdlYTk1NDE1OTU1NjM2LnBuZz9zPTIwMCIsInJlZnJlc2hlZF9hdCI6MTYwMjgxMTkxNSwiaXAiOiI2NC40Ni4xMC4zOCIsImlkZW50aXR5IjoiZmZhZWE1YWZlZDUxZjFhMWIyY2RjOTk2YWVkNTQ1YmUiLCJzZXNzaW9uX2lkIjoyOTMwNjY1LCJfc2Vzc2lvbl9leHBpcnkiOjEyMDk2MDB9.MW-LzxO5l1HPsw3fKOtd2A1n3HJF47HywD3j0CU-0Kw',
8+
'csrftoken': 'Lhp4Arh6FxIXPGh59WOdGz1eOqa3AVAzoQDbzjWSGfBTlLCz8gk2SpnMVguR7b75',
9+
}
10+
11+
headers = {
12+
'X-CSRFToken': 'Lhp4Arh6FxIXPGh59WOdGz1eOqa3AVAzoQDbzjWSGfBTlLCz8gk2SpnMVguR7b75',
13+
'Origin': 'https://leetcode.com',
14+
'Referer': 'https://leetcode.com/',
15+
# 'Referer': 'https://leetcode.com/problems/two-sum/',
16+
# 'Referer': 'https://leetcode.com/problems/sellers-with-no-sales/',
17+
# 'Referer': 'https://leetcode.com/problems/split-two-strings-to-make-palindrome/',
18+
'Content-type': 'application/json',
19+
}
20+
21+
# query = '\n'.join([
22+
# 'query getQuestionDetail($titleSlug: String!) {',
23+
# ' question(titleSlug: $titleSlug) {',
24+
# 'codeSnippets {',
25+
# 'lang',
26+
# 'langSlug',
27+
# '}',
28+
# # 'title',
29+
# # 'questionFrontendId',
30+
# # 'sampleTestCase',
31+
# # 'metaData',
32+
# # 'isPaidOnly',
33+
# # ' content',
34+
# # ' stats',
35+
# # ' codeDefinition',
36+
# # ' sampleTestCase',
37+
# # ' enableRunCode',
38+
# # ' metaData',
39+
# # ' translatedContent',
40+
# ' }',
41+
# '}',
42+
# ])
43+
44+
# json_data = {
45+
# 'operationName': 'getQuestionDetail',
46+
# 'query': query,
47+
# 'variables': {
48+
# # 'titleSlug': 'two-sum',
49+
# # 'titleSlug': 'sellers-with-no-sales',
50+
# 'titleSlug': 'split-two-strings-to-make-palindrome',
51+
# },
52+
# }
53+
54+
json_data = {
55+
'operationName': 'globalData',
56+
'query': 'query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n discuss\n mockInterview\n contest\n store\n chinaProblemDiscuss\n socialProviders\n studentFooter\n enableChannels\n dangerZone\n cnJobs\n enableSharedWorker\n enableRecaptchaV3\n enableDebugger\n enableDebuggerPremium\n enableAutocomplete\n enableAutocompletePremium\n enableAllQuestionsRaw\n autocompleteLanguages\n enableIndiaPricing\n enableReferralDiscount\n maxTimeTravelTicketCount\n __typename\n }\n userStatus {\n isSignedIn\n isAdmin\n isStaff\n isSuperuser\n isTranslator\n isPremium\n isVerified\n checkedInToday\n username\n realName\n userSlug\n avatar\n optedIn\n requestRegion\n region\n activeSessionId\n permissions\n notificationStatus {\n lastModified\n numUnread\n __typename\n }\n completedFeatureGuides\n __typename\n }\n siteRegion\n chinaHost\n websocketUrl\n recaptchaKey\n recaptchaKeyV2\n sitewideAnnouncement\n userCountryCode\n}\n',
57+
}
58+
59+
{'data': {'feature': {'questionTranslation': False, 'subscription': True, 'signUp': True, 'discuss': True, 'mockInterview': True, 'contest': True, 'store': True, 'chinaProblemDiscuss': False, 'socialProviders': '[{"id": "linkedin_oauth2", "login_url": "/accounts/linkedin_oauth2/login/", "deprecated": true}, {"id": "google", "login_url": "/accounts/google/login/", "deprecated": false}, {"id": "github", "login_url": "/accounts/github/login/", "deprecated": false}, {"id": "facebook", "login_url": "/accounts/facebook/login/", "deprecated": false}]', 'studentFooter': True, 'enableChannels': True, 'dangerZone': True, 'cnJobs': False, 'enableSharedWorker': True, 'enableRecaptchaV3': True, 'enableDebugger': True, 'enableDebuggerPremium': True, 'enableAutocomplete': True, 'enableAutocompletePremium': True, 'enableAllQuestionsRaw': True, 'autocompleteLanguages': '["c", "cpp", "java", "python", "python3", "javascript"]', 'enableIndiaPricing': True, 'enableReferralDiscount': True, 'maxTimeTravelTicketCount': 3, '__typename': 'FeatureNode'}, 'userStatus': {'isSignedIn': True, 'isAdmin': False, 'isStaff': False, 'isSuperuser': False, 'isTranslator': False, 'isPremium': False, 'isVerified': True, 'checkedInToday': True, 'username': 'lt-cli-dev', 'realName': '', 'userSlug': 'lt-cli-dev', 'avatar': 'https://www.gravatar.com/avatar/6b3d22dddf969ab3c97ea95415955636.png?s=200', 'optedIn': True, 'requestRegion': 'US', 'region': '', 'activeSessionId': 3701518, 'permissions': [], 'notificationStatus': {'lastModified': 1602738260, 'numUnread': 1, '__typename': 'UserNotificationNode'}, 'completedFeatureGuides': ['QUESTION_DETAIL_TOUR'], '__typename': 'MeNode'}, 'siteRegion': 'US', 'chinaHost': 'https://leetcode-cn.com', 'websocketUrl': 'wss://sockets.leetcode.com/ws/', 'recaptchaKey': '6LdBpsIUAAAAAKAYWjZfIpn4cJHVIk_tsmxpl7cz', 'recaptchaKeyV2': '6LdBX8MUAAAAAAI4aZHi1C59OJizaJTvPNvWH2wz', 'sitewideAnnouncement': None, 'userCountryCode': 'CA'}}
60+
61+
62+
# json_data = {
63+
# 'lang': 'golang',
64+
# 'question_id': '1',
65+
# 'typed_code': 'func twoSum(nums []int, target int) []int {\n\tcache := map[int]int{}\n\tfor index, num := range nums {\n\t\tif pairIndex, ok := cache[target-num]; ok {\n\t\t\treturn []int{pairIndex, index}\n\t\t}\n\t\tcache[num] = index\n\t}\n\treturn nil\n}\n',
66+
# }
67+
68+
resp = s.post('https://leetcode.com/graphql', cookies=cookies, headers=headers, json=json_data)
69+
# resp = s.post('https://leetcode.com/problems/two-sum/submit/', cookies=cookies, headers=headers, json=json_data)
70+
print(resp.json())

0 commit comments

Comments
 (0)