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
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit 916e936

Browse files
committed
When json parsing fails, if the underlying request requires user authentication, use is_session_bad() to check if LEETCODE_SESSION is valid.
1 parent 9d1a95c commit 916e936

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

β€Žsrc/cache/mod.rsβ€Ž

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use self::sql::*;
99
use crate::{cfg, err::Error, plugins::LeetCode};
1010
use colored::Colorize;
1111
use diesel::prelude::*;
12+
use serde::de::DeserializeOwned;
1213
use serde_json::Value;
1314
use std::collections::HashMap;
15+
use reqwest::Response;
1416

1517
/// sqlite connection
1618
pub fn conn(p: String) -> SqliteConnection {
@@ -79,6 +81,13 @@ impl Cache {
7981
}
8082
}
8183

84+
async fn resp_to_json<T: DeserializeOwned>(&self, resp: Response) -> Result<T, Error> {
85+
let maybe_json: Result<T,_> = resp.json().await;
86+
if maybe_json.is_err() && self.is_session_bad().await {
87+
Err(Error::CookieError)
88+
} else { Ok(maybe_json?) }
89+
}
90+
8291
/// Download leetcode problems to db
8392
pub async fn download_problems(self) -> Result<Vec<Problem>, Error> {
8493
info!("Fetching leetcode problems...");
@@ -90,7 +99,7 @@ impl Cache {
9099
.clone()
91100
.get_category_problems(i)
92101
.await?
93-
.json()
102+
.json()// does not require LEETCODE_SESSION
94103
.await?;
95104
parser::problem(&mut ps, json).ok_or(Error::NoneError)?;
96105
}
@@ -121,7 +130,7 @@ impl Cache {
121130
.0
122131
.get_question_daily()
123132
.await?
124-
.json()
133+
.json()// does not require LEETCODE_SESSION
125134
.await?
126135
).ok_or(Error::NoneError)
127136
}
@@ -286,30 +295,20 @@ impl Cache {
286295

287296
/// TODO: The real delay
288297
async fn recur_verify(&self, rid: String) -> Result<VerifyResult, Error> {
289-
use serde_json::{from_str, Error as SJError};
290298
use std::time::Duration;
291299

292300
trace!("Run veriy recursion...");
293301
std::thread::sleep(Duration::from_micros(3000));
294302

295-
// debug resp raw text
296-
let debug_raw = self
303+
let json:VerifyResult = self.resp_to_json(
304+
self
297305
.clone()
298306
.0
299307
.verify_result(rid.clone())
300308
.await?
301-
.text()
302-
.await?;
303-
debug!("debug resp raw text: \n{:#?}", &debug_raw);
304-
if debug_raw.is_empty() {
305-
return Err(Error::CookieError);
306-
}
307-
308-
// debug json deserializing
309-
let debug_json: Result<VerifyResult, SJError> = from_str(&debug_raw);
310-
debug!("debug json deserializing: \n{:#?}", &debug_json);
309+
).await?;
311310

312-
Ok(debug_json?)
311+
Ok(json)
313312
}
314313

315314
/// Exec problem filter β€”β€” Test or Submit
@@ -328,7 +327,7 @@ impl Cache {
328327
.clone()
329328
.run_code(json.clone(), url.clone(), refer.clone())
330329
.await?
331-
.json()
330+
.json()// does not require LEETCODE_SESSION (very oddly)
332331
.await?;
333332
trace!("Run code result {:#?}", run_res);
334333

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /