diff --git a/README.md b/README.md index c63ee6a..caaa3b7 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,12 @@ Then in python shell initialize the client (if you're using chrome, cookies can import leetcode # Get the next two values from your browser cookies -csrf_token = "xxx" leetcode_session = "yyy" +csrf_token = "xxx" + +# Experimental: Or CSRF token can be obtained automatically +import leetcode.auth +csrf_token = leetcode.auth.get_csrf_cookie(leetcode_session) configuration = leetcode.Configuration() diff --git a/leetcode/auth.py b/leetcode/auth.py new file mode 100644 index 0000000..d9efd68 --- /dev/null +++ b/leetcode/auth.py @@ -0,0 +1,12 @@ +import requests + + +def get_csrf_cookie(session_id: str) -> str: + response = requests.get( + "https://leetcode.com/", + cookies={ + "LEETCODE_SESSION": session_id, + }, + ) + + return response.cookies["csrftoken"] diff --git a/requirements.txt b/requirements.txt index bafdc07..2b19583 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ six >= 1.10 python_dateutil >= 2.5.3 setuptools >= 21.0.0 urllib3 >= 1.15.1 +requests