Skip to content

Commit 3c4ae29

Browse files
committed
change api
1 parent b479518 commit 3c4ae29

File tree

6 files changed

+140
-72
lines changed

6 files changed

+140
-72
lines changed

src/main/java/com/shuzijun/leetcode/plugin/actions/toolbar/FindTagAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void setSelected(AnActionEvent anActionEvent, boolean b) {
5151
@Override
5252
public void run(@NotNull ProgressIndicator progressIndicator) {
5353
if (b) {
54-
ViewManager.loadServiceData(tree, anActionEvent.getProject(), tag.getType());
54+
ViewManager.loadServiceData(tree, anActionEvent.getProject(), tag.getSlug());
5555
} else {
5656
ViewManager.loadServiceData(tree, anActionEvent.getProject());
5757
}

src/main/java/com/shuzijun/leetcode/plugin/manager/QuestionManager.java

Lines changed: 106 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,67 @@ public class QuestionManager {
3232
private final static String TRANSLATIONNAME = "translation.json";
3333

3434

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+
}
3770

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());
4871
}
4972

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+
5096
if (questionList != null && !questionList.isEmpty()) {
5197
String filePath = PersistentConfig.getInstance().getTempFilePath() + ALLNAME;
5298
FileUtils.saveFile(filePath, JSON.toJSONString(questionList));
@@ -56,6 +102,28 @@ public static List<Question> getQuestionService(Project project, String url) {
56102

57103
}
58104

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+
59127
public static List<Question> getQuestionCache() {
60128
if (QUESTIONLIST != null) {
61129
return QUESTIONLIST;
@@ -175,15 +243,15 @@ public static List<Tag> getLists() {
175243
return tags;
176244
}
177245

178-
public static List<Tag> getCategory(String url) {
246+
public static List<Tag> getCategory(String categorySlug) {
179247
List<Tag> tags = new ArrayList<>();
180248

181249
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeCardInfo());
182250
HttpResponse response = HttpRequestUtils.executeGet(httpRequest);
183251
if (response != null && response.getStatusCode() == 200) {
184252
try {
185253
String body = response.getBody();
186-
tags = parseCategory(body, url);
254+
tags = parseCategory(body, categorySlug);
187255
} catch (Exception e1) {
188256
LogUtils.LOG.error("Request CardInfo exception", e1);
189257
}
@@ -194,75 +262,52 @@ public static List<Tag> getCategory(String url) {
194262
}
195263

196264

197-
private static List<Question> parseQuestion(String str) {
265+
private static List<Question> parseQuestion(String str, Boolean isPremium) {
198266

199267
List<Question> questionList = new ArrayList<Question>();
200268

201269
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");
205272
for (int i = 0; i < jsonArray.size(); i++) {
206273
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+
}
208278
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"));
211281
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");
214284
} else {
215-
question.setStatus(object.get("status") == null ? "" : object.getString("status"));
285+
question.setStatus(object.get("status") == null ? "" : object.getString("status").toLowerCase());
216286
}
217287
} catch (Exception ee) {
218288
question.setStatus("");
219289
}
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"));
222292
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")) {
228295
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);
230299
}
231-
} else {
300+
} else if (object.containsKey("solutionNum")) {
232301
question.setArticleLive(Constant.ARTICLE_LIVE_LIST);
302+
} else {
303+
question.setArticleLive(Constant.ARTICLE_LIVE_NONE);
233304
}
234305
} catch (Exception e) {
235306
LogUtils.LOG.error("Identify abnormal article", e);
236307
question.setArticleLive(Constant.ARTICLE_LIVE_NONE);
237308
}
238309
questionList.add(question);
239310
}
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-
});
266311
}
267312
return questionList;
268313

@@ -354,7 +399,7 @@ private static List<Tag> parseTag(String str) {
354399
return tags;
355400
}
356401

