Skip to content

Commit 828a46f

Browse files
committed
Use conf.json to store user info
1 parent d7cc03c commit 828a46f

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

LeetCode_AC_Code_Crawler.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import os
1111
import sys
1212

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 = ""
1717

1818
suffix = {"cpp": "cpp", "cplusplus": "cpp", "c++": "cpp", "c": "c",
1919
"java": "java", "python": "py", "py": "py", "c#": "cs",
@@ -86,10 +86,11 @@ def save_ac_code(ac_list, premium):
8686
id = "0" + id
8787

8888
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)
9191

92-
completeName = os.path.join(directory + "\\" + folderName, "{}.{}".format("Solution", suff))
92+
completeName = os.path.join(
93+
outputDir + "\\" + folderName, "{}.{}".format("Solution", suff))
9394
sys.stdout.write(" "*60 + "\r")
9495
if not os.path.exists(completeName):
9596
print(folderName + " saved.")
@@ -123,7 +124,6 @@ def get_ac_problem_list():
123124
url = "https://leetcode.com/api/problems/algorithms/"
124125

125126
driver.get(url)
126-
pageSource = driver.page_source
127127
jsonObj = json.loads(driver.find_element_by_tag_name("body").text)
128128

129129
for ac in jsonObj["stat_status_pairs"]:
@@ -145,15 +145,16 @@ def get_ac_problem_list():
145145
def login():
146146
login_url = "https://leetcode.com/accounts/login/"
147147

148-
if Account and Password:
148+
if username and password:
149149
driver.get(login_url)
150150

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")
153153

154-
username.send_keys(Account)
155-
password.send_keys(Password)
154+
usernameField.send_keys(username)
155+
passwordField.send_keys(password)
156156

157+
time.sleep(1)
157158
driver.find_element_by_id("signin_btn").click()
158159

159160
delay = 5 # seconds
@@ -176,6 +177,14 @@ def suffix_conversion(suff="cpp"):
176177
return suffix[suff]
177178

178179
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+
179188
login()
180189
premium = premium_account_check()
181190
ac_list = get_ac_problem_list()
@@ -185,4 +194,4 @@ def suffix_conversion(suff="cpp"):
185194
print(str(len(ac_list)) + " AC solutions detected...")
186195
save_ac_code(ac_list, premium)
187196

188-
driver.quit()
197+
driver.quit()

conf.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Username": "YOUR_USERNAME",
3+
"Password": "YOUR_PASSWORD",
4+
"OutputDir": "THE_ABSOLUTE_PATH_THAT_YOU_WANT_TO_SAVE_FILES",
5+
"ChromedriverPath": "THE_ABSOLUTE_PATH_OF_CHROMEDRIVER"
6+
}

0 commit comments

Comments
 (0)