@@ -9,8 +9,10 @@ use self::sql::*;
9
9
use crate :: { cfg, err:: Error , plugins:: LeetCode } ;
10
10
use colored:: Colorize ;
11
11
use diesel:: prelude:: * ;
12
+ use serde:: de:: DeserializeOwned ;
12
13
use serde_json:: Value ;
13
14
use std:: collections:: HashMap ;
15
+ use reqwest:: Response ;
14
16
15
17
/// sqlite connection
16
18
pub fn conn ( p : String ) -> SqliteConnection {
@@ -79,6 +81,13 @@ impl Cache {
79
81
}
80
82
}
81
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
+
82
91
/// Download leetcode problems to db
83
92
pub async fn download_problems ( self ) -> Result < Vec < Problem > , Error > {
84
93
info ! ( "Fetching leetcode problems..." ) ;
@@ -90,7 +99,7 @@ impl Cache {
90
99
. clone ( )
91
100
. get_category_problems ( i)
92
101
. await ?
93
- . json ( )
102
+ . json ( ) // does not require LEETCODE_SESSION
94
103
. await ?;
95
104
parser:: problem ( & mut ps, json) . ok_or ( Error :: NoneError ) ?;
96
105
}
@@ -121,7 +130,7 @@ impl Cache {
121
130
. 0
122
131
. get_question_daily ( )
123
132
. await ?
124
- . json ( )
133
+ . json ( ) // does not require LEETCODE_SESSION
125
134
. await ?
126
135
) . ok_or ( Error :: NoneError )
127
136
}
@@ -286,30 +295,20 @@ impl Cache {
286
295
287
296
/// TODO: The real delay
288
297
async fn recur_verify ( & self , rid : String ) -> Result < VerifyResult , Error > {
289
- use serde_json:: { from_str, Error as SJError } ;
290
298
use std:: time:: Duration ;
291
299
292
300
trace ! ( "Run veriy recursion..." ) ;
293
301
std:: thread:: sleep ( Duration :: from_micros ( 3000 ) ) ;
294
302
295
- // debug resp raw text
296
- let debug_raw = self
303
+ let json : VerifyResult = self . resp_to_json (
304
+ self
297
305
. clone ( )
298
306
. 0
299
307
. verify_result ( rid. clone ( ) )
300
308
. 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 ?;
311
310
312
- Ok ( debug_json? )
311
+ Ok ( json )
313
312
}
314
313
315
314
/// Exec problem filter ββ Test or Submit
@@ -328,7 +327,7 @@ impl Cache {
328
327
. clone ( )
329
328
. run_code ( json. clone ( ) , url. clone ( ) , refer. clone ( ) )
330
329
. await ?
331
- . json ( )
330
+ . json ( ) // does not require LEETCODE_SESSION (very oddly)
332
331
. await ?;
333
332
trace ! ( "Run code result {:#?}" , run_res) ;
334
333
0 commit comments