357-
private static List<Tag> parseCategory(String str, String url) {
402+
private static List<Tag> parseCategory(String str, String categorySlug) {
358403
List<Tag> tags = new ArrayList<Tag>();
359404

360405
if (StringUtils.isNotBlank(str)) {
@@ -366,7 +411,7 @@ private static List<Tag> parseCategory(String str, String url) {
366411
tag.setSlug(object.getString("slug"));
367412
tag.setType(URLUtils.getLeetcodeUrl() + "/api" + object.getString("url").replace("problemset", "problems"));
368413
tag.setName(object.getString("title"));
369-
if (url.contains(tag.getType())) {
414+
if (categorySlug.contains(tag.getSlug())) {
370415
tag.setSelect(true);
371416
}
372417
tags.add(tag);

src/main/java/com/shuzijun/leetcode/plugin/manager/ViewManager.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.shuzijun.leetcode.plugin.model.Tag;
1212
import com.shuzijun.leetcode.plugin.utils.MessageUtils;
1313
import com.shuzijun.leetcode.plugin.utils.PropertiesUtils;
14-
import com.shuzijun.leetcode.plugin.utils.URLUtils;
1514
import com.shuzijun.leetcode.plugin.window.WindowFactory;
1615

1716
import javax.swing.*;
@@ -36,11 +35,11 @@ public class ViewManager {
3635
private static boolean intersection = Boolean.FALSE;
3736

3837
public static void loadServiceData(JTree tree, Project project) {
39-
loadServiceData(tree, project, URLUtils.getLeetcodeAll());
38+
loadServiceData(tree, project, "");
4039
}
4140

42-
public static void loadServiceData(JTree tree, Project project, String url) {
43-
List<Question> questionList = QuestionManager.getQuestionService(project, url);
41+
public static void loadServiceData(JTree tree, Project project, String categorySlug) {
42+
List<Question> questionList = QuestionManager.getQuestionService(project, categorySlug);
4443
if (questionList == null || questionList.isEmpty()) {
4544
MessageUtils.getInstance(project).showWarnMsg("warning", PropertiesUtils.getInfo("response.cache"));
4645
questionList = QuestionManager.getQuestionCache();
@@ -61,7 +60,7 @@ public String apply(Question question) {
6160
filter.put(Constant.FIND_TYPE_STATUS, QuestionManager.getStatus());
6261
filter.put(Constant.FIND_TYPE_LISTS, QuestionManager.getLists());
6362
filter.put(Constant.FIND_TYPE_TAGS, QuestionManager.getTags());
64-
filter.put(Constant.FIND_TYPE_CATEGORY, QuestionManager.getCategory(url));
63+
filter.put(Constant.FIND_TYPE_CATEGORY, QuestionManager.getCategory(categorySlug));
6564

6665

6766
DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();

src/main/java/com/shuzijun/leetcode/plugin/model/Question.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ public void setLevel(Integer level) {
104104
this.level = level;
105105
}
106106

107+
public void setLevel(String difficulty) {
108+
if(difficulty == null){
109+
this.level = 0;
110+
}else if("easy".equalsIgnoreCase(difficulty)){
111+
this.level = 1;
112+
}else if("medium".equalsIgnoreCase(difficulty)){
113+
this.level = 2;
114+
}else if("hard".equalsIgnoreCase(difficulty)){
115+
this.level = 3;
116+
}else {
117+
this.level = 0;
118+
}
119+
}
120+
107121
public String getStatus() {
108122
return status;
109123
}
@@ -197,11 +211,11 @@ public String toString() {
197211
StringBuffer sb = new StringBuffer();
198212

199213

200-
if ("notac".equals(status)) {
214+
if ("notac".equalsIgnoreCase(status)) {
201215
sb.append("❓");
202-
} else if ("ac".equals(status)) {
216+
} else if ("ac".equalsIgnoreCase(status)) {
203217
sb.append("✔");
204-
} else if ("lock".equals(status)) {
218+
} else if ("lock".equalsIgnoreCase(status)) {
205219
sb.append(" $ ");
206220
} else if (leaf && level != null) {
207221
sb.append(" ");

src/main/java/com/shuzijun/leetcode/plugin/model/Tag.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.shuzijun.leetcode.plugin.model;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
35
import java.util.Comparator;
46
import java.util.TreeSet;
57

@@ -15,7 +17,15 @@ public class Tag {
1517
private TreeSet<String> questions = new TreeSet<String>(new Comparator<String>() {
1618
@Override
1719
public int compare(String arg0, String arg1) {
18-
return Integer.valueOf(arg0).compareTo(Integer.valueOf(arg1));
20+
if (StringUtils.isNumeric(arg0) && StringUtils.isNumeric(arg1)) {
21+
return Integer.valueOf(arg0).compareTo(Integer.valueOf(arg1));
22+
} else if (StringUtils.isNumeric(arg0)) {
23+
return -1;
24+
} else if (StringUtils.isNumeric(arg1)) {
25+
return 1;
26+
} else {
27+
return arg0.compareTo(arg1);
28+
}
1929
}
2030
});
2131

src/main/java/com/shuzijun/leetcode/plugin/window/HttpLogin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static boolean ajaxLogin(Config config, JTree tree, Project project) {
3838

3939
try {
4040
HttpEntity ent = MultipartEntityBuilder.create()
41-
.addTextBody("csrfmiddlewaretoken", HttpRequestUtils.getToken())
41+
.addTextBody("csrfmiddlewaretoken", HttpRequestUtils.getToken() == null ? "": HttpRequestUtils.getToken())
4242
.addTextBody("login", config.getLoginName())
4343
.addTextBody("password", PersistentConfig.getInstance().getPassword())
4444
.addTextBody("next", "/problems")

0 commit comments

Comments
 (0)