@@ -9,8 +9,10 @@ use self::sql::*;
99use crate :: { cfg, err:: Error , plugins:: LeetCode } ;
1010use colored:: Colorize ;
1111use diesel:: prelude:: * ;
12+ use serde:: de:: DeserializeOwned ;
1213use serde_json:: Value ;
1314use std:: collections:: HashMap ;
15+ use reqwest:: Response ;
1416
1517/// sqlite connection
1618pub fn conn ( p : String ) -> SqliteConnection {
@@ -58,6 +60,34 @@ impl Cache {
5860 Ok ( ( ) )
5961 }
6062
63+ async fn get_user_info ( & self ) -> Result < ( String , bool ) , Error > {
64+ let user = parser:: user (
65+ self . clone ( ) . 0
66+ . get_user_info ( ) . await ?
67+ . json ( ) . await ?
68+ ) ;
69+ match user {
70+ None => Err ( Error :: NoneError ) ,
71+ Some ( None ) => Err ( Error :: CookieError ) ,
72+ Some ( Some ( ( s, b) ) ) => Ok ( ( s, b) )
73+ }
74+ }
75+
76+ async fn is_session_bad ( & self ) -> bool {
77+ // i.e. self.get_user_info().contains_err(Error::CookieError)
78+ match self . get_user_info ( ) . await {
79+ Err ( Error :: CookieError ) => true ,
80+ _ => false
81+ }
82+ }
83+
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+
6191 /// Download leetcode problems to db
6292 pub async fn download_problems ( self ) -> Result < Vec < Problem > , Error > {
6393 info ! ( "Fetching leetcode problems..." ) ;
@@ -69,7 +99,7 @@ impl Cache {
6999 . clone ( )
70100 . get_category_problems ( i)
71101 . await ?
72- . json ( )
102+ . json ( ) // does not require LEETCODE_SESSION
73103 . await ?;
74104 parser:: problem ( & mut ps, json) . ok_or ( Error :: NoneError ) ?;
75105 }
@@ -100,7 +130,7 @@ impl Cache {
100130 . 0
101131 . get_question_daily ( )
102132 . await ?
103- . json ( )
133+ . json ( ) // does not require LEETCODE_SESSION
104134 . await ?
105135 ) . ok_or ( Error :: NoneError )
106136 }
@@ -265,30 +295,20 @@ impl Cache {
265295
266296 /// TODO: The real delay
267297 async fn recur_verify ( & self , rid : String ) -> Result < VerifyResult , Error > {
268- use serde_json:: { from_str, Error as SJError } ;
269298 use std:: time:: Duration ;
270299
271300 trace ! ( "Run veriy recursion..." ) ;
272301 std:: thread:: sleep ( Duration :: from_micros ( 3000 ) ) ;
273302
274- // debug resp raw text
275- let debug_raw = self
303+ let json : VerifyResult = self . resp_to_json (
304+ self
276305 . clone ( )
277306 . 0
278307 . verify_result ( rid. clone ( ) )
279308 . await ?
280- . text ( )
281- . await ?;
282- debug ! ( "debug resp raw text: \n {:#?}" , & debug_raw) ;
283- if debug_raw. is_empty ( ) {
284- return Err ( Error :: CookieError ) ;
285- }
286-
287- // debug json deserializing
288- let debug_json: Result < VerifyResult , SJError > = from_str ( & debug_raw) ;
289- debug ! ( "debug json deserializing: \n {:#?}" , & debug_json) ;
309+ ) . await ?;
290310
291- Ok ( debug_json? )
311+ Ok ( json )
292312 }
293313
294314 /// Exec problem filter —— Test or Submit
@@ -307,10 +327,16 @@ impl Cache {
307327 . clone ( )
308328 . run_code ( json. clone ( ) , url. clone ( ) , refer. clone ( ) )
309329 . await ?
310- . json ( )
330+ . json ( ) // does not require LEETCODE_SESSION (very oddly)
311331 . await ?;
312332 trace ! ( "Run code result {:#?}" , run_res) ;
313333
334+ // Check if leetcode accepted the Run request
335+ if match run {
336+ Run :: Test => run_res. interpret_id . is_empty ( ) ,
337+ Run :: Submit => run_res. submission_id == 0
338+ } { return Err ( Error :: CookieError ) }
339+
314340 let mut res: VerifyResult = VerifyResult :: default ( ) ;
315341 while res. state != "SUCCESS" {
316342 res = match run {
0 commit comments