-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcode.py
More file actions
executable file
·81 lines (69 loc) · 3.1 KB
/
Copy pathlcode.py
File metadata and controls
executable file
·81 lines (69 loc) · 3.1 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/local/bin/python3
import requests
from bs4 import BeautifulSoup
import browsercookie
import json
import ssl
import pprint
PPRINT = pprint.PrettyPrinter(indent=4)
DIFFICULTY_TYPES = ['Easy']
COOKIE_PATH = '/Users/01204086/Library/Application Support/Google/Chrome/Profile 1/Cookies'
WEBSITE_URL = 'https://leetcode.com'
API_URL = 'https://leetcode.com/api/problems/all/'
def getCookie(website_url, cookie_path):
myNeedDomainDict = {}
targetDomain = website_url.split('/')[-1]
for _ in browsercookie.chrome([cookie_path]):
if targetDomain in _.domain:
myNeedDomainDict[_.name] = _.value
return myNeedDomainDict
def getQuizCount():
with requests.Session() as s:
s.cookies.update(requests.utils.cookiejar_from_dict(getCookie(WEBSITE_URL, COOKIE_PATH)))
r = s.get(API_URL)
my_result = json.loads(r.text)
my_statistic_data = {key: my_result[key] for key in ['ac_easy', 'ac_medium', 'ac_hard', 'num_solved']}
return my_statistic_data
def showQuizListFromLeetcode():
with requests.Session() as s:
s.cookies.update(requests.utils.cookiejar_from_dict(getCookie(WEBSITE_URL, COOKIE_PATH)))
r = s.get(API_URL)
#print("### header:", "\n", r.headers)
my_result = json.loads(r.text)
#print('User Name:' , my_result['user_name'])
my_statistic_data = {key: my_result[key] for key in ['ac_easy', 'ac_medium', 'ac_hard', 'num_solved']}
count_easy = 0
count_medium = 0
count_hard = 0
q = []
for i in range(len(my_result['stat_status_pairs'])):
q.append(my_result['stat_status_pairs'][i])
if q[i]['difficulty']['level'] == 1:
count_easy += 1
if q[i]['difficulty']['level'] == 2:
count_medium += 1
if q[i]['difficulty']['level'] == 3:
count_hard += 1
q = sorted(q, key=lambda e: e['stat']['frontend_question_id'])
print()
print("=====================================")
print("============= Leetcode ==============")
print("=====================================")
for e in q:
if e['status'] == "ac":
print("", str(e['stat']['frontend_question_id']).zfill(4), e['stat']['question__title'])
score = 5 * my_result['ac_hard'] + 3 * my_result['ac_medium'] + 1 * my_result['ac_easy']
print("=====================================")
print('Solved / Total (Easy) :' , stringFormatter(my_result['ac_easy'] , 4), '/', stringFormatter(count_easy, 4))
print('Solved / Total (Medium):' , stringFormatter(my_result['ac_medium'] , 4), '/', stringFormatter(count_medium, 4))
print('Solved / Total (Hard) :' , stringFormatter(my_result['ac_hard'] , 4), '/', stringFormatter(count_hard,4))
print('Solved / Total (All) :' , stringFormatter(my_result['num_solved'], 4), '/', stringFormatter(my_result['num_total'],4))
print('Total Score :' , stringFormatter(score, 4))
print("=====================================")
print()
def stringFormatter(s, num):
s = str(s)
return f'{s:>{num}}'
# run as a script (not module)
if __name__ == '__main__':
showQuizListFromLeetcode()