@@ -32,67 +32,21 @@ 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 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
- }
35
+ public static List <Question > getQuestionService (Project project , String url ) {
36
+ List <Question > questionList = null ;
70
37
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 ());
71
48
}
72
49
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
-
96
50
if (questionList != null && !questionList .isEmpty ()) {
97
51
String filePath = PersistentConfig .getInstance ().getTempFilePath () + ALLNAME ;
98
52
FileUtils .saveFile (filePath , JSON .toJSONString (questionList ));
@@ -102,28 +56,6 @@ public int compare(Question arg0, Question arg1) {
102
56
103
57
}
104
58
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
-
127
59
public static List <Question > getQuestionCache () {
128
60
if (QUESTIONLIST != null ) {
129
61
return QUESTIONLIST ;
@@ -243,15 +175,15 @@ public static List<Tag> getLists() {
243
175
return tags ;
244
176
}
245
177
246
- public static List <Tag > getCategory (String categorySlug ) {
178
+ public static List <Tag > getCategory (String url ) {
247
179
List <Tag > tags = new ArrayList <>();
248
180
249
181
HttpRequest httpRequest = HttpRequest .get (URLUtils .getLeetcodeCardInfo ());
250
182
HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
251
183
if (response != null && response .getStatusCode () == 200 ) {
252
184
try {
253
185
String body = response .getBody ();
254
- tags = parseCategory (body , categorySlug );
186
+ tags = parseCategory (body , url );
255
187
} catch (Exception e1 ) {
256
188
LogUtils .LOG .error ("Request CardInfo exception" , e1 );
257
189
}
@@ -262,52 +194,75 @@ public static List<Tag> getCategory(String categorySlug) {
262
194
}
263
195
264
196
265
- private static List <Question > parseQuestion (String str , Boolean isPremium ) {
197
+ private static List <Question > parseQuestion (String str ) {
266
198
267
199
List <Question > questionList = new ArrayList <Question >();
268
200
269
201
if (StringUtils .isNotBlank (str )) {
270
- JSONObject jsonObject = JSONObject .parseObject (str ).getJSONObject ("data" ).getJSONObject ("problemsetQuestionList" );
271
- JSONArray jsonArray = jsonObject .getJSONArray ("questions" );
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" );
272
205
for (int i = 0 ; i < jsonArray .size (); i ++) {
273
206
JSONObject object = jsonArray .getJSONObject (i );
274
- Question question = new Question (object .getString ("title" ));
275
- if (URLUtils .isCn () && !PersistentConfig .getInstance ().getConfig ().getEnglishContent ()) {
276
- question .setTitle (object .getString ("titleCn" ));
277
- }
207
+ Question question = new Question (object .getJSONObject ("stat" ).getString ("question__title" ));
278
208
question .setLeaf (Boolean .TRUE );
279
- question .setQuestionId (object .getString ("frontendQuestionId " ));
280
- question .setFrontendQuestionId (object .getString ("frontendQuestionId " ));
209
+ question .setQuestionId (object .getJSONObject ( "stat" ). getString ("question_id " ));
210
+ question .setFrontendQuestionId (object .getJSONObject ( "stat" ). getString ("frontend_question_id " ));
281
211
try {
282
- if (object .getBoolean ("paidOnly " ) && ! isPremium ) {
283
- question .setStatus (" lock" );
212
+ if (object .getBoolean ("paid_only " ) && isPremium ) {
213
+ question .setStatus (object . getBoolean ( "paid_only" ) ? " lock" : null );
284
214
} else {
285
- question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ). toLowerCase () );
215
+ question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ));
286
216
}
287
217
} catch (Exception ee ) {
288
218
question .setStatus ("" );
289
219
}
290
- question .setTitleSlug (object .getString ("titleSlug " ));
291
- question .setLevel (object .getString ("difficulty" ));
220
+ question .setTitleSlug (object .getJSONObject ( "stat" ). getString ("question__title_slug " ));
221
+ question .setLevel (object .getJSONObject ("difficulty" ). getInteger ( "level " ));
292
222
try {
293
- if (object .containsKey ("hasSolution" )) {
294
- if (object .getBoolean ("hasSolution" )) {
295
- question .setArticleLive (Constant .ARTICLE_LIVE_ONE );
296
- question .setArticleSlug (object .getString ("titleSlug" ));
297
- } else {
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" )) {
298
226
question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
227
+ } else {
228
+ question .setArticleLive (Constant .ARTICLE_LIVE_ONE );
229
+ question .setArticleSlug (object .getJSONObject ("stat" ).getString ("question__title_slug" ));
299
230
}
300
- } else if (object .containsKey ("solutionNum" )) {
301
- question .setArticleLive (Constant .ARTICLE_LIVE_LIST );
302
231
} else {
303
- question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
232
+ question .setArticleLive (Constant .ARTICLE_LIVE_LIST );
304
233
}
305
234
} catch (Exception e ) {
306
235
LogUtils .LOG .error ("Identify abnormal article" , e );
307
236
question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
308
237
}
309
238
questionList .add (question );
310
239
}
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
+ });
311
266
}
312
267
return questionList ;
313
268
@@ -399,7 +354,7 @@ private static List<Tag> parseTag(String str) {
399
354
return tags ;
400
355
}
401
356
402
- private static List <Tag > parseCategory (String str , String categorySlug ) {
357
+ private static List <Tag > parseCategory (String str , String url ) {
403
358
List <Tag > tags = new ArrayList <Tag >();
404
359
405
360
if (StringUtils .isNotBlank (str )) {
@@ -411,7 +366,7 @@ private static List<Tag> parseCategory(String str, String categorySlug) {
411
366
tag .setSlug (object .getString ("slug" ));
412
367
tag .setType (URLUtils .getLeetcodeUrl () + "/api" + object .getString ("url" ).replace ("problemset" , "problems" ));
413
368
tag .setName (object .getString ("title" ));
414
- if (categorySlug .contains (tag .getSlug ())) {
369
+ if (url .contains (tag .getType ())) {
415
370
tag .setSelect (true );
416
371
}
417
372
tags .add (tag );
0 commit comments