@@ -34,21 +34,45 @@ public class QuestionManager {
3434
3535 private static String dayQuestion = null ;
3636
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+ }
5053 }
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+ }
5172
73+ }
74+ questionOfToday ();
75+ sortQuestionList (questionList , new Sort (Constant .SORT_TYPE_ID , 1 ));
5276 if (questionList != null && !questionList .isEmpty ()) {
5377 String filePath = PersistentConfig .getInstance ().getTempFilePath () + Constant .DOC_PATH + ALLNAME ;
5478 FileUtils .saveFile (filePath , JSON .toJSONString (questionList ));
@@ -58,6 +82,28 @@ public static List<Question> getQuestionService(Project project, String url) {
5882
5983 }
6084
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+
61107 public static List <Question > getQuestionCache () {
62108 if (QUESTIONLIST != null ) {
63109 return QUESTIONLIST ;
@@ -177,15 +223,15 @@ public static List<Tag> getLists() {
177223 return tags ;
178224 }
179225
180- public static List <Tag > getCategory (String url ) {
226+ public static List <Tag > getCategory (String categorySlug ) {
181227 List <Tag > tags = new ArrayList <>();
182228
183229 HttpRequest httpRequest = HttpRequest .get (URLUtils .getLeetcodeCardInfo ());
184230 HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
185231 if (response != null && response .getStatusCode () == 200 ) {
186232 try {
187233 String body = response .getBody ();
188- tags = parseCategory (body , url );
234+ tags = parseCategory (body , categorySlug );
189235 } catch (Exception e1 ) {
190236 LogUtils .LOG .error ("Request CardInfo exception" , e1 );
191237 }
@@ -196,68 +242,60 @@ public static List<Tag> getCategory(String url) {
196242 }
197243
198244
199- private static List <Question > parseQuestion (String str ) {
245+ private static List <Question > parseQuestion (String str , Boolean isPremium ) {
200246
201247 List <Question > questionList = new ArrayList <Question >();
202248
203249 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" );
207252 for (int i = 0 ; i < jsonArray .size (); i ++) {
208253 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+ }
210258 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" ));
216262 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" );
219265 } else {
220- question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ));
266+ question .setStatus (object .get ("status" ) == null ? "" : object .getString ("status" ). toLowerCase () );
221267 }
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 " ));
224270 }
225271 } catch (Exception ee ) {
226272 question .setStatus ("" );
227273 }
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" ));
230276 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" )) {
236279 question .setArticleLive (Constant .ARTICLE_LIVE_ONE );
237- question .setArticleSlug (object .getJSONObject ( "stat" ). getString ("question__title_slug " ));
280+ question .setArticleSlug (object .getString ("titleSlug " ));
238281 question .setColumnArticles (1 );
282+ } else {
283+ question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
239284 }
240- } else {
285+ } else if ( object . containsKey ( "solutionNum" )) {
241286 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 );
245290 }
246291 } catch (Exception e ) {
247292 LogUtils .LOG .error ("Identify abnormal article" , e );
248293 question .setArticleLive (Constant .ARTICLE_LIVE_NONE );
249294 }
250295 questionList .add (question );
251296 }
252-
253- translation (questionList );
254-
255- questionOfToday ();
256- sortQuestionList (questionList ,new Sort (Constant .SORT_TYPE_ID ,1 ));
257297 }
258-
259298 return questionList ;
260-
261299 }
262300
263301 private static void translation (List <Question > questions ) {
@@ -347,7 +385,7 @@ private static List<Tag> parseTag(String str) {
347385 return tags ;
348386 }
349387
350- private static List <Tag > parseCategory (String str , String url ) {
388+ private static List <Tag > parseCategory (String str , String categorySlug ) {
351389 List <Tag > tags = new ArrayList <Tag >();
352390
353391 if (StringUtils .isNotBlank (str )) {
@@ -359,7 +397,7 @@ private static List<Tag> parseCategory(String str, String url) {
359397 tag .setSlug (object .getString ("slug" ));
360398 tag .setType (URLUtils .getLeetcodeUrl () + "/api" + object .getString ("url" ).replace ("problemset" , "problems" ));
361399 tag .setName (object .getString ("title" ));
362- if (url .contains (tag .getType ())) {
400+ if (categorySlug .contains (tag .getSlug ())) {
363401 tag .setSelect (true );
364402 }
365403 tags .add (tag );
@@ -398,11 +436,11 @@ public static void sortQuestionList(List<Question> list, Sort sort) {
398436 if (list == null || list .isEmpty () || sort .getType () == Constant .SORT_NONE ) {
399437 return ;
400438 }
401- Collections .sort (list ,new QuestionComparator (sort ));
439+ Collections .sort (list , new QuestionComparator (sort ));
402440
403441 }
404442
405- private static class QuestionComparator implements Comparator <Question >{
443+ private static class QuestionComparator implements Comparator <Question > {
406444
407445 private Sort sort ;
408446
@@ -413,9 +451,9 @@ public QuestionComparator(Sort sort) {
413451 @ Override
414452 public int compare (Question o1 , Question o2 ) {
415453 if (o1 .getFrontendQuestionId ().equals (dayQuestion )) {
416- return -1 ;
454+ return -1 ;
417455 } else if (o2 .getFrontendQuestionId ().equals (dayQuestion )) {
418- return 1 ;
456+ return 1 ;
419457 }
420458 int order = 0 ;
421459 if (Constant .SORT_TYPE_ID .equals (sort .getSlug ())) {
0 commit comments