Skip to content

Commit 78b9737

Browse files
committed
feat: support leetcode.cn
1 parent 39789f2 commit 78b9737

File tree

7 files changed

+37
-28
lines changed

7 files changed

+37
-28
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
[![telegram](https://img.shields.io/badge/telegram-blue?logo=telegram)](https://t.me/+U_5si6PhWykxZTI1)
88
[![LICENSE](https://img.shields.io/crates/l/leetcode-cli.svg)](https://choosealicense.com/licenses/mit/)
99

10+
Modify for **leetcode.cn**
11+
1012
## Installing
1113

1214
```sh
@@ -43,7 +45,7 @@ If no argument is provided, the shell is inferred from the `SHELL` environment v
4345

4446
## Usage
4547

46-
**Make sure you have logged in to `leetcode.com` with `Firefox`**. See [Cookies](#cookies) for why you need to do this first.
48+
**Make sure you have logged in to `leetcode.cn` with `Firefox`**. See [Cookies](#cookies) for why you need to do this first.
4749

4850
```sh
4951
leetcode 0.4.0
@@ -307,7 +309,7 @@ Open Firefox, press F12, and click `Storage` tab.
307309

308310
#### Step 2
309311

310-
Expand `Cookies` tab on the left and select https://leetcode.com.
312+
Expand `Cookies` tab on the left and select https://leetcode.cn.
311313

312314
#### Step 2
313315

src/cache/parser.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Option<()> {
1212

1313
problems.push(Problem {
1414
category: v.get("category_slug")?.as_str()?.to_string(),
15-
fid: stat.get("frontend_question_id")?.as_i64()? as i32,
15+
fid: stat
16+
.get("frontend_question_id")?
17+
.as_str()?
18+
.split(" ")
19+
.last()?
20+
.parse::<i32>()
21+
.ok()?,
1622
id: stat.get("question_id")?.as_i64()? as i32,
1723
level: p.get("difficulty")?.as_object()?.get("level")?.as_i64()? as i32,
1824
locked: p.get("paid_only")?.as_bool()?,
@@ -92,7 +98,8 @@ pub fn daily(v: Value) -> Option<i32> {
9298
v.as_object()?
9399
.get("data")?
94100
.as_object()?
95-
.get("activeDailyCodingChallengeQuestion")?
101+
.get("todayRecord")?
102+
.as_array()?[0]
96103
.as_object()?
97104
.get("question")?
98105
.as_object()?

src/config/sys.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ pub struct Urls {
3333
impl Default for Urls {
3434
fn default() -> Self {
3535
Self {
36-
base: "https://leetcode.com".into(),
37-
graphql: "https://leetcode.com/graphql".into(),
38-
login: "https://leetcode.com/accounts/login/".into(),
39-
problems: "https://leetcode.com/api/problems/$category/".into(),
40-
problem: "https://leetcode.com/problems/$slug/description/".into(),
41-
tag: "https://leetcode.com/tag/$slug/".into(),
42-
test: "https://leetcode.com/problems/$slug/interpret_solution/".into(),
43-
session: "https://leetcode.com/session/".into(),
44-
submit: "https://leetcode.com/problems/$slug/submit/".into(),
45-
submissions: "https://leetcode.com/submissions/detail/$id/".into(),
46-
submission: "https://leetcode.com/submissions/detail/$id/".into(),
47-
verify: "https://leetcode.com/submissions/detail/$id/check/".into(),
48-
favorites: "https://leetcode.com/list/api/questions".into(),
49-
favorite_delete: "https://leetcode.com/list/api/questions/$hash/$id".into(),
36+
base: "https://leetcode.cn".into(),
37+
graphql: "https://leetcode.cn/graphql".into(),
38+
login: "https://leetcode.cn/accounts/login/".into(),
39+
problems: "https://leetcode.cn/api/problems/$category/".into(),
40+
problem: "https://leetcode.cn/problems/$slug/description/".into(),
41+
tag: "https://leetcode.cn/tag/$slug/".into(),
42+
test: "https://leetcode.cn/problems/$slug/interpret_solution/".into(),
43+
session: "https://leetcode.cn/session/".into(),
44+
submit: "https://leetcode.cn/problems/$slug/submit/".into(),
45+
submissions: "https://leetcode.cn/submissions/detail/$id/".into(),
46+
submission: "https://leetcode.cn/submissions/detail/$id/".into(),
47+
verify: "https://leetcode.cn/submissions/detail/$id/check/".into(),
48+
favorites: "https://leetcode.cn/list/api/questions".into(),
49+
favorite_delete: "https://leetcode.cn/list/api/questions/$hash/$id".into(),
5050
}
5151
}
5252
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! ## Usage
1414
//!
15-
//! **Please make sure you have logined in `leetcode.com` with `chrome`**, more info plz checkout [this](#cookies)
15+
//! **Please make sure you have logined in `leetcode.cn` with `chrome`**, more info plz checkout [this](#cookies)
1616
//!
1717
//! ```sh
1818
//! leetcode 0.3.10
@@ -142,15 +142,15 @@
142142
//! session = "..."
143143
//! ```
144144
//!
145-
//! For Example, if you're using chrome to login to leetcode.com.
145+
//! For Example, if you're using chrome to login to leetcode.cn.
146146
//!
147147
//!
148148
//! #### Step 1
149149
//!
150150
//! Open chrome and paste the link below to the `chrome linkbar`.
151151
//!
152152
//! ```sh
153-
//! chrome://settings/cookies/detail?site=leetcode.com
153+
//! chrome://settings/cookies/detail?site=leetcode.cn
154154
//! ```
155155
//!
156156
//! #### Step 2

src/plugins/chrome.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn cookies() -> Result<Ident> {
6464
debug!("Chrome Cookies path is {:?}", &p);
6565
let mut conn = cache::conn(p.to_string_lossy().to_string());
6666
let res = cookies
67-
.filter(host_key.like("%leetcode.com"))
67+
.filter(host_key.like("%leetcode.cn"))
6868
.load::<Cookies>(&mut conn)
6969
.expect("Loading cookies from google chrome failed.");
7070

src/plugins/leetcode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ impl LeetCode {
145145
trace!("Requesting daily problem...");
146146
let url = &self.conf.sys.urls.graphql;
147147
let mut json: Json = HashMap::new();
148-
json.insert("operationName", "daily".to_string());
148+
json.insert("operationName", "questionOfToday".to_string());
149149
json.insert(
150150
"query",
151151
vec![
152-
"query daily {",
153-
" activeDailyCodingChallengeQuestion {",
152+
"query questionOfToday {",
153+
" todayRecord {",
154154
" question {",
155155
" questionFrontendId",
156156
" }",
@@ -159,7 +159,7 @@ impl LeetCode {
159159
]
160160
.join("\n"),
161161
);
162-
162+
163163
Req {
164164
default_headers: self.default_headers,
165165
refer: None,

src/plugins/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! + chrome cookie parser
44
//! + leetcode API
55
//!
6-
//! ## login to `leetcode.com`
7-
//! Leetcode-cli use chrome cookie directly, do not need to login, please make sure you have loggined in `leetcode.com` before usnig `leetcode-cli`
6+
//! ## login to `leetcode.cn`
7+
//! Leetcode-cli use chrome cookie directly, do not need to login, please make sure you have loggined in `leetcode.cn` before usnig `leetcode-cli`
88
//!
99
1010
// FIXME: Read cookies from local storage. (issue #122)

0 commit comments

Comments
 (0)