Skip to content

Commit 19d5a73

Browse files
committed
change api;fix bug
1 parent 66088e9 commit 19d5a73

File tree

10 files changed

+166
-97
lines changed

10 files changed

+166
-97
lines changed

src/main/java/com/shuzijun/leetcode/plugin/actions/AbstractAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import com.intellij.openapi.progress.ProgressManager;
88
import com.intellij.openapi.progress.Task;
99
import com.shuzijun.leetcode.plugin.model.Config;
10+
import com.shuzijun.leetcode.plugin.model.PluginConstant;
1011
import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
11-
import com.shuzijun.leetcode.plugin.setting.SettingConfigurable;
1212
import com.shuzijun.leetcode.plugin.utils.MTAUtils;
1313
import com.shuzijun.leetcode.plugin.utils.MessageUtils;
1414
import com.shuzijun.leetcode.plugin.utils.PropertiesUtils;
@@ -25,7 +25,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
2525
Config config = PersistentConfig.getInstance().getInitConfig();
2626
if (config == null) {
2727
MessageUtils.getInstance(anActionEvent.getProject()).showWarnMsg("warning", PropertiesUtils.getInfo("config.first"));
28-
ShowSettingsUtil.getInstance().showSettingsDialog(anActionEvent.getProject(), SettingConfigurable.DISPLAY_NAME);
28+
ShowSettingsUtil.getInstance().showSettingsDialog(anActionEvent.getProject(), PluginConstant.APPLICATION_CONFIGURABLE_DISPLAY_NAME);
2929
return;
3030
} else if (StringUtils.isBlank(config.getId())) {
3131
config.setId(MTAUtils.getI(""));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import com.intellij.openapi.actionSystem.AnAction;
44
import com.intellij.openapi.actionSystem.AnActionEvent;
55
import com.intellij.openapi.options.ShowSettingsUtil;
6-
import com.shuzijun.leetcode.plugin.setting.SettingConfigurable;
6+
import com.shuzijun.leetcode.plugin.model.PluginConstant;
77

88
/**
99
* @author shuzijun
1010
*/
1111
public class ConfigAction extends AnAction {
1212
@Override
1313
public void actionPerformed(AnActionEvent anActionEvent) {
14-
ShowSettingsUtil.getInstance().showSettingsDialog(anActionEvent.getProject(),SettingConfigurable.DISPLAY_NAME);
14+
ShowSettingsUtil.getInstance().showSettingsDialog(anActionEvent.getProject(), PluginConstant.APPLICATION_CONFIGURABLE_DISPLAY_NAME);
1515
}
1616
}

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
@@ -52,7 +52,7 @@ public void setSelected(AnActionEvent anActionEvent, boolean b) {
5252
@Override
5353
public void run(@NotNull ProgressIndicator progressIndicator) {
5454
if (b) {
55-
ViewManager.loadServiceData(tree, anActionEvent.getProject(), tag.getType());
55+
ViewManager.loadServiceData(tree, anActionEvent.getProject(), tag.getSlug());
5656
} else {
5757
ViewManager.loadServiceData(tree, anActionEvent.getProject());
5858
}

src/main/java/com/shuzijun/leetcode/plugin/editor/QuestionEditorProvider.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.shuzijun.leetcode.plugin.model.LeetcodeEditor;
1111
import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
1212
import com.shuzijun.leetcode.plugin.setting.ProjectConfig;
13+
import com.shuzijun.leetcode.plugin.utils.LogUtils;
1314
import org.apache.commons.lang3.StringUtils;
1415
import org.jetbrains.annotations.NotNull;
1516

@@ -26,21 +27,26 @@ public QuestionEditorProvider() {
2627

2728
@Override
2829
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
29-
Config config = PersistentConfig.getInstance().getInitConfig();
30-
if(config == null || !config.getQuestionEditor()){
31-
return false;
32-
}
33-
LeetcodeEditor leetcodeEditor = ProjectConfig.getInstance(project).getEditor(file.getPath());
34-
if(leetcodeEditor == null || StringUtils.isBlank(leetcodeEditor.getContentPath())){
35-
return false;
36-
}
37-
VirtualFile contentVf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(leetcodeEditor.getContentPath()));
38-
if(contentVf == null){
30+
try {
31+
Config config = PersistentConfig.getInstance().getInitConfig();
32+
if (config == null || !config.getQuestionEditor()) {
33+
return false;
34+
}
35+
LeetcodeEditor leetcodeEditor = ProjectConfig.getInstance(project).getEditor(file.getPath());
36+
if (leetcodeEditor == null || StringUtils.isBlank(leetcodeEditor.getContentPath())) {
37+
return false;
38+
}
39+
File contentFile = new File(leetcodeEditor.getContentPath());
40+
if (!contentFile.exists()) {
41+
return false;
42+
}
43+
} catch (Throwable e) {
44+
LogUtils.LOG.error("QuestionEditorProvider -> accept", e);
3945
return false;
4046
}
41-
4247
return this.myFirstProvider.accept(project, file);
4348
}
49+
4450
@Override
4551
public Builder createEditorAsync(@NotNull Project project, @NotNull VirtualFile file) {
4652
LeetcodeEditor leetcodeEditor = ProjectConfig.getInstance(project).getEditor(file.getPath());
@@ -60,7 +66,7 @@ public FileEditor build() {
6066
protected FileEditor createSplitEditor(@NotNull FileEditor firstEditor, @NotNull FileEditor secondEditor) {
6167

6268
//if (firstEditor instanceof TextEditor && secondEditor instanceof MarkdownSplitEditor) {
63-
return new QuestionEditorWithPreview((TextEditor)firstEditor, secondEditor);
69+
return new QuestionEditorWithPreview((TextEditor) firstEditor, secondEditor);
6470
//} else {
6571
// throw new IllegalArgumentException("Main editor should be TextEditor");
6672
//}

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

Lines changed: 93 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)