Skip to content

Commit f84721c

Browse files
committed
finally write a get cookies script
1 parent 62a5e27 commit f84721c

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/target
1+
target/
2+
venv/
23
.direnv/
34
.envrc

flake.nix

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
let
1010
system = "x86_64-linux";
1111
pkgs = import nixpkgs { inherit system; };
12+
python = pkgs.python312;
13+
venv_dir = "./venv/";
1214
in
1315
{
1416
devShells.${system}.default = pkgs.mkShell {
@@ -17,12 +19,19 @@
1719
cargo
1820
clippy
1921
rust-analyzer
20-
(python312.withPackages (
21-
ps: with ps; [
22-
natsort
23-
]
24-
))
22+
python.pkgs.venvShellHook
2523
];
24+
venvDir = venv_dir;
25+
postShellHook = ''
26+
SENTINEL="${venv_dir}/.installed"
27+
REQUIREMENTS="requirements.txt"
28+
29+
if [ ! -f "$SENTINEL" ] || [ "$REQUIREMENTS" -nt "$SENTINEL" ]; then
30+
pip install --upgrade pip
31+
pip install -r "$REQUIREMENTS"
32+
touch "$SENTINEL"
33+
fi
34+
'';
2635
};
2736
};
2837
}

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
natsort==8.4.0
2+
browser_cookie3==0.20.1

scripts/get_cookies.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
import browser_cookie3 # type: ignore
4+
import os
5+
import re
6+
7+
8+
LEETCODE_URL = "https://leetcode.com"
9+
10+
CSRFTOKEN_KEY = "csrftoken"
11+
LEETCODE_SESSION_KEY = "LEETCODE_SESSION"
12+
13+
14+
CONFIG_LOCATION = "~/.leetcode/leetcode.toml"
15+
CONFIG_LOCATION = os.path.expanduser(CONFIG_LOCATION)
16+
17+
CSRF_CONFIG_REGEX = re.compile(r"^csrf = \".*\"$", re.MULTILINE)
18+
SESSION_CONFIG_REGEX = re.compile(r"^session = \".*\"$", re.MULTILINE)
19+
20+
21+
def main() -> None:
22+
cj = browser_cookie3.chrome(domain_name="leetcode.com")
23+
cookies = {c.name: c.value for c in cj}
24+
25+
csrftoken = cookies[CSRFTOKEN_KEY]
26+
leetcode_session = cookies[LEETCODE_SESSION_KEY]
27+
28+
with open(CONFIG_LOCATION, "r") as f:
29+
content = f.read()
30+
31+
content = CSRF_CONFIG_REGEX.sub(f'csrf = "{csrftoken}"', content)
32+
content = SESSION_CONFIG_REGEX.sub(f'session = "{leetcode_session}"', content)
33+
34+
with open(CONFIG_LOCATION, "w") as f:
35+
f.write(content)
36+
37+
38+
if __name__ == "__main__":
39+
main()

0 commit comments

Comments
 (0)