-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcodeFile.py
More file actions
executable file
·205 lines (179 loc) · 6.53 KB
/
Copy pathlcodeFile.py
File metadata and controls
executable file
·205 lines (179 loc) · 6.53 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/local/bin/python3
import requests
from bs4 import BeautifulSoup
import browsercookie
import json
import ssl
import pprint
#import json2html
from json2html import *
import os
PPRINT = pprint.PrettyPrinter(indent=4)
DIFFICULTY_TYPES = ['Easy']
RESET = False
COOKIE_PATH = '/Users/01204086/Library/Application Support/Google/Chrome/Profile 1/Cookies'
WEBSITE_URL = 'https://leetcode.com'
URL_PROBLEM = 'https://leetcode.com/problems/'
API_URL = 'https://leetcode.com/api/problems/all/'
LCODE_JSON_PATH = 'show-lcode-app/src/lcode-react.json'
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 = []
qMap = {}
qArr = []
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("=====================================")
data = []
existedData = {}
if RESET:
print("RESET mode (wipe-out)")
else:
print("APPEND mode")
if not RESET:
with open(LCODE_JSON_PATH, "r") as fr:
data = fr.read().splitlines(True)
# data = data[1:len(data)-1]
dataArr = json.loads("".join(data))
for data in dataArr:
existedData[data["Number"]] = {
"Number": data["Number"],
"Level" : data["Level"],
"Title" : data["Title"],
"Url" : data["Url"],
"Tags" : data["Tags"],
"Memo" : data["Memo"]
}
for e in q:
if e['status'] == "ac":
qNumber = str(e['stat']['frontend_question_id']).zfill(4)
qTitle = e['stat']['question__title']
qSlug = e['stat']['question__title_slug']
qLevel = e['difficulty']['level']
#print(qNumber, qTitle, qLevel)
qUrl = URL_PROBLEM + qSlug
qLink = "<a href=\"" + qUrl + "\">" + qTitle + "</a>"
qMap[qNumber] = [qTitle, qUrl, qLevel]
appendData = None
if not RESET:
if qNumber in existedData: #skip
appendData = existedData[qNumber]
else:
appendData = {
"Number": qNumber,
"Level" : qLevel,
"Title" : qTitle,
"Url" : qUrl,
"Tags" : [],
"Memo" : ""
}
else:
appendData = {
"Number": qNumber,
"Level" : qLevel,
"Title" : qTitle,
"Url" : qUrl,
"Tags" : [],
"Memo" : ""
}
qArr.append(appendData)
print(appendData)
#Write file
with open(LCODE_JSON_PATH, "w") as f:
f.write("[" + "\n")
for idx, object in enumerate(qArr):
# Use json to write
json.dump(object, f, separators=(', ', ': '), indent = 4, ensure_ascii=False)
# Manually write
# print("{")
# for j, line in enumerate(object.items()):
# if j != len(object.items()) - 1:
# print("\"", line[0], "\"", ":", line[1], ",")
# else:
# print("\"", line[0], "\"", ":", line[1])
# print("}")
if idx != len(qArr) - 1:
f.write(",\n")
else:
f.write("\n")
#qJson = json.dumps(qMap)
#f.write(qJson)
f.write("]")
f.close()
def stringFormatter(s, num):
s = str(s)
return f'{s:>{num}}'
from datetime import datetime
import time
from os import listdir
from os import walk
def fetchFileDate():
mypath = 'algo'
# files = listdir(mypath)
# print(files)
fileList = []
fileMap = {}
for root, _, files in walk(mypath):
for file in files:
filePath = root + "/" + file
#print(filePath)
fileNum = file.split("__")
if len(fileNum) != 3:
continue
fileId = fileNum[1]
fileTime = time.localtime(int(os.path.getmtime(filePath)))
fileMonth = time.strftime("%Y-%m", fileTime)
fileDate = time.strftime("%Y-%m-%d", fileTime)
#print(ts)
fileList.append((fileDate, fileNum[1], fileNum[2], filePath))
fileMap[fileId] = fileDate
fileList.sort(key = lambda x: x[0])
# for row in fileList:
# print(row)
return fileMap
# for i in range(len(fileList)) :
# if i and fileList[i-1][0] != fileList[i][0]:
# print()
# print(fileList[i][1], fileList[i][2], fileList[i][3])
# run as a script (not module)
if __name__ == '__main__':
fileMap = fetchFileDate()
for id, date in sorted(fileMap.items()):
print(id, " last updated: ", date)
# filename = 'lcode.py'
# print(os.path.getsize(filename))
# print(os.path.getmtime(filename))
# print(os.path.getctime(filename))