@@ -30,7 +30,7 @@ public static List<Submission> getSubmissionService(Question question, Project p
30
30
List <Submission > submissionList = new ArrayList <Submission >();
31
31
32
32
try {
33
- HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (),"application/json" );
33
+ HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (), "application/json" );
34
34
httpRequest .setBody ("{\" operationName\" :\" Submissions\" ,\" variables\" :{\" offset\" :0,\" limit\" :20,\" lastKey\" :null,\" questionSlug\" :\" " + question .getTitleSlug () + "\" },\" query\" :\" query Submissions($offset: Int!, $limit: Int!, $lastKey: String, $questionSlug: String!) {\\ n submissionList(offset: $offset, limit: $limit, lastKey: $lastKey, questionSlug: $questionSlug) {\\ n lastKey\\ n hasNext\\ n submissions {\\ n id\\ n statusDisplay\\ n lang\\ n runtime\\ n timestamp\\ n url\\ n isPending\\ n memory\\ n __typename\\ n }\\ n __typename\\ n }\\ n}\\ n\" }" );
35
35
httpRequest .addHeader ("Accept" , "application/json" );
36
36
HttpResponse response = HttpRequestUtils .executePost (httpRequest );
@@ -76,65 +76,56 @@ public static void openSubmission(Submission submission, Question question, Proj
76
76
77
77
File file = new File (filePath );
78
78
if (file .exists ()) {
79
- FileUtils .openFileEditor (file ,project );
79
+ FileUtils .openFileEditor (file , project );
80
80
} else {
81
81
try {
82
- HttpRequest httpRequest = HttpRequest .get (URLUtils .getLeetcodeSubmissions () + submission .getId () + "/" );
83
- HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
84
- if (response != null && response .getStatusCode () == 200 ) {
85
-
86
- String html = response .getBody ();
87
- String body = CommentUtils .createSubmissions (html );
88
- if (StringUtils .isBlank (body )) {
89
- LogUtils .LOG .error (html );
90
- MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("submission.parse" ));
91
- } else {
92
- try {
93
- JSONObject jsonObject = JSONObject .parseObject (body );
94
- StringBuffer sb = new StringBuffer ();
95
-
96
- sb .append (jsonObject .getString ("submissionCode" ).replaceAll ("\\ u000A" , "\n " )).append ("\n " );
97
-
98
- JSONObject submissionData = jsonObject .getJSONObject ("submissionData" );
99
- if ("Accepted" .equals (submission .getStatus ())) {
100
- sb .append (codeTypeEnum .getComment ()).append ("runtime:" ).append (submissionData .getString ("runtime" )).append ("\n " );
101
- sb .append (codeTypeEnum .getComment ()).append ("memory:" ).append (submissionData .getString ("memory" )).append ("\n " );
102
- } else if ("Wrong Answer" .equals (submission .getStatus ())) {
103
- sb .append (codeTypeEnum .getComment ()).append ("total_testcases:" ).append (submissionData .getString ("total_testcases" )).append ("\n " );
104
- sb .append (codeTypeEnum .getComment ()).append ("total_correct:" ).append (submissionData .getString ("total_correct" )).append ("\n " );
105
- sb .append (codeTypeEnum .getComment ()).append ("input_formatted:" ).append (submissionData .getString ("input_formatted" )).append ("\n " );
106
- sb .append (codeTypeEnum .getComment ()).append ("expected_output:" ).append (submissionData .getString ("expected_output" )).append ("\n " );
107
- sb .append (codeTypeEnum .getComment ()).append ("code_output:" ).append (submissionData .getString ("code_output" )).append ("\n " );
108
- } else if ("Runtime Error" .equals (submission .getStatus ())) {
109
- sb .append (codeTypeEnum .getComment ()).append ("runtime_error:" ).append (submissionData .getString ("runtime_error" )).append ("\n " );
110
- sb .append (codeTypeEnum .getComment ()).append ("last_testcase:" ).append (submissionData .getString ("last_testcase" ).replaceAll ("(\\ r|\\ r\\ n|\\ n\\ r|\\ n)" , " " )).append ("\n " );
111
- } else if ("Compile Error" .equals (submission .getStatus ())) {
112
- sb .append (codeTypeEnum .getComment ()).append ("total_correct:" ).append (submissionData .getString ("total_correct" )).append ("\n " );
113
- sb .append (codeTypeEnum .getComment ()).append ("compile_error:" ).append (submissionData .getString ("compile_error" )).append ("\n " );
114
- } else {
115
- sb .append (codeTypeEnum .getComment ()).append ("runtime:" ).append (submissionData .getString ("runtime" )).append ("\n " );
116
- sb .append (codeTypeEnum .getComment ()).append ("memory:" ).append (submissionData .getString ("memory" )).append ("\n " );
117
- sb .append (codeTypeEnum .getComment ()).append ("total_testcases:" ).append (submissionData .getString ("total_testcases" )).append ("\n " );
118
- sb .append (codeTypeEnum .getComment ()).append ("total_correct:" ).append (submissionData .getString ("total_correct" )).append ("\n " );
119
- sb .append (codeTypeEnum .getComment ()).append ("input_formatted:" ).append (submissionData .getString ("input_formatted" )).append ("\n " );
120
- sb .append (codeTypeEnum .getComment ()).append ("expected_output:" ).append (submissionData .getString ("expected_output" )).append ("\n " );
121
- sb .append (codeTypeEnum .getComment ()).append ("code_output:" ).append (submissionData .getString ("code_output" )).append ("\n " );
122
- sb .append (codeTypeEnum .getComment ()).append ("runtime_error:" ).append (submissionData .getString ("runtime_error" )).append ("\n " );
123
- if (submissionData .containsKey ("last_testcase" )) {
124
- sb .append (codeTypeEnum .getComment ()).append ("last_testcase:" ).append (submissionData .getString ("last_testcase" ).replaceAll ("(\\ r|\\ r\\ n|\\ n\\ r|\\ n)" , " " )).append ("\n " );
125
- }
126
- }
127
- FileUtils .saveFile (file , sb .toString ());
128
- FileUtils .openFileEditor (file ,project );
129
- } catch (Exception e ) {
130
- LogUtils .LOG .error (body , e );
131
- MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("submission.parse" ));
132
- }
133
- }
134
82
83
+ JSONObject jsonObject ;
84
+ if (URLUtils .isCn ()) {
85
+ jsonObject = loadSubmissionCn (submission ,project );
86
+ } else {
87
+ jsonObject = loadSubmissionEn (submission ,project );
88
+ }
89
+ if (jsonObject == null ) {
90
+ return ;
91
+ }
92
+
93
+ StringBuffer sb = new StringBuffer ();
94
+
95
+ sb .append (jsonObject .getString ("submissionCode" ).replaceAll ("\\ u000A" , "\n " )).append ("\n " );
96
+
97
+ JSONObject submissionData = jsonObject .getJSONObject ("submissionData" );
98
+ if ("Accepted" .equals (submission .getStatus ())) {
99
+ sb .append (codeTypeEnum .getComment ()).append ("runtime:" ).append (submissionData .getString ("runtime" )).append ("\n " );
100
+ sb .append (codeTypeEnum .getComment ()).append ("memory:" ).append (submissionData .getString ("memory" )).append ("\n " );
101
+ } else if ("Wrong Answer" .equals (submission .getStatus ())) {
102
+ sb .append (codeTypeEnum .getComment ()).append ("total_testcases:" ).append (submissionData .getString ("total_testcases" )).append ("\n " );
103
+ sb .append (codeTypeEnum .getComment ()).append ("total_correct:" ).append (submissionData .getString ("total_correct" )).append ("\n " );
104
+ sb .append (codeTypeEnum .getComment ()).append ("input_formatted:" ).append (submissionData .getString ("input_formatted" )).append ("\n " );
105
+ sb .append (codeTypeEnum .getComment ()).append ("expected_output:" ).append (submissionData .getString ("expected_output" )).append ("\n " );
106
+ sb .append (codeTypeEnum .getComment ()).append ("code_output:" ).append (submissionData .getString ("code_output" )).append ("\n " );
107
+ } else if ("Runtime Error" .equals (submission .getStatus ())) {
108
+ sb .append (codeTypeEnum .getComment ()).append ("runtime_error:" ).append (submissionData .getString ("runtime_error" )).append ("\n " );
109
+ sb .append (codeTypeEnum .getComment ()).append ("last_testcase:" ).append (submissionData .getString ("last_testcase" ).replaceAll ("(\\ r|\\ r\\ n|\\ n\\ r|\\ n)" , " " )).append ("\n " );
110
+ } else if ("Compile Error" .equals (submission .getStatus ())) {
111
+ sb .append (codeTypeEnum .getComment ()).append ("total_correct:" ).append (submissionData .getString ("total_correct" )).append ("\n " );
112
+ sb .append (codeTypeEnum .getComment ()).append ("compile_error:" ).append (submissionData .getString ("compile_error" )).append ("\n " );
135
113
} else {
136
- MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("request.failed" ));
114
+ sb .append (codeTypeEnum .getComment ()).append ("runtime:" ).append (submissionData .getString ("runtime" )).append ("\n " );
115
+ sb .append (codeTypeEnum .getComment ()).append ("memory:" ).append (submissionData .getString ("memory" )).append ("\n " );
116
+ sb .append (codeTypeEnum .getComment ()).append ("total_testcases:" ).append (submissionData .getString ("total_testcases" )).append ("\n " );
117
+ sb .append (codeTypeEnum .getComment ()).append ("total_correct:" ).append (submissionData .getString ("total_correct" )).append ("\n " );
118
+ sb .append (codeTypeEnum .getComment ()).append ("input_formatted:" ).append (submissionData .getString ("input_formatted" )).append ("\n " );
119
+ sb .append (codeTypeEnum .getComment ()).append ("expected_output:" ).append (submissionData .getString ("expected_output" )).append ("\n " );
120
+ sb .append (codeTypeEnum .getComment ()).append ("code_output:" ).append (submissionData .getString ("code_output" )).append ("\n " );
121
+ sb .append (codeTypeEnum .getComment ()).append ("runtime_error:" ).append (submissionData .getString ("runtime_error" )).append ("\n " );
122
+ if (submissionData .containsKey ("last_testcase" )) {
123
+ sb .append (codeTypeEnum .getComment ()).append ("last_testcase:" ).append (submissionData .getString ("last_testcase" ).replaceAll ("(\\ r|\\ r\\ n|\\ n\\ r|\\ n)" , " " )).append ("\n " );
124
+ }
137
125
}
126
+ FileUtils .saveFile (file , sb .toString ());
127
+ FileUtils .openFileEditor (file , project );
128
+
138
129
139
130
} catch (Exception e ) {
140
131
LogUtils .LOG .error ("获取提交详情失败" , e );
@@ -143,5 +134,65 @@ public static void openSubmission(Submission submission, Question question, Proj
143
134
}
144
135
145
136
}
137
+
138
+ }
139
+
140
+ private static JSONObject loadSubmissionEn (Submission submission ,Project project ) {
141
+ HttpRequest httpRequest = HttpRequest .get (URLUtils .getLeetcodeSubmissions () + submission .getId () + "/" );
142
+ HttpResponse response = HttpRequestUtils .executeGet (httpRequest );
143
+ if (response != null && response .getStatusCode () == 200 ) {
144
+ String html = response .getBody ();
145
+ String body = CommentUtils .createSubmissions (html );
146
+ if (StringUtils .isBlank (body )) {
147
+ LogUtils .LOG .error (html );
148
+ MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("submission.parse" ));
149
+ } else {
150
+ try {
151
+ JSONObject jsonObject = JSONObject .parseObject (body );
152
+ return jsonObject ;
153
+ } catch (Exception e ) {
154
+ LogUtils .LOG .error (body , e );
155
+ MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("submission.parse" ));
156
+ }
157
+ }
158
+ } else {
159
+ MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("request.failed" ));
160
+ }
161
+ return null ;
162
+ }
163
+
164
+ private static JSONObject loadSubmissionCn (Submission submission ,Project project ) {
165
+ HttpRequest httpRequest = HttpRequest .post (URLUtils .getLeetcodeGraphql (), "application/json" );
166
+ httpRequest .setBody ("{\" operationName\" :\" mySubmissionDetail\" ,\" variables\" :{\" id\" :\" " + submission .getId () + "\" },\" query\" :\" query mySubmissionDetail($id: ID!) {\\ n submissionDetail(submissionId: $id) {\\ n id\\ n code\\ n runtime\\ n memory\\ n statusDisplay\\ n timestamp\\ n lang\\ n passedTestCaseCnt\\ n totalTestCaseCnt\\ n sourceUrl\\ n question {\\ n titleSlug\\ n title\\ n translatedTitle\\ n questionId\\ n __typename\\ n }\\ n ... on GeneralSubmissionNode {\\ n outputDetail {\\ n codeOutput\\ n expectedOutput\\ n input\\ n compileError\\ n runtimeError\\ n lastTestcase\\ n __typename\\ n }\\ n __typename\\ n }\\ n __typename\\ n }\\ n}\\ n\" }" );
167
+ httpRequest .addHeader ("Accept" , "application/json" );
168
+ HttpResponse response = HttpRequestUtils .executePost (httpRequest );
169
+ if (response != null && response .getStatusCode () == 200 ) {
170
+ String body = response .getBody ();
171
+ if (StringUtils .isNotBlank (body )) {
172
+ JSONObject jsonObject = new JSONObject ();
173
+ JSONObject cnObject = JSONObject .parseObject (body ).getJSONObject ("data" ).getJSONObject ("submissionDetail" );
174
+
175
+ jsonObject .put ("submissionCode" , cnObject .getString ("code" ));
176
+
177
+ JSONObject submissionData = new JSONObject ();
178
+ submissionData .put ("runtime" , cnObject .getString ("runtime" ));
179
+ submissionData .put ("memory" , cnObject .getString ("memory" ));
180
+ submissionData .put ("total_testcases" , cnObject .getString ("totalTestCaseCnt" ));
181
+ submissionData .put ("total_correct" , cnObject .getString ("passedTestCaseCnt" ));
182
+ submissionData .put ("input_formatted" , cnObject .getJSONObject ("outputDetail" ).getString ("input" ));
183
+ submissionData .put ("expected_output" , cnObject .getJSONObject ("outputDetail" ).getString ("expectedOutput" ));
184
+ submissionData .put ("code_output" , cnObject .getJSONObject ("outputDetail" ).getString ("codeOutput" ));
185
+ submissionData .put ("runtime_error" , cnObject .getJSONObject ("outputDetail" ).getString ("runtimeError" ));
186
+ submissionData .put ("last_testcase" , cnObject .getJSONObject ("outputDetail" ).getString ("lastTestcase" ));
187
+ submissionData .put ("compile_error" , cnObject .getJSONObject ("outputDetail" ).getString ("compileError" ));
188
+ jsonObject .put ("submissionData" , submissionData );
189
+
190
+ return jsonObject ;
191
+
192
+ }
193
+ } else {
194
+ MessageUtils .getInstance (project ).showWarnMsg ("error" , PropertiesUtils .getInfo ("request.failed" ));
195
+ }
196
+ return null ;
146
197
}
147
198
}
0 commit comments