@@ -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 {
@@ -58,6 +60,34 @@ impl Cache {
58
60
Ok ( ( ) )
59
61
}
60
62
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
+
61
91
/// Download leetcode problems to db
62
92
pub async fn download_problems ( self ) -> Result < Vec < Problem > , Error > {
63
93
info ! ( "Fetching leetcode problems..." ) ;
@@ -69,7 +99,7 @@ impl Cache {
69
99
. clone ( )
70
100
. get_category_problems ( i)
71
101
. await ?
72
- . json ( )
102
+ . json ( ) // does not require LEETCODE_SESSION
73
103
. await ?;
74
104
parser:: problem ( & mut ps, json) . ok_or ( Error :: NoneError ) ?;
75
105
}
@@ -100,7 +130,7 @@ impl Cache {
100
130
. 0
101
131
. get_question_daily ( )
102
132
. await ?
103
- . json ( )
133
+ . json ( ) // does not require LEETCODE_SESSION
104
134
. await ?
105
135
) . ok_or ( Error :: NoneError )
106
136
}
@@ -265,30 +295,20 @@ impl Cache {
265
295
266
296
/// TODO: The real delay
267
297
async fn recur_verify ( & self , rid : String ) -> Result < VerifyResult , Error > {
268
- use serde_json:: { from_str, Error as SJError } ;
269
298
use std:: time:: Duration ;
270
299
271
300
trace ! ( "Run veriy recursion..." ) ;
272
301
std:: thread:: sleep ( Duration :: from_micros ( 3000 ) ) ;
273
302
274
- // debug resp raw text
275
- let debug_raw = self
303
+ let json : VerifyResult = self . resp_to_json (
304
+ self
276
305
. clone ( )
277
306
. 0
278
307
. verify_result ( rid. clone ( ) )
279
308
. 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 ?;
290
310
291
- Ok ( debug_json? )
311
+ Ok ( json )
292
312
}
293
313
294
314
/// Exec problem filter —— Test or Submit
@@ -307,10 +327,16 @@ impl Cache {
307
327
. clone ( )
308
328
. run_code ( json. clone ( ) , url. clone ( ) , refer. clone ( ) )
309
329
. await ?
310
- . json ( )
330
+ . json ( ) // does not require LEETCODE_SESSION (very oddly)
311
331
. await ?;
312
332
trace ! ( "Run code result {:#?}" , run_res) ;
313
333
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
+
314
340
let mut res: VerifyResult = VerifyResult :: default ( ) ;
315
341
while res. state != "SUCCESS" {
316
342
res = match run {
0 commit comments