Skip to content

Commit afa4ae2

Browse files
committed
Remove option to log in with username/password
1 parent 1dd9a0e commit afa4ae2

File tree

3 files changed

+16
-51
lines changed

3 files changed

+16
-51
lines changed

README.md

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,11 @@ optional arguments:
9898
9999
## Login
100100
101-
To download your submissions you need to log in your LeetCode account. There are two ways to log in, by inserting
102-
username/password or by cookies.
101+
To download your submissions you need to log in your LeetCode account by providing the cookies.
103102
104-
You can either use the interactive menu to supply the required information or you can pass them as program arguments
105-
when lunching the script.
106-
107-
The former option is to be preferred as it will avoid storing your password or your cookies in the command history.
108-
109-
### Username/Password
110-
111-
To log in using username and password, insert them using the interactive menu (preferred) or pass them as arguments when
112-
lunching the script, like in the following example:
113-
114-
```bash
115-
python leetcode-export --username {USERNAME} --password {PASSWORD}
116-
```
117-
118-
### Cookies
119-
120-
To log in using cookies, you first need to get them from a session where you are already logged in. Login in your
121-
browser, open the browser's Dev Tool, click on the Network tab and copy the cookie header that is sent when you visit
122-
any leetcode webpage.
103+
To log in using cookies, you need to get them from a session where you are already logged in. Login in your LeetCode
104+
account in your browser, open the browser's Dev Tool, click on the Network tab and copy the cookie header that is sent
105+
when you visit any leetcode webpage.
123106
124107
You can insert the cookie string that you have just copied in the interactive menu (recommended) or you can pass it as a
125108
program argument when lunching the script, like in the following example:
@@ -128,6 +111,8 @@ program argument when lunching the script, like in the following example:
128111
python leetcode-export --cookies {COOKIES}
129112
```
130113

114+
Using the interactive menu is to be preferred as it will avoid storing your cookies in the command history.
115+
131116
## Filename template arguments
132117

133118
### Problem description filename template
@@ -150,7 +135,7 @@ title: str
150135
title_slug: str
151136
```
152137

153-
Default problem description filename template: `${questionId} - ${titleSlug}.txt`
138+
Default problem description filename template: `${question_id} - ${title_slug}.txt`
154139

155140
### Submission filename template
156141

leetcode_export/__main__.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
22
import argparse
3-
import getpass
43
import logging
54
import os
65
from string import Template
@@ -18,8 +17,6 @@
1817

1918
def parse_args():
2019
parser = argparse.ArgumentParser(description='Export LeetCode solutions')
21-
parser.add_argument('--username', type=str, help='Set LeetCode username')
22-
parser.add_argument('--password', type=str, help='Set LeetCode password')
2320
parser.add_argument('--cookies', type=str, help='Set LeetCode cookies')
2421
parser.add_argument('--folder', type=str, default='.', help='Output folder')
2522
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Enable verbose logging details')
@@ -58,35 +55,16 @@ def main():
5855
submission_template = Template(args.submission_filename)
5956

6057
leetcode = LeetCode()
61-
username = ''
62-
password = ''
63-
cookies = ''
64-
65-
# Login into leetcode
66-
if (not args.username or not args.password) and not args.cookies:
67-
choice = input("How do you want to login?\n 1 - Username and Password\n 2 - Cookies\n")
68-
while choice != '1' and choice != '2':
69-
print("Choice not valid, input 1 or 2")
70-
choice = input("How do you want to login?\n 1 - Username and Password\n 2 - Cookies\n")
71-
72-
if choice == '1':
73-
username = input("Insert LeetCode username: ")
74-
password = getpass.getpass(prompt="Insert LeetCode password: ")
75-
else:
76-
cookies = input("Insert LeetCode cookies: ")
77-
else:
78-
username = args.username
79-
password = args.password
80-
cookies = args.cookies
58+
cookies = args.cookies
59+
60+
if not cookies:
61+
cookies = input("Insert LeetCode cookies: ")
8162

82-
if username and password and not leetcode.log_in(args.username, args.password):
63+
if not leetcode.set_cookies(cookies):
8364
print(
84-
"Login not successful! You might have entered the wrong username/password or you need to complete the reCAPTCHA. If you need to complete the reCAPTCHA, log in with the cookies instead. Check the log for more information.")
65+
"Cookies not valid. Copy them from the Network tab of your browser by clicking on any leetcode.com request and going in Request Headers > cookie.")
8566
exit(1)
8667

87-
if cookies:
88-
leetcode.set_cookies(cookies)
89-
9068
# Create output folder if it doesn't already exist
9169
if not os.path.exists(args.folder):
9270
os.mkdir(args.folder)

leetcode_export/leetcode.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ def log_in(self, username: str, password: str) -> bool:
3636
logging.warning(response_post.json())
3737
return False
3838

39-
def set_cookies(self, cookies: str):
39+
def set_cookies(self, cookies: str) -> bool:
4040
cookies_list = cookies.split(';')
4141
cookies_list = map(lambda el: el.split('='), cookies_list)
4242
for cookies in cookies_list:
4343
self.session.cookies.set(cookies[0].strip(), cookies[1].strip())
4444
if self.is_user_logged():
4545
logging.info("Cookies set successful")
46+
return True
4647
else:
4748
logging.warning("Cookies set failed")
49+
return False
4850

4951
def is_user_logged(self) -> bool:
5052
if self.user_logged and datetime.datetime.now() < self.user_logged_expiration:

0 commit comments

Comments
 (0)