Skip to content

Commit 5c8eae8

Browse files
committed
feat: Added setting problem by its own ID
1 parent 445863c commit 5c8eae8

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leetcoderustapi"
3-
version = "1.0.6"
3+
version = "1.0.7"
44
authors = ["1101-1", "https://github.com/1101-1/"]
55
edition = "2021"
66
license = "MIT"
@@ -24,5 +24,4 @@ reqwest = { version = "0.11.18", features = ["json", "multipart"] }
2424
serde = { version = "1.0.164", features = ["derive"] }
2525
serde_json = "1.0.99"
2626
thiserror = "1.0.41"
27-
tokio = { version = "1.29.1", features = ["time", "full"] }
28-
uuid = { version = "1.4.1", features = ["v4"] }
27+
tokio = { version = "1.29.1", features = ["time", "full"] }

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ async fn main() {
5353
// Fetch the full data for a specific problem by it's name
5454
let problem_info = api.set_problem("two sum").await.unwrap();
5555

56-
// Fetch the full data for a specific problem by it's ID(same as name)
57-
let problem_info = api.set_problem("1").await.unwrap();
56+
// Fetch the full data for a specific problem by it's ID
57+
let problem_info = api.set_problem_by_id(1).await.unwrap();
5858

5959
// Retrieve previous submissions to this problem
6060
let my_submissions = problem_info.my_submissions().await.unwrap();

src/lib.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl UserApi {
7171
// headers.insert("Sec-Fetch-Site", HeaderValue::from_static("same-origin"));
7272

7373
// let cookie = Self::get_csrf(username, password, headers.clone()).await?;
74-
74+
7575
// let valid_data = Self::valid_check(headers.clone(), &cookie).await?;
7676

7777
// let cookie = if valid_data.0 {
@@ -95,7 +95,7 @@ impl UserApi {
9595

9696
// Ok(Self { client })
9797
// }
98-
98+
9999
// Soon will be added if i find way to overcome captcha
100100

101101
// async fn get_csrf(
@@ -190,26 +190,39 @@ impl UserApi {
190190
.await?;
191191

192192
let resp_info = serde_json::from_str::<CookieData>(&cookie_info)?;
193-
193+
194194
// Soon will be added if i find way to overcome captcha
195195

196196
// let captcha_key = client.post(format!("https://www.recaptcha.net/recaptcha/enterprise/reload?k={}", resp_info.data.recaptchaKey)).headers(headers);
197197

198-
if resp_info
199-
.data
200-
.userStatus
201-
.isSignedIn
202-
{
198+
if resp_info.data.userStatus.isSignedIn {
203199
return Ok((true, String::from(token)));
204200
}
205201

206202
Ok((false, String::from(token)))
207203
}
208204

209-
pub async fn set_problem(&self, task: &str) -> Result<Problem, Errors> {
205+
pub async fn set_problem(&self, problem_name: &str) -> Result<Problem, Errors> {
206+
let info = Self::fetch_problem_full_data(
207+
&self,
208+
Self::get_question_name(&self, String::from(problem_name)).await?,
209+
)
210+
.await?;
211+
212+
Ok(Problem {
213+
client: self.client.clone(),
214+
task_search_name: info.0,
215+
full_data: info.1,
216+
})
217+
}
218+
219+
pub async fn set_problem_by_id(
220+
&self,
221+
problem_id: u32,
222+
) -> Result<Problem, Errors> {
210223
let info = Self::fetch_problem_full_data(
211224
&self,
212-
Self::get_question_name(&self, String::from(task)).await?,
225+
Self::get_question_name(&self, problem_id.to_string()).await?,
213226
)
214227
.await?;
215228

@@ -222,12 +235,12 @@ impl UserApi {
222235

223236
async fn fetch_problem_full_data(
224237
&self,
225-
task_name: String,
238+
problem: String,
226239
) -> Result<(String, ProblemFullData), Errors> {
227240
let json_obj = json!({
228241
"operationName": "questionData",
229242
"variables": {
230-
"titleSlug": task_name
243+
"titleSlug": problem
231244
},
232245
"query": "query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n canSeeQuestion\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n exampleTestcases\n categoryTitle\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n paidOnly\n hasVideoSolution\n paidOnlyVideo\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n enableTestMode\n enableDebugger\n envInfo\n libraryUrl\n adminUrl\n challengeQuestion {\n id\n date\n incompleteChallengeCount\n streakCount\n type\n __typename\n }\n __typename\n }\n}"
233246
});

0 commit comments

Comments
 (0)
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