leetcode-local-cli 0.9.0__tar.gz → 0.10.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- leetcode_local_cli-0.10.1/PKG-INFO +151 -0
- leetcode_local_cli-0.10.1/README.md +127 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/pyproject.toml +1 -1
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/cli.py +52 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/client.py +80 -6
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/__init__.py +22 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/account.py +101 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/common.py +26 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/problems.py +54 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/setup.py +65 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/submission.py +72 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/testing.py +233 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/submission.py +61 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/ui.py +39 -13
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/__init__.py +1 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/account.py +32 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/common.py +86 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/diagnostics.py +54 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/local_test.py +36 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/login.py +241 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/problems.py +133 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/setup.py +34 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/submission.py +237 -0
- leetcode_local_cli-0.9.0/PKG-INFO +0 -168
- leetcode_local_cli-0.9.0/README.md +0 -144
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/cli.py +0 -718
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/service.py +0 -284
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/LICENSE +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/__init__.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/__main__.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/_test_runner.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/auth.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/browser.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/config.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/doctor.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/local_testing.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/paths.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/problem.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/safe_files.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/solution_source.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/version.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/workspace.py +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: leetcode-local-cli
|
|
3
|
+
Version: 0.10.1
|
|
4
|
+
Summary: Local coding workflow CLI for LeetCode CN.
|
|
5
|
+
Keywords: cli,leetcode,leetcode-cn,uv
|
|
6
|
+
Author: Aetherialter
|
|
7
|
+
Author-email: Aetherialter <Aetherialter@outlook.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Dist: httpx>=0.28.1
|
|
16
|
+
Requires-Dist: rich>=15.0.0
|
|
17
|
+
Requires-Dist: typer>=0.25.1
|
|
18
|
+
Requires-Dist: websockets>=15.0
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Project-URL: Homepage, https://github.com/Aetherialter/leetcode-local-cli
|
|
21
|
+
Project-URL: Issues, https://github.com/Aetherialter/leetcode-local-cli/issues
|
|
22
|
+
Project-URL: Repository, https://github.com/Aetherialter/leetcode-local-cli
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# leetcode-local-cli
|
|
26
|
+
|
|
27
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
28
|
+
|
|
29
|
+
当前已发布版本:`v0.10.1`。
|
|
30
|
+
|
|
31
|
+
## 安装
|
|
32
|
+
|
|
33
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
34
|
+
|
|
35
|
+
Linux / macOS:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.sh | sh
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Windows PowerShell:
|
|
42
|
+
|
|
43
|
+
```powershell
|
|
44
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.ps1 | iex"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
也可以直接安装并初始化:
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
uv tool install leetcode-local-cli
|
|
51
|
+
lc init
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
自动化验收可以显式指定完整工作区路径:
|
|
55
|
+
|
|
56
|
+
```shell
|
|
57
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
升级或卸载:
|
|
61
|
+
|
|
62
|
+
```shell
|
|
63
|
+
uv tool upgrade leetcode-local-cli
|
|
64
|
+
uv tool uninstall leetcode-local-cli
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 快速开始
|
|
68
|
+
|
|
69
|
+
```shell
|
|
70
|
+
lc init
|
|
71
|
+
lc login
|
|
72
|
+
lc doctor
|
|
73
|
+
lc get 1
|
|
74
|
+
lc solve 1
|
|
75
|
+
lc test
|
|
76
|
+
lc submit
|
|
77
|
+
lc check <Submission ID>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
81
|
+
|
|
82
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
83
|
+
|
|
84
|
+
## 命令
|
|
85
|
+
|
|
86
|
+
| 命令 | 作用 |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
89
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
90
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
91
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
92
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
93
|
+
| `lc solve <题号>` | 生成并打开 `solution.py` |
|
|
94
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
95
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
96
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
97
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
98
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
99
|
+
|
|
100
|
+
## 浏览器登录
|
|
101
|
+
|
|
102
|
+
执行 `lc login` 后:
|
|
103
|
+
|
|
104
|
+
1. CLI 优先尝试日常 Chrome,再尝试日常 Edge。
|
|
105
|
+
2. 浏览器会打开 Remote debugging 页面与 LeetCode。
|
|
106
|
+
3. 勾选 **Allow remote debugging for this browser instance**;出现确认框时选择 **Allow**。
|
|
107
|
+
4. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
108
|
+
5. 自动登录失败时,回退到隐藏输入的手动 Cookie。
|
|
109
|
+
|
|
110
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
|
|
111
|
+
|
|
112
|
+
## 本地调用
|
|
113
|
+
|
|
114
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
115
|
+
|
|
116
|
+
```text
|
|
117
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。链表和二叉树暂不自动转换。
|
|
121
|
+
|
|
122
|
+
非交互示例:
|
|
123
|
+
|
|
124
|
+
```shell
|
|
125
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
129
|
+
|
|
130
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
|
|
131
|
+
|
|
132
|
+
## 安全与限制
|
|
133
|
+
|
|
134
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
135
|
+
- Session 保存在工作区 `.leetcode_local_cli/session.json`,包含真实 Cookie,不得提交、上传、同步或共享。
|
|
136
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
137
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
138
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
139
|
+
|
|
140
|
+
## 维护者文档
|
|
141
|
+
|
|
142
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
143
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
144
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
145
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
146
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
147
|
+
- [发布流程](docs/RELEASING.md)
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# leetcode-local-cli
|
|
2
|
+
|
|
3
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
4
|
+
|
|
5
|
+
当前已发布版本:`v0.10.1`。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
10
|
+
|
|
11
|
+
Linux / macOS:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.sh | sh
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Windows PowerShell:
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.ps1 | iex"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
也可以直接安装并初始化:
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
uv tool install leetcode-local-cli
|
|
27
|
+
lc init
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
自动化验收可以显式指定完整工作区路径:
|
|
31
|
+
|
|
32
|
+
```shell
|
|
33
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
升级或卸载:
|
|
37
|
+
|
|
38
|
+
```shell
|
|
39
|
+
uv tool upgrade leetcode-local-cli
|
|
40
|
+
uv tool uninstall leetcode-local-cli
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 快速开始
|
|
44
|
+
|
|
45
|
+
```shell
|
|
46
|
+
lc init
|
|
47
|
+
lc login
|
|
48
|
+
lc doctor
|
|
49
|
+
lc get 1
|
|
50
|
+
lc solve 1
|
|
51
|
+
lc test
|
|
52
|
+
lc submit
|
|
53
|
+
lc check <Submission ID>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
57
|
+
|
|
58
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
59
|
+
|
|
60
|
+
## 命令
|
|
61
|
+
|
|
62
|
+
| 命令 | 作用 |
|
|
63
|
+
| --- | --- |
|
|
64
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
65
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
66
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
67
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
68
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
69
|
+
| `lc solve <题号>` | 生成并打开 `solution.py` |
|
|
70
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
71
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
72
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
73
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
74
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
75
|
+
|
|
76
|
+
## 浏览器登录
|
|
77
|
+
|
|
78
|
+
执行 `lc login` 后:
|
|
79
|
+
|
|
80
|
+
1. CLI 优先尝试日常 Chrome,再尝试日常 Edge。
|
|
81
|
+
2. 浏览器会打开 Remote debugging 页面与 LeetCode。
|
|
82
|
+
3. 勾选 **Allow remote debugging for this browser instance**;出现确认框时选择 **Allow**。
|
|
83
|
+
4. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
84
|
+
5. 自动登录失败时,回退到隐藏输入的手动 Cookie。
|
|
85
|
+
|
|
86
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
|
|
87
|
+
|
|
88
|
+
## 本地调用
|
|
89
|
+
|
|
90
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。链表和二叉树暂不自动转换。
|
|
97
|
+
|
|
98
|
+
非交互示例:
|
|
99
|
+
|
|
100
|
+
```shell
|
|
101
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
105
|
+
|
|
106
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
|
|
107
|
+
|
|
108
|
+
## 安全与限制
|
|
109
|
+
|
|
110
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
111
|
+
- Session 保存在工作区 `.leetcode_local_cli/session.json`,包含真实 Cookie,不得提交、上传、同步或共享。
|
|
112
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
113
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
114
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
115
|
+
|
|
116
|
+
## 维护者文档
|
|
117
|
+
|
|
118
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
119
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
120
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
121
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
122
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
123
|
+
- [发布流程](docs/RELEASING.md)
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
from typer import Exit, Option, Typer, echo
|
|
5
|
+
|
|
6
|
+
from leetcode_local_cli.commands import register_commands
|
|
7
|
+
from leetcode_local_cli.version import PACKAGE_NAME, get_version
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
app = Typer(help="力扣中文站本地化刷题 CLI 工具", no_args_is_help=True)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _configure_utf8_output() -> None:
|
|
14
|
+
"""Keep localized CLI output writable when Windows redirects the streams."""
|
|
15
|
+
for stream in (sys.stdout, sys.stderr):
|
|
16
|
+
reconfigure = getattr(stream, "reconfigure", None)
|
|
17
|
+
if callable(reconfigure):
|
|
18
|
+
reconfigure(encoding="utf-8", errors="backslashreplace")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def run() -> None:
|
|
22
|
+
"""Run the command-line application with deterministic UTF-8 output."""
|
|
23
|
+
_configure_utf8_output()
|
|
24
|
+
app()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _version_callback(value: bool) -> None:
|
|
28
|
+
if value:
|
|
29
|
+
echo(f"{PACKAGE_NAME} {get_version()}")
|
|
30
|
+
raise Exit()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@app.callback()
|
|
34
|
+
def main(
|
|
35
|
+
version: Annotated[
|
|
36
|
+
bool,
|
|
37
|
+
Option(
|
|
38
|
+
"--version",
|
|
39
|
+
callback=_version_callback,
|
|
40
|
+
is_eager=True,
|
|
41
|
+
help="显示版本并退出",
|
|
42
|
+
),
|
|
43
|
+
] = False,
|
|
44
|
+
) -> None:
|
|
45
|
+
"""力扣中文站本地化刷题 CLI 工具。"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
register_commands(app)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
run()
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
from email.utils import parsedate_to_datetime
|
|
2
4
|
from enum import Enum
|
|
5
|
+
import math
|
|
3
6
|
from typing import Any
|
|
4
7
|
|
|
5
8
|
import httpx
|
|
@@ -86,6 +89,7 @@ query questionData($titleSlug: String!) {
|
|
|
86
89
|
|
|
87
90
|
|
|
88
91
|
class ClientErrorKind(str, Enum):
|
|
92
|
+
TIMEOUT = "timeout"
|
|
89
93
|
NETWORK = "network"
|
|
90
94
|
HTTP = "http"
|
|
91
95
|
INVALID_JSON = "invalid_json"
|
|
@@ -99,12 +103,32 @@ class ClientResult:
|
|
|
99
103
|
data: Any = None
|
|
100
104
|
error: ClientErrorKind | None = None
|
|
101
105
|
message: str = ""
|
|
106
|
+
status_code: int | None = None
|
|
107
|
+
retry_after_seconds: float | None = None
|
|
102
108
|
|
|
103
109
|
@property
|
|
104
110
|
def ok(self) -> bool:
|
|
105
111
|
return self.error is None
|
|
106
112
|
|
|
107
113
|
|
|
114
|
+
def _parse_retry_after(value: str | None) -> float | None:
|
|
115
|
+
if not value:
|
|
116
|
+
return None
|
|
117
|
+
try:
|
|
118
|
+
seconds = float(value)
|
|
119
|
+
except ValueError:
|
|
120
|
+
try:
|
|
121
|
+
retry_at = parsedate_to_datetime(value)
|
|
122
|
+
except (TypeError, ValueError, OverflowError):
|
|
123
|
+
return None
|
|
124
|
+
if retry_at.tzinfo is None:
|
|
125
|
+
retry_at = retry_at.replace(tzinfo=timezone.utc)
|
|
126
|
+
return max(0.0, (retry_at - datetime.now(timezone.utc)).total_seconds())
|
|
127
|
+
if not math.isfinite(seconds) or seconds < 0:
|
|
128
|
+
return None
|
|
129
|
+
return seconds
|
|
130
|
+
|
|
131
|
+
|
|
108
132
|
class LeetCodeClient:
|
|
109
133
|
def __init__(self, cookies: dict[str, str] | None = None):
|
|
110
134
|
self.client = httpx.Client(
|
|
@@ -137,10 +161,18 @@ class LeetCodeClient:
|
|
|
137
161
|
**kwargs,
|
|
138
162
|
)
|
|
139
163
|
response.raise_for_status()
|
|
164
|
+
except httpx.TimeoutException:
|
|
165
|
+
return ClientResult(error=ClientErrorKind.TIMEOUT)
|
|
140
166
|
except httpx.RequestError:
|
|
141
167
|
return ClientResult(error=ClientErrorKind.NETWORK)
|
|
142
|
-
except httpx.HTTPStatusError:
|
|
143
|
-
return ClientResult(
|
|
168
|
+
except httpx.HTTPStatusError as exc:
|
|
169
|
+
return ClientResult(
|
|
170
|
+
error=ClientErrorKind.HTTP,
|
|
171
|
+
status_code=exc.response.status_code,
|
|
172
|
+
retry_after_seconds=_parse_retry_after(
|
|
173
|
+
exc.response.headers.get("Retry-After")
|
|
174
|
+
),
|
|
175
|
+
)
|
|
144
176
|
|
|
145
177
|
try:
|
|
146
178
|
result = response.json()
|
|
@@ -354,20 +386,62 @@ class LeetCodeClient:
|
|
|
354
386
|
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
355
387
|
return ClientResult(data=submission_id)
|
|
356
388
|
|
|
357
|
-
def get_submission_result(
|
|
389
|
+
def get_submission_result(
|
|
390
|
+
self,
|
|
391
|
+
submission_id: int,
|
|
392
|
+
*,
|
|
393
|
+
timeout: float = 10,
|
|
394
|
+
) -> ClientResult:
|
|
395
|
+
from leetcode_local_cli.submission import SubmissionCheck
|
|
396
|
+
|
|
358
397
|
result = self._request_json(
|
|
359
398
|
"GET",
|
|
360
399
|
f"/submissions/detail/{submission_id}/check/",
|
|
361
|
-
timeout=
|
|
400
|
+
timeout=timeout,
|
|
362
401
|
)
|
|
363
402
|
if not result.ok:
|
|
364
403
|
return result
|
|
365
404
|
payload = result.data
|
|
366
405
|
if not isinstance(payload, dict):
|
|
367
406
|
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
368
|
-
|
|
407
|
+
state = payload.get("state")
|
|
408
|
+
if not isinstance(state, str) or not state:
|
|
369
409
|
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
370
|
-
|
|
410
|
+
status_message = payload.get("status_msg")
|
|
411
|
+
if status_message is not None and (
|
|
412
|
+
not isinstance(status_message, str) or not status_message
|
|
413
|
+
):
|
|
414
|
+
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
415
|
+
runtime = payload.get("status_runtime")
|
|
416
|
+
if runtime is None:
|
|
417
|
+
runtime = payload.get("runtime")
|
|
418
|
+
if runtime is not None and not isinstance(runtime, str):
|
|
419
|
+
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
420
|
+
memory = payload.get("status_memory")
|
|
421
|
+
if memory is None:
|
|
422
|
+
memory = payload.get("memory")
|
|
423
|
+
if memory is not None and not isinstance(memory, str):
|
|
424
|
+
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
425
|
+
total_correct = payload.get("total_correct")
|
|
426
|
+
if total_correct is not None and (
|
|
427
|
+
not isinstance(total_correct, int) or isinstance(total_correct, bool)
|
|
428
|
+
):
|
|
429
|
+
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
430
|
+
total_testcases = payload.get("total_testcases")
|
|
431
|
+
if total_testcases is not None and (
|
|
432
|
+
not isinstance(total_testcases, int) or isinstance(total_testcases, bool)
|
|
433
|
+
):
|
|
434
|
+
return ClientResult(error=ClientErrorKind.INVALID_RESPONSE)
|
|
435
|
+
return ClientResult(
|
|
436
|
+
data=SubmissionCheck(
|
|
437
|
+
state=state,
|
|
438
|
+
status_message=status_message,
|
|
439
|
+
runtime=runtime,
|
|
440
|
+
memory=memory,
|
|
441
|
+
total_correct=total_correct,
|
|
442
|
+
total_testcases=total_testcases,
|
|
443
|
+
)
|
|
444
|
+
)
|
|
371
445
|
|
|
372
446
|
def close(self) -> None:
|
|
373
447
|
self.client.close()
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typer import Typer
|
|
2
|
+
|
|
3
|
+
from leetcode_local_cli.commands.account import login, profile, status
|
|
4
|
+
from leetcode_local_cli.commands.problems import get_problem, show, solve
|
|
5
|
+
from leetcode_local_cli.commands.setup import init_workspace
|
|
6
|
+
from leetcode_local_cli.commands.submission import check, submit
|
|
7
|
+
from leetcode_local_cli.commands.testing import doctor, test
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def register_commands(app: Typer) -> None:
|
|
11
|
+
"""Register every public CLI command without coupling handlers to the app."""
|
|
12
|
+
app.command("init")(init_workspace)
|
|
13
|
+
app.command()(login)
|
|
14
|
+
app.command()(status)
|
|
15
|
+
app.command()(profile)
|
|
16
|
+
app.command("get")(get_problem)
|
|
17
|
+
app.command()(show)
|
|
18
|
+
app.command()(solve)
|
|
19
|
+
app.command()(test)
|
|
20
|
+
app.command()(doctor)
|
|
21
|
+
app.command()(submit)
|
|
22
|
+
app.command()(check)
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
from typer import BadParameter, Exit, Option
|
|
5
|
+
|
|
6
|
+
from leetcode_local_cli.auth import get_cookies_from_input
|
|
7
|
+
from leetcode_local_cli.browser import BrowserKind
|
|
8
|
+
from leetcode_local_cli.commands import common
|
|
9
|
+
from leetcode_local_cli.ui import (
|
|
10
|
+
info,
|
|
11
|
+
loading,
|
|
12
|
+
render_profile,
|
|
13
|
+
success,
|
|
14
|
+
warning,
|
|
15
|
+
)
|
|
16
|
+
from leetcode_local_cli.use_cases.account import get_account_profile, get_user_status
|
|
17
|
+
from leetcode_local_cli.use_cases.common import UseCaseError
|
|
18
|
+
from leetcode_local_cli.use_cases.login import (
|
|
19
|
+
LoginReporter,
|
|
20
|
+
try_automatic_login,
|
|
21
|
+
validate_and_save_login,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
LOGIN_REPORTER = LoginReporter(info=info, warning=warning, loading=loading)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def login(
|
|
29
|
+
browser: Annotated[
|
|
30
|
+
BrowserKind,
|
|
31
|
+
Option(
|
|
32
|
+
"--browser",
|
|
33
|
+
help="登录浏览器:auto 依次尝试 Chrome、Edge;也可明确指定",
|
|
34
|
+
),
|
|
35
|
+
] = BrowserKind.AUTO,
|
|
36
|
+
devtools_port: Annotated[
|
|
37
|
+
int | None,
|
|
38
|
+
Option(
|
|
39
|
+
"--devtools-port",
|
|
40
|
+
min=1,
|
|
41
|
+
max=65535,
|
|
42
|
+
help="高级用法:连接已开启的本机浏览器 DevTools 端口",
|
|
43
|
+
),
|
|
44
|
+
] = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
paths = common.require_app_paths()
|
|
47
|
+
if devtools_port is not None and browser is BrowserKind.AUTO:
|
|
48
|
+
raise BadParameter(
|
|
49
|
+
"--devtools-port 必须与 --browser chrome 或 --browser edge 一起使用",
|
|
50
|
+
param_hint="--devtools-port",
|
|
51
|
+
)
|
|
52
|
+
try:
|
|
53
|
+
if try_automatic_login(
|
|
54
|
+
browser,
|
|
55
|
+
paths.session_file,
|
|
56
|
+
reporter=LOGIN_REPORTER,
|
|
57
|
+
devtools_port=devtools_port,
|
|
58
|
+
):
|
|
59
|
+
success("成功登录")
|
|
60
|
+
return
|
|
61
|
+
except UseCaseError as exc:
|
|
62
|
+
common.exit_for_use_case_error(exc)
|
|
63
|
+
_login_manually(paths.session_file)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _login_manually(session_file: Path) -> None:
|
|
67
|
+
warning("无法自动获取 LeetCode 登录状态,请手动粘贴 Cookie")
|
|
68
|
+
manual_cookies = get_cookies_from_input()
|
|
69
|
+
if not manual_cookies:
|
|
70
|
+
warning("未获取有效 Cookie")
|
|
71
|
+
raise Exit(1)
|
|
72
|
+
try:
|
|
73
|
+
valid = validate_and_save_login(
|
|
74
|
+
manual_cookies,
|
|
75
|
+
source="manual",
|
|
76
|
+
session_file=session_file,
|
|
77
|
+
)
|
|
78
|
+
except UseCaseError as exc:
|
|
79
|
+
common.exit_for_use_case_error(exc)
|
|
80
|
+
if not valid:
|
|
81
|
+
warning("Cookie 无效或已过期")
|
|
82
|
+
raise Exit(1)
|
|
83
|
+
success("成功登录")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def status() -> None:
|
|
87
|
+
try:
|
|
88
|
+
user_status = get_user_status(common.require_app_paths())
|
|
89
|
+
except UseCaseError as exc:
|
|
90
|
+
common.exit_for_use_case_error(exc)
|
|
91
|
+
username = user_status.get("username", "未知用户")
|
|
92
|
+
success(f"在线状态: 当前账号 {username}")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def profile() -> None:
|
|
96
|
+
try:
|
|
97
|
+
with loading("正在获取账户信息..."):
|
|
98
|
+
account_profile = get_account_profile(common.require_app_paths())
|
|
99
|
+
except UseCaseError as exc:
|
|
100
|
+
common.exit_for_use_case_error(exc)
|
|
101
|
+
render_profile(account_profile)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from typing import NoReturn
|
|
2
|
+
|
|
3
|
+
from typer import Exit
|
|
4
|
+
|
|
5
|
+
from leetcode_local_cli.config import ConfigError, resolve_app_paths
|
|
6
|
+
from leetcode_local_cli.paths import AppPaths
|
|
7
|
+
from leetcode_local_cli.ui import error, warning
|
|
8
|
+
from leetcode_local_cli.use_cases.common import UseCaseError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def require_app_paths() -> AppPaths:
|
|
12
|
+
try:
|
|
13
|
+
return resolve_app_paths()
|
|
14
|
+
except ConfigError as exc:
|
|
15
|
+
error(str(exc))
|
|
16
|
+
raise Exit(1) from exc
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def exit_for_use_case_error(exc: UseCaseError) -> NoReturn:
|
|
20
|
+
if exc.warning_only:
|
|
21
|
+
warning(exc.message)
|
|
22
|
+
else:
|
|
23
|
+
error(exc.message)
|
|
24
|
+
if exc.suggestion:
|
|
25
|
+
warning(exc.suggestion)
|
|
26
|
+
raise Exit(1) from exc
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from leetcode_local_cli.commands import common
|
|
2
|
+
from leetcode_local_cli.ui import (
|
|
3
|
+
loading,
|
|
4
|
+
render_problem_detail,
|
|
5
|
+
render_problem_list,
|
|
6
|
+
)
|
|
7
|
+
from leetcode_local_cli.use_cases.common import UseCaseError
|
|
8
|
+
from leetcode_local_cli.use_cases.problems import (
|
|
9
|
+
get_problem_detail_by_question_id,
|
|
10
|
+
get_problem_summaries,
|
|
11
|
+
write_problem_solution,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_problem(question_id: str) -> None:
|
|
16
|
+
try:
|
|
17
|
+
problem_detail = get_problem_detail_by_question_id(
|
|
18
|
+
common.require_app_paths(),
|
|
19
|
+
question_id,
|
|
20
|
+
progress=loading,
|
|
21
|
+
)
|
|
22
|
+
except UseCaseError as exc:
|
|
23
|
+
common.exit_for_use_case_error(exc)
|
|
24
|
+
render_problem_detail(problem_detail)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def show(limit: int = 50, skip: int = 0) -> None:
|
|
28
|
+
try:
|
|
29
|
+
problem_summaries = get_problem_summaries(
|
|
30
|
+
common.require_app_paths(),
|
|
31
|
+
limit=limit,
|
|
32
|
+
skip=skip,
|
|
33
|
+
progress=loading,
|
|
34
|
+
)
|
|
35
|
+
except UseCaseError as exc:
|
|
36
|
+
common.exit_for_use_case_error(exc)
|
|
37
|
+
render_problem_list(problem_summaries)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def solve(question_id: str) -> None:
|
|
41
|
+
paths = common.require_app_paths()
|
|
42
|
+
try:
|
|
43
|
+
problem_detail = get_problem_detail_by_question_id(
|
|
44
|
+
paths,
|
|
45
|
+
question_id,
|
|
46
|
+
progress=loading,
|
|
47
|
+
)
|
|
48
|
+
except UseCaseError as exc:
|
|
49
|
+
common.exit_for_use_case_error(exc)
|
|
50
|
+
render_problem_detail(problem_detail)
|
|
51
|
+
try:
|
|
52
|
+
write_problem_solution(paths, problem_detail)
|
|
53
|
+
except UseCaseError as exc:
|
|
54
|
+
common.exit_for_use_case_error(exc)
|