@@ -32,21 +32,67 @@ public class QuestionManager {
32
32
private final static String TRANSLATIONNAME = "translation.json" ;
33
33
34
34
35
- public static List <Question > getQuestionService (Project project , String url ) {
36
- List <Question > questionList = null ;
35
+ public static List <Question > getQuestionService (Project project , String categorySlug ) {
36
+ Boolean isPremium = false ;
37
+ if (HttpRequestUtils .isLogin ()) {
38
+ HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (), "application/json" );
39
+ 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\" }" );
40
+ httpRequest .addHeader ("Accept" , "application/json" );
41
+ HttpResponse response = HttpRequestUtils .executePost (httpRequest );
42
+ if (response != null && response .getStatusCode () == 200 ) {
43
+ JSONObject user = JSONObject .parseObject (response .getBody ()).getJSONObject ("data" ).getJSONObject ("userStatus" );
44
+ isPremium = user .getBoolean ("isPremium" );
45
+ ApplicationManager .getApplication ().invokeAndWait (() -> {
46
+ WindowFactory .updateTitle (project , user .getString ("username" ));
47
+ });
48
+ } else {
49
+ LogUtils .LOG .error ("Request userStatus failed, status:" + response == null ? "" : response .getStatusCode ());
50
+ }
51
+ }
52
+ List <Question > questionList = new ArrayList <>();
53
+ int skip = 0 ;
54
+ int limit = 100 ;
55
+ int total = -1 ;
56
+ while (true ) {
57
+ try {
58
+ Map <String , Object > page = getQuestionPage (categorySlug , skip , limit , isPremium );
59
+ if (total < 0 ) {
60
+ total = (int ) page .get ("total" );
61
+ }
62
+ questionList .addAll ((List ) page .get ("questionList" ));
63
+ skip = skip + limit ;
64
+ if (total <= skip ){
65
+ break ;
66
+ }
67
+ } catch (Exception e ) {
68
+ return null ;
69
+ }
37
70
38
- HttpRequest httpRequest = HttpRequest .get (url );
39
- HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
40
- if (response != null && response .getStatusCode () == 200 ) {
41
- questionList = parseQuestion (response .getBody ());
42
- JSONObject jsonObject = JSONObject .parseObject (response .getBody ());
43
- ApplicationManager .getApplication ().invokeAndWait (() -> {
44
- WindowFactory .updateTitle (project , jsonObject .getString ("user_name" ));
45
- });
46
- } else {
47
- LogUtils .LOG .error ("Request question list failed, status:" + response == null ? "" : response .getStatusCode ());
48
71
}
49
72
73
+ String dayQuestion = questionOfToday ();
74
+ Collections .sort (questionList , new Comparator <Question >() {
75
+ @ Override
76
+ public int compare (Question arg0 , Question arg1 ) {
77
+ String frontendId0 = arg0 .getFrontendQuestionId ();
78
+ String frontendId1 = arg1 .getFrontendQuestionId ();
79
+ if (frontendId0 .equals (dayQuestion )) {
80
+ return -1 ;
81
+ } else if (frontendId1 .equals (dayQuestion )) {
82
+ return 1 ;
83
+ } else if (StringUtils .isNumeric (frontendId0 ) && StringUtils .isNumeric (frontendId1 )) {
84
+ return Integer .valueOf (frontendId0 ).compareTo (Integer .valueOf (frontendId1 ));
85
+ } else if (StringUtils .isNumeric (frontendId0 )) {
86
+ return -1 ;
87
+ } else if (StringUtils .isNumeric (frontendId1 )) {
88
+ return 1 ;
89
+ } else {
90
+ return frontendId0 .compareTo (frontendId1 );
91
+ }
92
+
93
+ }
94
+ });
95
+
50
96
if (questionList != null && !questionList .isEmpty ()) {
51
97
String filePath = PersistentConfig .getInstance ().getTempFilePath () + ALLNAME ;
52
98
FileUtils .saveFile (filePath , JSON .toJSONString (questionList ));
@@ -56,6 +102,28 @@ public static List<Question> getQuestionService(Project project, String url) {
56
102
57
103
}
58
104
105
+ private static Map <String , Object > getQuestionPage (String categorySlug , int skip , int limit , Boolean isPremium ) throws Exception {
106
+ HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (), "application/json" );
107
+ if (URLUtils .isCn ()) {
108
+ 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\" }" );
109
+ } else {
110
+ 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\" }" );
111
+ }
112
+ httpRequest .addHeader ("Accept" , "application/json" );
113
+ HttpResponse response = HttpRequestUtils .executePost (httpRequest );
114
+ if (response != null && response .getStatusCode () == 200 ) {
115
+ List questionList = parseQuestion (response .getBody (), isPremium );
116
+ Integer total = JSONObject .parseObject (response .getBody ()).getJSONObject ("data" ).getJSONObject ("problemsetQuestionList" ).getInteger ("total" );
117
+ Map <String , Object > page = new HashMap <>();
118
+ page .put ("total" , total );
119
+ page .put ("questionList" , questionList );
120
+ return page ;
121
+ } else {
122
+ LogUtils .LOG .error ("Request question list failed, status:" + response == null ? "" : response .getStatusCode ());
123
+ throw new RuntimeException ("Request question list failed" );
124
+ }
125
+ }
126
+
59
127
public static List <Question > getQuestionCache () {
60
128
if (QUESTIONLIST != null ) {
61
129
return QUESTIONLIST ;
@@ -175,15 +243,15 @@ public static List<Tag> getLists() {
175
243
return tags ;
176
244
}
177
245
178
- public static List <Tag > getCategory (String url ) {
246
+ public static List <Tag > getCategory (String categorySlug ) {
179
247
List <Tag > tags = new ArrayList <>();
180
248
181
249
HttpRequest httpRequest = HttpRequest .get (URLUtils .getLeetcodeCardInfo ());
182
250
HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
183
251
if (response != null && response .getStatusCode () == 200 ) {
184
252
try {
185
253
String body = response .getBody ();
186
- tags = parseCategory (body , url );
254
+ tags = parseCategory (body , categorySlug );
187
255
} catch (Exception e1 ) {
188
256
LogUtils .LOG .error ("Request CardInfo exception" , e1 );
189
257
}
@@ -194,75 +262,52 @@ public static List<Tag> getCategory(String url) {
194
262
}
195
263
196
264
197
- private static List <Question > parseQuestion (String str ) {
265
+ private static List <Question > parseQuestion (String str , Boolean isPremium ) {
198
266
199
267
List <Question > questionList = new ArrayList <Question >();
200
268
201
269
if (StringUtils .isNotBlank (str )) {
202
- JSONObject jsonObject = JSONObject .parseObject (str );
203
- Boolean isPremium = new Integer ("0" ).equals (jsonObject .getInteger ("frequency_high" )); //Premium users display frequency
204
- JSONArray jsonArray = jsonObject .getJSONArray ("stat_status_pairs" );
270
+ JSONObject jsonObject = JSONObject .parseObject (str ).getJSONObject ("data" ).getJSONObject ("problemsetQuestionList" );
271
+ JSONArray jsonArray = jsonObject .getJSONArray ("questions" );
205
272
for (int i = 0 ; i < jsonArray .size (); i ++) {
206
273
JSONObject object = jsonArray .getJSONObject (i );
207
- Question question = new Question (object .getJSONObject ("stat" ).getString ("question__title" ));
274
+ Question question = new Question (object .getString ("title" ));
275
+ if (URLUtils .isCn () && !PersistentConfig .getInstance ().getConfig ().getEnglishContent ()) {
276
+ question .setTitle (object .getString ("titleCn" ));
277
+ }
208
278
question .setLeaf (Boolean .TRUE );
209
- question .setQuestionId (object .getJSONObject ( "stat" ). getString ("question_id " ));
210
- question .setFrontendQuestionId (object .getJSONObject ( "stat" ). getString ("frontend_question_id " ));
279
+ question .setQuestionId (object .getString ("frontendQuestionId " ));
280
+ question .setFrontendQuestionId (object .getString ("frontendQuestionId " ));
211
281
try {
212
- if (object .getBoolean ("paid_only " ) && isPremium ) {
213
- question .setStatus (object . getBoolean ( "paid_only" ) ? " lock" : null );
282
+ if (object .getBoolean ("paidOnly " ) && ! isPremium ) {
283
+ question .setStatus (" lock" );
214
284
} else {
215
- question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ));
285
+ question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ). toLowerCase () );
216
286
}
217
287
} catch (Exception ee ) {
218
288
question .setStatus ("" );
219
289
}
220
- question .setTitleSlug (object .getJSONObject ( "stat" ). getString ("question__title_slug " ));
221
- question .setLevel (object .getJSONObject ("difficulty" ). getInteger ( "level " ));
290
+ question .setTitleSlug (object .getString ("titleSlug " ));
291
+ question .setLevel (object .getString ("difficulty" ));
222
292
try {
223
- if (object .getJSONObject ("stat" ).containsKey ("question__article__live" )) {
224
- if (object .getJSONObject ("stat" ).get ("question__article__live" ) == null
225
- || !object .getJSONObject ("stat" ).getBoolean ("question__article__live" )) {
226
- question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
227
- } else {
293
+ if (object .containsKey ("hasSolution" )) {
294
+ if (object .getBoolean ("hasSolution" )) {
228
295
question .setArticleLive (Constant .ARTICLE_LIVE_ONE );
229
- question .setArticleSlug (object .getJSONObject ("stat" ).getString ("question__title_slug" ));
296
+ question .setArticleSlug (object .getString ("titleSlug" ));
297
+ } else {
298
+ question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
230
299
}
231
- } else {
300
+ } else if ( object . containsKey ( "solutionNum" )) {
232
301
question .setArticleLive (Constant .ARTICLE_LIVE_LIST );
302
+ } else {
303
+ question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
233
304
}
234
305
} catch (Exception e ) {
235
306
LogUtils .LOG .error ("Identify abnormal article" , e );
236
307
question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
237
308
}
238
309
questionList .add (question );
239
310
}
240
-
241
- translation (questionList );
242
-
243
- String dayQuestion = questionOfToday ();
244
-
245
- Collections .sort (questionList , new Comparator <Question >() {
246
- @ Override
247
- public int compare (Question arg0 , Question arg1 ) {
248
- String frontendId0 = arg0 .getFrontendQuestionId ();
249
- String frontendId1 = arg1 .getFrontendQuestionId ();
250
- if (frontendId0 .equals (dayQuestion )) {
251
- return -1 ;
252
- } else if (frontendId1 .equals (dayQuestion )) {
253
- return 1 ;
254
- } else if (StringUtils .isNumeric (frontendId0 ) && StringUtils .isNumeric (frontendId1 )) {
255
- return Integer .valueOf (frontendId0 ).compareTo (Integer .valueOf (frontendId1 ));
256
- } else if (StringUtils .isNumeric (frontendId0 )) {
257
- return -1 ;
258
- } else if (StringUtils .isNumeric (frontendId1 )) {
259
- return 1 ;
260
- } else {
261
- return frontendId0 .compareTo (frontendId1 );
262
- }
263
-
264
- }
265
- });
266
311
}
267
312
return questionList ;
268
313
@@ -354,7 +399,7 @@ private static List<Tag> parseTag(String str) {
354
399
return tags ;
355
400
}
356
401
357
- private static List <Tag > parseCategory (String str , String url ) {
402
+ private static List <Tag > parseCategory (String str , String categorySlug ) {
358
403
List <Tag > tags = new ArrayList <Tag >();
359
404
360
405
if (StringUtils .isNotBlank (str )) {
@@ -366,7 +411,7 @@ private static List<Tag> parseCategory(String str, String url) {
366
411
tag .setSlug (object .getString ("slug" ));
367
412
tag .setType (URLUtils .getLeetcodeUrl () + "/api" + object .getString ("url" ).replace ("problemset" , "problems" ));
368
413
tag .setName (object .getString ("title" ));
369
- if (url .contains (tag .getType ())) {
414
+ if (categorySlug .contains (tag .getSlug ())) {
370
415
tag .setSelect (true );
371
416
}
372
417
tags .add (tag );
0 commit comments