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 66ccc99

Browse files
committed
feat: Add list actions
1 parent 74acd1f commit 66ccc99

File tree

6 files changed

+313
-48
lines changed

6 files changed

+313
-48
lines changed

‎src/lib.rs‎

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
use error::Errors;
22
use problem_actions::Problem;
3-
use problem_build::{Filters, TaskBuilder};
3+
use problem_build::{Filters, ProblemBuilder};
4+
use profile::{MyProfile, UserProfile};
45
use reqwest::header::{HeaderMap, HeaderValue};
5-
use resources::{cookie::CookieData, descr::ProblemData, problemfulldata::ProblemFullData};
6+
use resources::{
7+
cookie::CookieData, descr::ProblemData, fav_list::FavoriteList,
8+
problemfulldata::ProblemFullData,
9+
};
610
use serde_json::{json, Value};
711

812
pub mod error;
913
pub mod problem_actions;
1014
pub mod problem_build;
15+
pub mod profile;
1116
pub mod resources;
1217

1318
pub struct UserApi {
@@ -105,7 +110,7 @@ impl UserApi {
105110
}
106111

107112
pub async fn set_problem(&self, task: &str) -> Result<Problem, Errors> {
108-
let info = Self::fetch_full_data(
113+
let info = Self::fetch_problem_full_data(
109114
&self,
110115
Self::get_question_name(&self, String::from(task)).await?,
111116
)
@@ -118,7 +123,7 @@ impl UserApi {
118123
})
119124
}
120125

121-
async fn fetch_full_data(
126+
async fn fetch_problem_full_data(
122127
&self,
123128
task_name: String,
124129
) -> Result<(String, ProblemFullData), Errors> {
@@ -164,20 +169,20 @@ impl UserApi {
164169

165170
let query = serde_json::to_string(&query)?;
166171

167-
let task_info = self
172+
let problem_info = self
168173
.client
169-
.get("https://leetcode.com/graphql/")
174+
.post("https://leetcode.com/graphql/")
170175
.body(query)
171176
.send()
172177
.await?
173178
.text()
174179
.await?;
175180

176-
Ok(serde_json::from_str::<ProblemData>(&task_info)?)
181+
Ok(serde_json::from_str::<ProblemData>(&problem_info)?)
177182
}
178183

179-
pub fn problem_builder(&self) -> TaskBuilder {
180-
TaskBuilder {
184+
pub fn problem_builder(&self) -> ProblemBuilder {
185+
ProblemBuilder {
181186
client: self.client.clone(),
182187
key_word: String::new(),
183188
limit: 5,
@@ -202,21 +207,95 @@ impl UserApi {
202207

203208
let query = serde_json::to_string(&query)?;
204209

205-
let task_info = self
210+
let problem_info = self
206211
.client
207-
.get("https://leetcode.com/graphql/")
212+
.post("https://leetcode.com/graphql/")
208213
.body(query)
209214
.send()
210215
.await?
211216
.text()
212217
.await?;
213218

214-
let parsed_data: ProblemData = serde_json::from_str(&task_info)?;
219+
let parsed_data: ProblemData = serde_json::from_str(&problem_info)?;
215220

216221
Ok(parsed_data.data.problemsetQuestionList.questions[0]
217222
.titleSlug
218223
.clone())
219224
}
225+
226+
pub async fn my_profile(&self) -> Result<MyProfile, Errors> {
227+
Ok(MyProfile {
228+
client: self.client.clone(),
229+
fav_lists: Self::fetch_fav_list_data(&self).await?,
230+
})
231+
}
232+
233+
pub async fn find_user(&self, username: &str) -> UserProfile {
234+
todo!()
235+
}
236+
237+
async fn fetch_fav_list_data(&self) -> Result<FavoriteList, Errors> {
238+
let query = json!({
239+
"operationName": "favoritesList",
240+
"variables": {},
241+
"query": "query favoritesList {
242+
favoritesLists {
243+
allFavorites {
244+
idHash
245+
name
246+
description
247+
viewCount
248+
creator
249+
isWatched
250+
isPublicFavorite
251+
questions {
252+
questionId
253+
status
254+
title
255+
titleSlug
256+
__typename
257+
}
258+
__typename
259+
}
260+
watchedFavorites {
261+
idHash
262+
name
263+
description
264+
viewCount
265+
creator
266+
isWatched
267+
isPublicFavorite
268+
questions {
269+
questionId
270+
status
271+
title
272+
titleSlug
273+
__typename
274+
}
275+
__typename
276+
}
277+
__typename
278+
}
279+
userStatus {
280+
username
281+
__typename
282+
}
283+
}"
284+
});
285+
286+
let query = serde_json::to_string(&query)?;
287+
288+
let list_data = self
289+
.client
290+
.post("https://leetcode.com/graphql/")
291+
.body(query)
292+
.send()
293+
.await?
294+
.text()
295+
.await?;
296+
297+
Ok(serde_json::from_str::<FavoriteList>(&list_data)?)
298+
}
220299
}
221300

222301
#[derive(Debug)]

‎src/problem_actions.rs‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ use std::time::Duration;
22

33
use serde_json::json;
44

5-
use crate::{resources::{
6-
problemfulldata::{
7-
CodeSnippetNode, ProblemFullData, SimilarQuestions, Solution, Statistics, TopicTagNode,
5+
use crate::{
6+
error::Errors,
7+
resources::{
8+
problemfulldata::{
9+
CodeSnippetNode, ProblemFullData, SimilarQuestions, Solution, Statistics, TopicTagNode,
10+
},
11+
subm_send::{SubmExecutionResult, SubmissionCase, SubmissionCaseResp},
12+
subm_show::SubmList,
13+
test_send::{TestCase, TestCaseResp, TestExecutionResult},
14+
Descryption, Rate,
815
},
9-
subm_send::{SubmExecutionResult, SubmissionCase, SubmissionCaseResp},
10-
subm_show::SubmList,
11-
test_send::{TestCase, TestCaseResp, TestExecutionResult},
12-
Descryption, Rate,
13-
}, error::Errors};
16+
};
1417

1518
#[derive(Debug)]
1619
pub struct Problem {
@@ -64,17 +67,12 @@ impl Problem {
6467
}
6568
}
6669

67-
pub async fn send_subm(
68-
&self,
69-
lang: &str,
70-
code: &str,
71-
) -> Result<SubmExecutionResult, Errors> {
70+
pub async fn send_subm(&self, lang: &str, code: &str) -> Result<SubmExecutionResult, Errors> {
7271
let json_data = serde_json::to_string(&SubmissionCase {
7372
question_id: self.full_data.data.question.questionId.clone(),
7473
lang: lang.to_lowercase(),
7574
typed_code: String::from(code),
76-
})
77-
.unwrap();
75+
})?;
7876

7977
let resp = self
8078
.client
@@ -117,39 +115,40 @@ impl Problem {
117115
self.full_data.data.question.topicTags.clone()
118116
}
119117

120-
pub fn similar_questions(&self) -> Vec<SimilarQuestions> {
121-
serde_json::from_str::<Vec<SimilarQuestions>>(
118+
pub fn similar_questions(&self) -> Result<Vec<SimilarQuestions>,Errors> {
119+
Ok(serde_json::from_str::<Vec<SimilarQuestions>>(
122120
self.full_data.data.question.similarQuestions.as_str(),
123-
)
124-
.unwrap()
121+
)?)
125122
}
126-
pub fn stats(&self) -> Statistics {
127-
serde_json::from_str::<Statistics>(self.full_data.data.question.stats.as_str()).unwrap()
123+
pub fn stats(&self) -> Result<Statistics, Errors> {
124+
Ok(serde_json::from_str::<Statistics>(
125+
self.full_data.data.question.stats.as_str(),
126+
)?)
128127
}
129128

130129
pub fn hints(&self) -> Vec<String> {
131130
self.full_data.data.question.hints.clone()
132131
}
133132

134-
pub fn description(&self) -> Descryption {
133+
pub fn description(&self) -> Result<Descryption,Errors> {
135134
let descryption = json!({
136135
"name": self.full_data.data.question.title,
137136
"content": self.full_data.data.question.content
138137
});
139-
serde_json::from_value::<Descryption>(descryption).unwrap()
138+
Ok(serde_json::from_value::<Descryption>(descryption)?)
140139
}
141140

142141
pub fn difficulty(&self) -> String {
143142
self.full_data.data.question.difficulty.clone()
144143
}
145144

146-
pub fn rating(&self) -> Rate {
145+
pub fn rating(&self) -> Result<Rate,Errors> {
147146
let rate = json!({
148147
"likes": self.full_data.data.question.likes,
149148
"dislikes": self.full_data.data.question.dislikes
150149
});
151150

152-
serde_json::from_value::<Rate>(rate).unwrap()
151+
Ok(serde_json::from_value::<Rate>(rate)?)
153152
}
154153

155154
pub fn category(&self) -> String {
@@ -161,7 +160,7 @@ impl Problem {
161160
"operationName": "Submissions",
162161
"variables": {
163162
"offset": 0,
164-
"limit": 20,
163+
"limit": 10,
165164
"lastKey": null,
166165
"questionSlug": self.task_search_name
167166
},

‎src/problem_build.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde_json::json;
44
use crate::{error::Errors, resources::descr::ProblemData, Category, Difficulty, Status, Tags};
55

66
#[derive(Debug)]
7-
pub struct TaskBuilder {
7+
pub struct ProblemBuilder {
88
pub client: reqwest::Client,
99
pub key_word: String,
1010
pub limit: u32,
@@ -35,8 +35,8 @@ impl Default for Filters {
3535
}
3636

3737
#[allow(unused)]
38-
impl TaskBuilder {
39-
pub fn set_category(mut self, categoty: Category) -> TaskBuilder {
38+
impl ProblemBuilder {
39+
pub fn set_category(mut self, categoty: Category) -> ProblemBuilder {
4040
match categoty {
4141
Category::AllTopics => self.category = String::from(""),
4242
Category::Algorithms => self.category = String::from("algorithms"),
@@ -48,7 +48,7 @@ impl TaskBuilder {
4848
self
4949
}
5050

51-
pub fn set_difficulty(mut self, difficulty: Difficulty) -> TaskBuilder {
51+
pub fn set_difficulty(mut self, difficulty: Difficulty) -> ProblemBuilder {
5252
match difficulty {
5353
Difficulty::Easy => self.filters.difficulty = String::from("EASY"),
5454
Difficulty::Medium => self.filters.difficulty = String::from("MEDIUM"),
@@ -57,7 +57,7 @@ impl TaskBuilder {
5757
self
5858
}
5959

60-
pub fn set_status(mut self, status: Status) -> TaskBuilder {
60+
pub fn set_status(mut self, status: Status) -> ProblemBuilder {
6161
match status {
6262
Status::Todo => self.filters.status = String::from("NOT_STARTED"),
6363
Status::Solved => self.filters.status = String::from("AC"),
@@ -66,17 +66,17 @@ impl TaskBuilder {
6666
self
6767
}
6868

69-
pub fn set_note_limit(mut self, limit: u32) -> TaskBuilder {
69+
pub fn set_note_limit(mut self, limit: u32) -> ProblemBuilder {
7070
self.limit = limit;
7171
self
7272
}
7373

74-
pub fn set_keyword(mut self, keyword: &str) -> TaskBuilder {
74+
pub fn set_keyword(mut self, keyword: &str) -> ProblemBuilder {
7575
self.key_word = String::from(keyword);
7676
self
7777
}
7878

79-
pub fn set_tags(mut self, tags: Vec<Tags>) -> TaskBuilder {
79+
pub fn set_tags(mut self, tags: Vec<Tags>) -> ProblemBuilder {
8080
let mut res_tags = Vec::new();
8181

8282
for tag in tags {
@@ -226,7 +226,7 @@ impl TaskBuilder {
226226

227227
let query = serde_json::to_string(&query)?;
228228

229-
let task_info = self
229+
let problem_info = self
230230
.client
231231
.get("https://leetcode.com/graphql/")
232232
.body(query)
@@ -235,6 +235,6 @@ impl TaskBuilder {
235235
.text()
236236
.await?;
237237

238-
Ok(serde_json::from_str::<ProblemData>(&task_info)?)
238+
Ok(serde_json::from_str::<ProblemData>(&problem_info)?)
239239
}
240240
}

0 commit comments

Comments
(0)

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