-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc.py
More file actions
executable file
·51 lines (41 loc) · 1.48 KB
/
lc.py
File metadata and controls
executable file
·51 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/local/bin/python3
import leetcode
import sys
import json
DEFAULT_QUERY = "playground"
DEFAULT_VARIABLES = r'{"titleSlug":"sort-vowels-in-a-string"}'
DEFAULT_OPERATION_NAME = "getQuestionDetail"
def get_csrf_token(fname = "token"):
with open(fname, "r") as t:
return t.read().rstrip('\n')
def get_session(fname = "session"):
with open(fname, "r") as s:
return s.read().rstrip('\n')
configuration = leetcode.Configuration()
configuration.api_key['x-csrftoken'] = get_csrf_token()
configuration.api_key['csrftoken'] = configuration.api_key['x-csrftoken']
configuration.api_key['LEETCODE_SESSION'] = get_session()
configuration.api_key['Referer'] = "https://leetcode.com/"
configuration.debug = False
def query_argument():
if len(sys.argv) == 1:
return DEFAULT_QUERY
else:
return sys.argv[1]
def variables_argument():
if len(sys.argv) < 3:
return DEFAULT_VARIABLES
elif len(sys.argv) < 4:
return f'{{"titleSlug":"{sys.argv[2]}"}}'
def get_query(query_filename = DEFAULT_QUERY):
with open(f"queries/{query_filename}.graphql", "r") as f:
return f.read()
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
request = leetcode.GraphqlQuery(
query=get_query(query_argument()),
variables=variables_argument(),
)
response_obj = api_instance.graphql_post(body=request)
response_json_str = json.dumps(response_obj.to_dict())
print(response_json_str)
# print(response_obj.to_str())