Skip to content

Commit 8cb81b5

Browse files
committed
Fix expired session detection
The old code failed to detect the expiration of the session cookie, causing problems in testing and submission. Now we detect if the session cookie is usable by trying getting the progress of the user.
1 parent 82a2173 commit 8cb81b5

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

autoload/leetcode.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
LC_SUBMISSION = LC_BASE + '/submissions/detail/{submission}/'
4242
LC_CHECK = LC_BASE + '/submissions/detail/{submission}/check/'
4343
LC_PROBLEM_SET_ALL = LC_BASE + '/problemset/all/'
44+
LC_PROGRESS_ALL = LC_BASE + '/api/progress/all/'
4445

4546
EMPTY_FREQUENCIES = [0, 0, 0, 0, 0, 0, 0, 0]
4647

@@ -69,7 +70,6 @@ def _make_headers():
6970
assert is_login()
7071
headers = {'Origin': LC_BASE,
7172
'Referer': LC_BASE,
72-
'X-CSRFToken': session.cookies['csrftoken'],
7373
'X-Requested-With': 'XMLHttpRequest'}
7474
return headers
7575

@@ -143,6 +143,19 @@ def is_login():
143143
return session and 'LEETCODE_SESSION' in session.cookies
144144

145145

146+
def get_progress():
147+
headers = _make_headers()
148+
res = session.get(LC_PROGRESS_ALL, headers=headers)
149+
if res.status_code != 200:
150+
_echoerr('cannot get the progress')
151+
return None
152+
153+
data = res.json()
154+
if 'solvedTotal' not in data:
155+
return None
156+
return data
157+
158+
146159
def load_session_cookie(browser):
147160
if browser_cookie3 is None:
148161
_echoerr('browser_cookie3 not installed: pip3 install browser_cookie3 --user')
@@ -170,9 +183,9 @@ def load_session_cookie(browser):
170183
session = requests.Session()
171184
session.cookies.set_cookie(session_cookie)
172185

173-
res = session.get(LC_BASE)
174-
if res.status_code != 200:
175-
_echoerr('cannot open ' + LC_BASE + '. Session might have expired.')
186+
progress = get_progress()
187+
if progress is None:
188+
_echoerr('cannot get progress. Please relogin in your browser.')
176189
keyring.delete_password('leetcode.vim', 'SESSION_COOKIE')
177190
return False
178191

@@ -182,7 +195,11 @@ def load_session_cookie(browser):
182195
def _get_category_problems(category):
183196
headers = _make_headers()
184197
url = LC_CATEGORY_PROBLEMS.format(category=category)
198+
log.info('_get_category_problems request: url="%s" headers="%s"',
199+
url, headers)
185200
res = session.get(url, headers=headers)
201+
log.info('_get_category_problems response: status="%s" body="%s"',
202+
res.status_code, res.text)
186203
if res.status_code != 200:
187204
_echoerr('cannot get the category: {}'.format(category))
188205
return []

0 commit comments

Comments
 (0)