Skip to content

Feat: support leetcode.cn #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: support leetcode.cn
  • Loading branch information
wesleyel committed Jul 4, 2024
commit 78b9737c9550038aa0b88c16824aa5102ed41a03
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
[![telegram](https://img.shields.io/badge/telegram-blue?logo=telegram)](https://t.me/+U_5si6PhWykxZTI1)
[![LICENSE](https://img.shields.io/crates/l/leetcode-cli.svg)](https://choosealicense.com/licenses/mit/)

Modify for **leetcode.cn**

## Installing

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

## Usage

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

```sh
leetcode 0.4.0
Expand Down Expand Up @@ -307,7 +309,7 @@ Open Firefox, press F12, and click `Storage` tab.

#### Step 2

Expand `Cookies` tab on the left and select https://leetcode.com.
Expand `Cookies` tab on the left and select https://leetcode.cn.

#### Step 2

Expand Down
11 changes: 9 additions & 2 deletions src/cache/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Option<()> {

problems.push(Problem {
category: v.get("category_slug")?.as_str()?.to_string(),
fid: stat.get("frontend_question_id")?.as_i64()? as i32,
fid: stat
.get("frontend_question_id")?
.as_str()?
.split(" ")
.last()?
.parse::<i32>()
.ok()?,
id: stat.get("question_id")?.as_i64()? as i32,
level: p.get("difficulty")?.as_object()?.get("level")?.as_i64()? as i32,
locked: p.get("paid_only")?.as_bool()?,
Expand Down Expand Up @@ -92,7 +98,8 @@ pub fn daily(v: Value) -> Option<i32> {
v.as_object()?
.get("data")?
.as_object()?
.get("activeDailyCodingChallengeQuestion")?
.get("todayRecord")?
.as_array()?[0]
.as_object()?
.get("question")?
.as_object()?
Expand Down
28 changes: 14 additions & 14 deletions src/config/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ pub struct Urls {
impl Default for Urls {
fn default() -> Self {
Self {
base: "https://leetcode.com".into(),
graphql: "https://leetcode.com/graphql".into(),
login: "https://leetcode.com/accounts/login/".into(),
problems: "https://leetcode.com/api/problems/$category/".into(),
problem: "https://leetcode.com/problems/$slug/description/".into(),
tag: "https://leetcode.com/tag/$slug/".into(),
test: "https://leetcode.com/problems/$slug/interpret_solution/".into(),
session: "https://leetcode.com/session/".into(),
submit: "https://leetcode.com/problems/$slug/submit/".into(),
submissions: "https://leetcode.com/submissions/detail/$id/".into(),
submission: "https://leetcode.com/submissions/detail/$id/".into(),
verify: "https://leetcode.com/submissions/detail/$id/check/".into(),
favorites: "https://leetcode.com/list/api/questions".into(),
favorite_delete: "https://leetcode.com/list/api/questions/$hash/$id".into(),
base: "https://leetcode.cn".into(),
graphql: "https://leetcode.cn/graphql".into(),
login: "https://leetcode.cn/accounts/login/".into(),
problems: "https://leetcode.cn/api/problems/$category/".into(),
problem: "https://leetcode.cn/problems/$slug/description/".into(),
tag: "https://leetcode.cn/tag/$slug/".into(),
test: "https://leetcode.cn/problems/$slug/interpret_solution/".into(),
session: "https://leetcode.cn/session/".into(),
submit: "https://leetcode.cn/problems/$slug/submit/".into(),
submissions: "https://leetcode.cn/submissions/detail/$id/".into(),
submission: "https://leetcode.cn/submissions/detail/$id/".into(),
verify: "https://leetcode.cn/submissions/detail/$id/check/".into(),
favorites: "https://leetcode.cn/list/api/questions".into(),
favorite_delete: "https://leetcode.cn/list/api/questions/$hash/$id".into(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//! ## Usage
//!
//! **Please make sure you have logined in `leetcode.com` with `chrome`**, more info plz checkout [this](#cookies)
//! **Please make sure you have logined in `leetcode.cn` with `chrome`**, more info plz checkout [this](#cookies)
//!
//! ```sh
//! leetcode 0.3.10
Expand Down Expand Up @@ -142,15 +142,15 @@
//! session = "..."
//! ```
//!
//! For Example, if you're using chrome to login to leetcode.com.
//! For Example, if you're using chrome to login to leetcode.cn.
//!
//!
//! #### Step 1
//!
//! Open chrome and paste the link below to the `chrome linkbar`.
//!
//! ```sh
//! chrome://settings/cookies/detail?site=leetcode.com
//! chrome://settings/cookies/detail?site=leetcode.cn
//! ```
//!
//! #### Step 2
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn cookies() -> Result<Ident> {
debug!("Chrome Cookies path is {:?}", &p);
let mut conn = cache::conn(p.to_string_lossy().to_string());
let res = cookies
.filter(host_key.like("%leetcode.com"))
.filter(host_key.like("%leetcode.cn"))
.load::<Cookies>(&mut conn)
.expect("Loading cookies from google chrome failed.");

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/leetcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ impl LeetCode {
trace!("Requesting daily problem...");
let url = &self.conf.sys.urls.graphql;
let mut json: Json = HashMap::new();
json.insert("operationName", "daily".to_string());
json.insert("operationName", "questionOfToday".to_string());
json.insert(
"query",
vec![
"query daily {",
" activeDailyCodingChallengeQuestion {",
"query questionOfToday {",
" todayRecord {",
" question {",
" questionFrontendId",
" }",
Expand All @@ -159,7 +159,7 @@ impl LeetCode {
]
.join("\n"),
);

Req {
default_headers: self.default_headers,
refer: None,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! + chrome cookie parser
//! + leetcode API
//!
//! ## login to `leetcode.com`
//! Leetcode-cli use chrome cookie directly, do not need to login, please make sure you have loggined in `leetcode.com` before usnig `leetcode-cli`
//! ## login to `leetcode.cn`
//! Leetcode-cli use chrome cookie directly, do not need to login, please make sure you have loggined in `leetcode.cn` before usnig `leetcode-cli`
//!

// FIXME: Read cookies from local storage. (issue #122)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy