10
10
import os
11
11
import sys
12
12
13
- Account = "YOUR_ACCOUNT "
14
- Password = "YOUR_PASSWORD "
15
- directory = "THE_ABSOLUTE_PATH_THAT_YOU_WANT_TO_SAVE_FILES "
16
- driver = webdriver . Chrome ( "THE_ABSOLUTE_PATH_OF_CHROMEDRIVER_THAT_YOU_JUST_INSTALLED" )
13
+ username = ""
14
+ password = ""
15
+ outputDir = ""
16
+ driver = ""
17
17
18
18
suffix = {"cpp" : "cpp" , "cplusplus" : "cpp" , "c++" : "cpp" , "c" : "c" ,
19
19
"java" : "java" , "python" : "py" , "py" : "py" , "c#" : "cs" ,
@@ -86,10 +86,11 @@ def save_ac_code(ac_list, premium):
86
86
id = "0" + id
87
87
88
88
folderName = id + ". " + ac ["title" ].strip ()
89
- if not os .path .exists (directory + "\\ " + folderName ):
90
- os .makedirs (directory + "\\ " + folderName )
89
+ if not os .path .exists (outputDir + "\\ " + folderName ):
90
+ os .makedirs (outputDir + "\\ " + folderName )
91
91
92
- completeName = os .path .join (directory + "\\ " + folderName , "{}.{}" .format ("Solution" , suff ))
92
+ completeName = os .path .join (
93
+ outputDir + "\\ " + folderName , "{}.{}" .format ("Solution" , suff ))
93
94
sys .stdout .write (" " * 60 + "\r " )
94
95
if not os .path .exists (completeName ):
95
96
print (folderName + " saved." )
@@ -123,7 +124,6 @@ def get_ac_problem_list():
123
124
url = "https://leetcode.com/api/problems/algorithms/"
124
125
125
126
driver .get (url )
126
- pageSource = driver .page_source
127
127
jsonObj = json .loads (driver .find_element_by_tag_name ("body" ).text )
128
128
129
129
for ac in jsonObj ["stat_status_pairs" ]:
@@ -145,15 +145,16 @@ def get_ac_problem_list():
145
145
def login ():
146
146
login_url = "https://leetcode.com/accounts/login/"
147
147
148
- if Account and Password :
148
+ if username and password :
149
149
driver .get (login_url )
150
150
151
- username = driver .find_element_by_id ("id_login" )
152
- password = driver .find_element_by_id ("id_password" )
151
+ usernameField = driver .find_element_by_id ("id_login" )
152
+ passwordField = driver .find_element_by_id ("id_password" )
153
153
154
- username .send_keys (Account )
155
- password .send_keys (Password )
154
+ usernameField .send_keys (username )
155
+ passwordField .send_keys (password )
156
156
157
+ time .sleep (1 )
157
158
driver .find_element_by_id ("signin_btn" ).click ()
158
159
159
160
delay = 5 # seconds
@@ -176,6 +177,14 @@ def suffix_conversion(suff="cpp"):
176
177
return suffix [suff ]
177
178
178
179
if __name__ == "__main__" :
180
+ with open ('C:\Users\jj251\Desktop\LeetCode-AC-Code-Crawler\conf.json' , 'r' ) as f :
181
+ conf = json .loads (f .read ())
182
+ username = conf ["Username" ]
183
+ password = conf ["Password" ]
184
+ outputDir = conf ["OutputDir" ]
185
+ driverPath = conf ["ChromedriverPath" ]
186
+ driver = webdriver .Chrome (driverPath )
187
+
179
188
login ()
180
189
premium = premium_account_check ()
181
190
ac_list = get_ac_problem_list ()
@@ -185,4 +194,4 @@ def suffix_conversion(suff="cpp"):
185
194
print (str (len (ac_list )) + " AC solutions detected..." )
186
195
save_ac_code (ac_list , premium )
187
196
188
- driver .quit ()
197
+ driver .quit ()
0 commit comments