Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bbbc795

Browse files
committed
feat: Add README.md
1 parent 956ce72 commit bbbc795

File tree

8 files changed

+231
-123
lines changed

8 files changed

+231
-123
lines changed

‎README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# LeetCode API Rust Library
2+
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.
3+
## Features
4+
* Retrieve a list of LeetCode problems.
5+
* Fetch problem details, including the problem description, constraints, and examples.
6+
* Submit solutions to LeetCode problems.
7+
* Check submission results, including status, runtime, and memory usage.
8+
9+
## Installation
10+
11+
Add the following line to your `Cargo.toml` file:
12+
```toml
13+
[dependencies]
14+
leetcode-api = "0.1.0"
15+
```
16+
## Usage
17+
### Authentication
18+
To use the LeetCode API, you need to obtain an authentication token. Follow the instructions provided by LeetCode to obtain your token.
19+
20+
### Example
21+
```rust
22+
use leetcoderustapi::UserApi;
23+
24+
#[tokio::main]
25+
async fn main() {
26+
// Set cookie from leetcode
27+
let token = std::env::var("COOKIE").expect("cookie doesn't set");
28+
29+
// Create a new LeetCode client
30+
let api = UserApi::new(&token);
31+
32+
// Show found problems by keyword and show 5 notes
33+
let show_problems = api.show_tasks_list("sum", 5).await.unwrap();
34+
35+
// Find problems by properties with creating builder
36+
let show_problems_builder = api
37+
.show_task_builder()
38+
.set_category(leetcoderustapi::Category::Algorithms)
39+
.set_difficulty(leetcoderustapi::Difficulty::Easy)
40+
.set_keyword("sum")
41+
//max show notes limit is 2763; default is 5
42+
.set_note_limit(3)
43+
.set_status(leetcoderustapi::Status::Solved)
44+
.set_tags(vec![
45+
leetcoderustapi::Tags::Array,
46+
leetcoderustapi::Tags::BinarySearch,
47+
])
48+
.build()
49+
.await
50+
.unwrap();
51+
52+
// Fetch the full data for a specific problem
53+
let problem_info = api.set_task("two sum").await.unwrap();
54+
55+
// Retrieve code snippets
56+
let code_snippets = problem_info.code_snippets();
57+
58+
// Retrieve solution info
59+
let solution_info = problem_info.solution_info();
60+
61+
// Retrieve related topics
62+
let related_topics = problem_info.related_topics();
63+
64+
// Retrieve similar questions
65+
let similar_questions = problem_info.similar_questions();
66+
67+
// Retrieve stats
68+
let stats = problem_info.stats();
69+
70+
// Retrieve hints
71+
let hints = problem_info.hints();
72+
73+
// Retrieve description
74+
let description = problem_info.description();
75+
76+
// Retrieve difficulty
77+
let difficulty = problem_info.difficulty();
78+
79+
// Retrieve likes
80+
let likes = problem_info.likes();
81+
82+
// Retrieve category
83+
let category = problem_info.category();
84+
85+
// We also can send submissions and tests
86+
// Need to specify a lang and provided code
87+
let subm_response = problem_info
88+
.send_subm("rust", "impl Solution { fn two_sum() {}}")
89+
.await
90+
.unwrap();
91+
let test_response = problem_info
92+
.send_test("rust", "impl Solution { fn two_sum() {}}")
93+
.await
94+
.unwrap();
95+
}
96+
```
97+
#### Important
98+
Replace `"COOKIE"` with your actual LeetCode authentication cookie.
99+
100+
For example format in `.env` file:
101+
102+
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"
103+
104+
### License
105+
This library is licensed under the `MIT License`.

‎src/lib.rs

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ use std::error::Error;
22

33
use reqwest::header::{HeaderMap, HeaderValue};
44
use serde_json::json;
5-
use source::{taskfulldata::TaskFullData, descr::TaskData};
5+
use source::{descr::TaskData, taskfulldata::TaskFullData};
66
use task_actions::Task;
7-
use task_build::{TaskBuilder,Filters};
7+
use task_build::{Filters,TaskBuilder};
88

9-
mod source;
10-
mod task_actions;
11-
mod task_build;
12-
mod query_enums;
9+
pub mod source;
10+
pub mod task_actions;
11+
pub mod task_build;
1312

