LeetCode API Rust Library
This Rust library provides a convenient way to interact with the LeetCode API, allowing you to programmatically access LeetCode problems, submit solutions, and retrieve submission results.
Features
- Retrieve a list of LeetCode problems.
- Fetch problem details, including the problem description, constraints, and examples.
- Submit solutions to LeetCode problems.
- Check submission results, including status, runtime, and memory usage.
Installation
Add the following line to your Cargo.toml
file:
[dependencies]
leetcoderustapi = "1.0.0"
Usage
Authentication
To use the LeetCode API, you need to obtain an authentication token. Follow the instructions provided by LeetCode to obtain your token.
Example: Action with problems
use leetcoderustapi::{problem_build::{Tags, Category, Difficulty, Status}, UserApi, ProgrammingLanguage,};
#[tokio::main]
async fn main() {
let token = std::env::var("COOKIE").expect("cookie doesn't set");
let api = UserApi::new(&token).await.unwrap();
let show_problems = api.show_problm_list("sum", 5).await.unwrap();
let problems_builder = api
.problem_builder()
.set_category(Category::Algorithms)
.set_difficulty(Difficulty::Easy)
.set_keyword("sum")
.set_note_limit(3)
.set_status(Status::Solved)
.set_tags(vec![
Tags::Array,
Tags::BinarySearch,
])
.build()
.await
.unwrap();
let problem_info = api.set_problem("two sum").await.unwrap();
let my_submissions = problem_info.my_submissions().await.unwrap();
let code_snippets = problem_info.code_snippets().unwrap();
let solution_info = problem_info.solution_info().unwrap();
let related_topics = problem_info.related_topics();
let similar_questions = problem_info.similar_questions().unwrap();
let stats = problem_info.stats().unwrap();
let hints = problem_info.hints();
let description = problem_info.description().unwrap();
let difficulty = problem_info.difficulty();
let likes = problem_info.rating().unwrap();
let category = problem_info.category();
let subm_response = problem_info
.send_subm(ProgrammingLanguage::Rust, "impl Solution { fn two_sum() {}}")
.await
.unwrap();
let test_response = problem_info
.send_test(ProgrammingLanguage::Rust, "impl Solution { fn two_sum() {}}")
.await
.unwrap();
}
Example: Actions with Self profile
#[tokio::main]
async fn main() {
let token = std::env::var("COOKIE").expect("cookie doesn't set");
let api = UserApi::new(&token).await.unwrap();
let user_profile = api.my_profile().await.unwrap();
user_profile
.create_fav_list("my_new_favorite_list")
.await
.unwrap();
user_profile
.rename_fav_list("my_new_favorite_list", "hard_problems")
.await
.unwrap();
user_profile.set_public("hard_problems").await.unwrap();
user_profile.set_private("hard_problems").await.unwrap();
let share_list_url = user_profile.get_share_url("hard_problems").await.unwrap();
let lists = user_profile.show_lists();
user_profile
.delete_list("hard_problems")
.await
.unwrap();
let notifications = user_profile.get_notifications().await.unwrap();
}
Example: Actions with Public user profile
#[tokio::main]
async fn main() {
let token = std::env::var("COOKIE").expect("cookie doesn't set");
let api = UserApi::new(&token).await.unwrap();
let user = api
.find_profile("1101-1")
.await;
let user_stats = user.
user_stats()
.await
.unwrap();
let lang_stats = user
.language_stats()
.await
.unwrap();
let skill_stats = user
.skill_stats()
.await
.unwrap();
let beat_stats = ser
.problem_beat_stats()
.await
.unwrap();
let beat_stats = ser
.recent_subm_list()
.await
.unwrap();
}
Important
Replace "COOKIE"
with your actual LeetCode authentication cookie.
For example format in .env
file:
COOKIE="csrftoken=gN3mmFEKoBFHLZuiHEvZYupqirq7brDmi845GhUK8xBa9u3SUVkgTPFTPsLFuAzR; _ga_CDRWKZTDEX=GS1.1.1688568040.1.1.1688568081.19.0.0; _ga=GA1.1.2048740381.1688568040; _dd_s=rum=0&expire=1688568980299; NEW_PROBLEMLIST_PAGE=1"
License
This library is licensed under the MIT License
.