@@ -34,21 +34,45 @@ public class QuestionManager {
34
34
35
35
private static String dayQuestion = null ;
36
36
37
- public static List <Question > getQuestionService (Project project , String url ) {
38
- List <Question > questionList = null ;
39
-
40
- HttpRequest httpRequest = HttpRequest .get (url );
41
- HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
42
- if (response != null && response .getStatusCode () == 200 ) {
43
- questionList = parseQuestion (response .getBody ());
44
- JSONObject jsonObject = JSONObject .parseObject (response .getBody ());
45
- ApplicationManager .getApplication ().invokeAndWait (() -> {
46
- WindowFactory .updateTitle (project , jsonObject .getString ("user_name" ));
47
- });
48
- } else {
49
- LogUtils .LOG .error ("Request question list failed, status:" + response == null ? "" : response .getStatusCode ());
37
+ public static List <Question > getQuestionService (Project project , String categorySlug ) {
38
+ Boolean isPremium = false ;
39
+ if (HttpRequestUtils .isLogin ()) {
40
+ HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (), "application/json" );
41
+ httpRequest .setBody ("{\" query\" :\" \\ n query globalData {\\ n userStatus {\\ n isSignedIn\\ n isPremium\\ n username\\ n realName\\ n avatar\\ n userSlug\\ n isAdmin\\ n useTranslation\\ n premiumExpiredAt\\ n isTranslator\\ n isSuperuser\\ n isPhoneVerified\\ n }\\ n jobsMyCompany {\\ n nameSlug\\ n }\\ n commonNojPermissionTypes\\ n}\\ n \" ,\" variables\" :{},\" operationName\" :\" globalData\" }" );
42
+ httpRequest .addHeader ("Accept" , "application/json" );
43
+ HttpResponse response = HttpRequestUtils .executePost (httpRequest );
44
+ if (response != null && response .getStatusCode () == 200 ) {
45
+ JSONObject user = JSONObject .parseObject (response .getBody ()).getJSONObject ("data" ).getJSONObject ("userStatus" );
46
+ isPremium = user .getBoolean ("isPremium" );
47
+ ApplicationManager .getApplication ().invokeAndWait (() -> {
48
+ WindowFactory .updateTitle (project , user .getString ("username" ));
49
+ });
50
+ } else {
51
+ LogUtils .LOG .error ("Request userStatus failed, status:" + response == null ? "" : response .getStatusCode ());
52
+ }
50
53
}
54
+ List <Question > questionList = new ArrayList <>();
55
+ int skip = 0 ;
56
+ int limit = 100 ;
57
+ int total = -1 ;
58
+ while (true ) {
59
+ try {
60
+ Map <String , Object > page = getQuestionPage (categorySlug , skip , limit , isPremium );
61
+ if (total < 0 ) {
62
+ total = (int ) page .get ("total" );
63
+ }
64
+ questionList .addAll ((List ) page .get ("questionList" ));
65
+ skip = skip + limit ;
66
+ if (total <= skip ){
67
+ break ;
68
+ }
69
+ } catch (Exception e ) {
70
+ return null ;
71
+ }
51
72
73
+ }
74
+ questionOfToday ();
75
+ sortQuestionList (questionList , new Sort (Constant .SORT_TYPE_ID , 1 ));
52
76
if (questionList != null && !questionList .isEmpty ()) {
53
77
String filePath = PersistentConfig .getInstance ().getTempFilePath () + Constant .DOC_PATH + ALLNAME ;
54
78
FileUtils .saveFile (filePath , JSON .toJSONString (questionList ));
@@ -58,6 +82,28 @@ public static List<Question> getQuestionService(Project project, String url) {
58
82
59
83
}
60
84
85
+ private static Map <String , Object > getQuestionPage (String categorySlug , int skip , int limit , Boolean isPremium ) throws Exception {
86
+ HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (), "application/json" );
87
+ if (URLUtils .isCn ()) {
88
+ httpRequest .setBody ("{\" query\" :\" \\ n query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {\\ n problemsetQuestionList(\\ n categorySlug: $categorySlug\\ n limit: $limit\\ n skip: $skip\\ n filters: $filters\\ n ) {\\ n hasMore\\ n total\\ n questions {\\ n acRate\\ n difficulty\\ n freqBar\\ n frontendQuestionId\\ n isFavor\\ n paidOnly\\ n solutionNum\\ n status\\ n title\\ n titleCn\\ n titleSlug\\ n topicTags {\\ n name\\ n nameTranslated\\ n id\\ n slug\\ n }\\ n extra {\\ n hasVideoSolution\\ n topCompanyTags {\\ n imgUrl\\ n slug\\ n numSubscribed\\ n }\\ n }\\ n }\\ n }\\ n}\\ n \" ,\" variables\" :{\" categorySlug\" :\" " +categorySlug +"\" ,\" skip\" :" + skip + ",\" limit\" :" + limit + ",\" filters\" :{}},\" operationName\" :\" problemsetQuestionList\" }" );
89
+ } else {
90
+ httpRequest .setBody ("{\" query\" :\" \\ n query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {\\ n problemsetQuestionList: questionList(\\ n categorySlug: $categorySlug\\ n limit: $limit\\ n skip: $skip\\ n filters: $filters\\ n ) {\\ n total: totalNum\\ n questions: data {\\ n acRate\\ n difficulty\\ n freqBar\\ n frontendQuestionId: questionFrontendId\\ n isFavor\\ n paidOnly: isPaidOnly\\ n status\\ n title\\ n titleSlug\\ n topicTags {\\ n name\\ n id\\ n slug\\ n }\\ n hasSolution\\ n hasVideoSolution\\ n }\\ n }\\ n}\\ n \" ,\" variables\" :{\" categorySlug\" :\" " + categorySlug + "\" ,\" skip\" :" + skip + ",\" limit\" :" + limit + ",\" filters\" :{}},\" operationName\" :\" problemsetQuestionList\" }" );
91
+ }
92
+ httpRequest .addHeader ("Accept" , "application/json" );
93
+ HttpResponse response = HttpRequestUtils .executePost (httpRequest );
94
+ if (response != null && response .getStatusCode () == 200 ) {
95
+ List questionList = parseQuestion (response .getBody (), isPremium );
96
+ Integer total = JSONObject .parseObject (response .getBody ()).getJSONObject ("data" ).getJSONObject ("problemsetQuestionList" ).getInteger ("total" );
97
+ Map <String , Object > page = new HashMap <>();
98
+ page .put ("total" , total );
99
+ page .put ("questionList" , questionList );
100
+ return page ;
101
+ } else {
102
+ LogUtils .LOG .error ("Request question list failed, status:" + response == null ? "" : response .getStatusCode ());
103
+ throw new RuntimeException ("Request question list failed" );
104
+ }
105
+ }
106
+
61
107
public static List <Question > getQuestionCache () {
62
108
if (QUESTIONLIST != null ) {
63
109
return QUESTIONLIST ;
@@ -177,15 +223,15 @@ public static List<Tag> getLists() {
177
223
return tags ;
178
224
}
179
225
180
- public static List <Tag > getCategory (String url ) {
226
+ public static List <Tag > getCategory (String categorySlug ) {
181
227
List <Tag > tags = new ArrayList <>();
182
228
183
229
HttpRequest httpRequest = HttpRequest .get (URLUtils .getLeetcodeCardInfo ());
184
230
HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
185
231
if (response != null && response .getStatusCode () == 200 ) {
186
232
try {
187
233
String body = response .getBody ();
188
- tags = parseCategory (body , url );
234
+ tags = parseCategory (body , categorySlug );
189
235
} catch (Exception e1 ) {
190
236
LogUtils .LOG .error ("Request CardInfo exception" , e1 );
191
237
}
@@ -196,68 +242,60 @@ public static List<Tag> getCategory(String url) {
196
242
}
197
243
198
244
199
- private static List <Question > parseQuestion (String str ) {
245
+ private static List <Question > parseQuestion (String str , Boolean isPremium ) {
200
246
201
247
List <Question > questionList = new ArrayList <Question >();
202
248
203
249
if (StringUtils .isNotBlank (str )) {
204
- JSONObject jsonObject = JSONObject .parseObject (str );
205
- Boolean isPremium = new Integer ("0" ).equals (jsonObject .getInteger ("frequency_high" )); //Premium users display frequency
206
- JSONArray jsonArray = jsonObject .getJSONArray ("stat_status_pairs" );
250
+ JSONObject jsonObject = JSONObject .parseObject (str ).getJSONObject ("data" ).getJSONObject ("problemsetQuestionList" );
251
+ JSONArray jsonArray = jsonObject .getJSONArray ("questions" );
207
252
for (int i = 0 ; i < jsonArray .size (); i ++) {
208
253
JSONObject object = jsonArray .getJSONObject (i );
209
- Question question = new Question (object .getJSONObject ("stat" ).getString ("question__title" ));
254
+ Question question = new Question (object .getString ("title" ));
255
+ if (URLUtils .isCn () && !PersistentConfig .getInstance ().getConfig ().getEnglishContent ()) {
256
+ question .setTitle (object .getString ("titleCn" ));
257
+ }
210
258
question .setLeaf (Boolean .TRUE );
211
- question .setQuestionId (object .getJSONObject ("stat" ).getString ("question_id" ));
212
- question .setFrontendQuestionId (object .getJSONObject ("stat" ).getString ("frontend_question_id" ));
213
- question .setAcs (object .getJSONObject ("stat" ).getInteger ("total_acs" ));
214
- question .setSubmitted (object .getJSONObject ("stat" ).getInteger ("total_submitted" ));
215
- question .setAcceptance ();
259
+ question .setQuestionId (object .getString ("frontendQuestionId" ));
260
+ question .setFrontendQuestionId (object .getString ("frontendQuestionId" ));
261
+ question .setAcceptance (object .getDouble ("acRate" ));
216
262
try {
217
- if (object .getBoolean ("paid_only " ) && isPremium ) {
218
- question .setStatus (object . getBoolean ( "paid_only" ) ? " lock" : null );
263
+ if (object .getBoolean ("paidOnly " ) && ! isPremium ) {
264
+ question .setStatus (" lock" );
219
265
} else {
220
- question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ));
266
+ question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ). toLowerCase () );
221
267
}
222
- if (object .containsKey ("frequency" ) ) {
223
- question .setFrequency (object .getDouble ("frequency " ));
268
+ if (object .containsKey ("freqBar" ) && object . get ( "freqBar" ) != null ) {
269
+ question .setFrequency (object .getDouble ("freqBar " ));
224
270
}
225
271
} catch (Exception ee ) {
226
272
question .setStatus ("" );
227
273
}
228
- question .setTitleSlug (object .getJSONObject ( "stat" ). getString ("question__title_slug " ));
229
- question .setLevel (object .getJSONObject ("difficulty" ). getInteger ( "level " ));
274
+ question .setTitleSlug (object .getString ("titleSlug " ));
275
+ question .setLevel (object .getString ("difficulty" ));
230
276
try {
231
- if (object .getJSONObject ("stat" ).containsKey ("question__article__live" )) {
232
- if (object .getJSONObject ("stat" ).get ("question__article__live" ) == null
233
- || !object .getJSONObject ("stat" ).getBoolean ("question__article__live" )) {
234
- question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
235
- } else {
277
+ if (object .containsKey ("hasSolution" )) {
278
+ if (object .getBoolean ("hasSolution" )) {
236
279
question .setArticleLive (Constant .ARTICLE_LIVE_ONE );
237
- question .setArticleSlug (object .getJSONObject ( "stat" ). getString ("question__title_slug " ));
280
+ question .setArticleSlug (object .getString ("titleSlug " ));
238
281
question .setColumnArticles (1 );
282
+ } else {
283
+ question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
239
284
}
240
- } else {
285
+ } else if ( object . containsKey ( "solutionNum" )) {
241
286
question .setArticleLive (Constant .ARTICLE_LIVE_LIST );
242
- if (object .getJSONObject ( "stat" ). containsKey ( "total_column_articles" )) {
243
- question . setColumnArticles ( object . getJSONObject ( "stat" ). getInteger ( "total_column_articles" ));
244
- }
287
+ question . setColumnArticles (object .getInteger ( "solutionNum" ));
288
+ } else {
289
+ question . setArticleLive ( Constant . ARTICLE_LIVE_NONE );
245
290
}
246
291
} catch (Exception e ) {
247
292
LogUtils .LOG .error ("Identify abnormal article" , e );
248
293
question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
249
294
}
250
295
questionList .add (question );
251
296
}
252
-
253
- translation (questionList );
254
-
255
- questionOfToday ();
256
- sortQuestionList (questionList ,new Sort (Constant .SORT_TYPE_ID ,1 ));
257
297
}
258
-
259
298
return questionList ;
260
-
261
299
}
262
300
263
301
private static void translation (List <Question > questions ) {
@@ -347,7 +385,7 @@ private static List<Tag> parseTag(String str) {
347
385
return tags ;
348
386
}
349
387
350
- private static List <Tag > parseCategory (String str , String url ) {
388
+ private static List <Tag > parseCategory (String str , String categorySlug ) {
351
389
List <Tag > tags = new ArrayList <Tag >();
352
390
353
391
if (StringUtils .isNotBlank (str )) {
@@ -359,7 +397,7 @@ private static List<Tag> parseCategory(String str, String url) {
359
397
tag .setSlug (object .getString ("slug" ));
360
398
tag .setType (URLUtils .getLeetcodeUrl () + "/api" + object .getString ("url" ).replace ("problemset" , "problems" ));
361
399
tag .setName (object .getString ("title" ));
362
- if (url .contains (tag .getType ())) {
400
+ if (categorySlug .contains (tag .getSlug ())) {
363
401
tag .setSelect (true );
364
402
}
365
403
tags .add (tag );
@@ -398,11 +436,11 @@ public static void sortQuestionList(List<Question> list, Sort sort) {
398
436
if (list == null || list .isEmpty () || sort .getType () == Constant .SORT_NONE ) {
399
437
return ;
400
438
}
401
- Collections .sort (list ,new QuestionComparator (sort ));
439
+ Collections .sort (list , new QuestionComparator (sort ));
402
440
403
441
}
404
442
405
- private static class QuestionComparator implements Comparator <Question >{
443
+ private static class QuestionComparator implements Comparator <Question > {
406
444
407
445
private Sort sort ;
408
446
@@ -413,9 +451,9 @@ public QuestionComparator(Sort sort) {
413
451
@ Override
414
452
public int compare (Question o1 , Question o2 ) {
415
453
if (o1 .getFrontendQuestionId ().equals (dayQuestion )) {
416
- return -1 ;
454
+ return -1 ;
417
455
} else if (o2 .getFrontendQuestionId ().equals (dayQuestion )) {
418
- return 1 ;
456
+ return 1 ;
419
457
}
420
458
int order = 0 ;
421
459
if (Constant .SORT_TYPE_ID .equals (sort .getSlug ())) {
0 commit comments