14-
15-
pub(crate) struct UserApi {
13+
pub struct UserApi {
1614
client: reqwest::Client,
1715
}
1816

@@ -92,7 +90,7 @@ impl UserApi {
9290
Ok((full_data.data.question.titleSlug.clone(), full_data))
9391
}
9492

95-
pub async fn show_task_list(
93+
pub async fn show_tasks_list(
9694
&self,
9795
key_word: &str,
9896
limit: u32,
@@ -176,3 +174,102 @@ impl UserApi {
176174
.clone())
177175
}
178176
}
177+
178+
#[allow(unused)]
179+
pub enum Category {
180+
AllTopics,
181+
Algorithms,
182+
DataBase,
183+
JavaScript,
184+
Shell,
185+
Concurrency,
186+
}
187+
188+
#[allow(unused)]
189+
pub enum Difficulty {
190+
Easy,
191+
Medium,
192+
Hard,
193+
}
194+
195+
#[allow(unused)]
196+
pub enum Status {
197+
Todo,
198+
Solved,
199+
Attempted,
200+
}
201+
#[allow(unused)]
202+
#[derive(Debug)]
203+
pub enum Tags {
204+
Array,
205+
String,
206+
HashTable,
207+
Math,
208+
DynamicProgramming,
209+
Sorting,
210+
Greedy,
211+
DepthFirstSearch,
212+
Database,
213+
BinarySearch,
214+
BreadthFirstSearch,
215+
Tree,
216+
Matrix,
217+
TwoPointers,
218+
BinaryTree,
219+
BitManipulation,
220+
HeapPriorityQueue,
221+
Stack,
222+
Graph,
223+
PrefixSum,
224+
Simulation,
225+
Design,
226+
Counting,
227+
Backtracking,
228+
SlidingWindow,
229+
UnionFind,
230+
LinkedList,
231+
OrderedSet,
232+
MonotonicStack,
233+
Enumeration,
234+
Recursion,
235+
Trie,
236+
DivideAndConquer,
237+
Bitmask,
238+
BinarySearchTree,
239+
NumberTheory,
240+
Queue,
241+
SegmentTree,
242+
Memoization,
243+
Geometry,
244+
TopologicalSort,
245+
BinaryIndexedTree,
246+
HashFunction,
247+
GameTheory,
248+
ShortestPath,
249+
Combinatorics,
250+
DataStream,
251+
Interactive,
252+
StringMatching,
253+
RollingHash,
254+
Brainteaser,
255+
Randomized,
256+
MonotonicQueue,
257+
MergeSort,
258+
Iterator,
259+
Concurrency,
260+
DoublyLinkedList,
261+
ProbabilityStatistics,
262+
Quickselect,
263+
BucketSort,
264+
SuffixArray,
265+
MinimumSpanningTree,
266+
CountingSort,
267+
Shell,
268+
LineSweep,
269+
ReservoirSampling,
270+
EulerianCircuit,
271+
RadixSort,
272+
StronglyConnectedComponent,
273+
RejectionSampling,
274+
BiconnectedComponent,
275+
}

‎src/query_enums.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

‎src/source/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
pub mod descr;
22
pub mod subm_send;
33
pub mod subm_show;
4-
pub mod test_send;
54
pub mod taskfulldata;
5+
pub mod test_send;
66

77
use serde::Deserialize;
88

99
#[allow(unused)]
1010
#[derive(Deserialize, Debug)]
11-
pub(crate) struct Rate {
11+
pub struct Rate {
1212
pub likes: u32,
1313
pub dislikes: u32,
1414
}
1515

1616
#[allow(unused)]
1717
#[derive(Deserialize, Debug)]
18-
pub(crate) struct Descryption {
18+
pub struct Descryption {
1919
pub name: String,
2020
pub content: String,
21-
}
21+
}

‎src/source/subm_send.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use serde::{Deserialize, Serialize};
22

33
#[derive(Deserialize, Debug)]
4-
pub(crate) struct SubmissionCaseResp {
4+
pub struct SubmissionCaseResp {
55
pub submission_id: u32,
66
}
77

88
#[derive(Serialize, Debug)]
9-
pub(crate) struct SubmissionCase {
9+
pub struct SubmissionCase {
1010
pub question_id: String,
1111
pub lang: String,
1212
pub typed_code: String,
1313
}
1414

1515
#[allow(unused)]
1616
#[derive(Deserialize, Debug)]
17-
pub(crate) struct SubmExecutionResult {
17+
pub struct SubmExecutionResult {
1818
pub status_code: Option<u32>,
1919
pub lang: Option<String>,
2020
pub run_success: Option<bool>,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /