Skip to content

Commit 994d960

Browse files
author
jetsaii
committed
Complete graphql parsing
1 parent 6ab2d93 commit 994d960

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

src/main/java/leetcode/api/StatsResponse.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class StatsResponse {
44
private final String status;
55
private final String message;
66
private final int totalSolved;
7+
private final int totalQuestions;
78
private final int easySolved;
89
private final int totalEasy;
910
private final int mediumSolved;
@@ -15,10 +16,11 @@ public class StatsResponse {
1516
private final int contributionPoints;
1617
private final int reputation;
1718

18-
public StatsResponse(String status, String message, int totalSolved, int easySolved, int totalEasy, int mediumSolved, int totalMedium, int hardSolved, int totalHard, float acceptanceRate, int ranking, int contributionPoints, int reputation) {
19+
public StatsResponse(String status, String message, int totalSolved, int totalQuestions, int easySolved, int totalEasy, int mediumSolved, int totalMedium, int hardSolved, int totalHard, float acceptanceRate, int ranking, int contributionPoints, int reputation) {
1920
this.status = status;
2021
this.message = message;
2122
this.totalSolved = totalSolved;
23+
this.totalQuestions = totalQuestions;
2224
this.easySolved = easySolved;
2325
this.totalEasy = totalEasy;
2426
this.mediumSolved = mediumSolved;
@@ -43,6 +45,8 @@ public int getTotalSolved() {
4345
return totalSolved;
4446
}
4547

48+
public int getTotalQuestions() { return totalQuestions; }
49+
4650
public int getEasySolved() {
4751
return easySolved;
4852
}

src/main/java/leetcode/api/StatsServiceImpl.java

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import org.springframework.stereotype.Service;
44
import java.io.IOException;
5+
import java.math.BigDecimal;
6+
import java.math.RoundingMode;
7+
58
import org.springframework.boot.configurationprocessor.json.JSONObject;
9+
import org.springframework.boot.configurationprocessor.json.JSONArray;
610
import org.springframework.boot.configurationprocessor.json.JSONException;
711
import okhttp3.Request;
812
import okhttp3.OkHttpClient;
@@ -35,14 +39,78 @@ public StatsResponse getStats(String username) {
3539

3640
if (response.isSuccessful()) {
3741
// Parse GraphQL response
38-
return new StatsResponse("success", "retrieved", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
42+
43+
// User not found
44+
if (jsonObject.has("errors")) {
45+
return new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
46+
} else { // Parse user info
47+
return decodeGraphqlJson(jsonObject);
48+
}
3949
} else {
40-
return new StatsResponse("error", jsonObject.getString("error"), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
50+
return new StatsResponse("error", jsonObject.getString("error"), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
4151
}
42-
} catch (IOException ex) {
43-
return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
52+
} catch (IOException ex) { // Post request error
53+
return new StatsResponse("error", ex.getMessage(), 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
4454
} catch (JSONException ex) { // Query serialization error
45-
return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
55+
return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
56+
}
57+
}
58+
59+
private StatsResponse decodeGraphqlJson(JSONObject json) {
60+
int totalSolved = 0;
61+
int totalQuestions = 0;
62+
int easySolved = 0;
63+
int totalEasy = 0;
64+
int mediumSolved = 0;
65+
int totalMedium = 0;
66+
int hardSolved = 0;
67+
int totalHard = 0;
68+
float acceptanceRate = 0;
69+
int ranking = 0;
70+
int contributionPoints = 0;
71+
int reputation = 0;
72+
73+
try {
74+
JSONObject data = json.getJSONObject("data");
75+
JSONArray allQuestions = data.getJSONArray("allQuestionsCount");
76+
JSONObject matchedUser = data.getJSONObject("matchedUser");
77+
JSONObject submitStats = matchedUser.getJSONObject("submitStats");
78+
JSONArray actualSubmissions = submitStats.getJSONArray("acSubmissionNum");
79+
JSONArray totalSubmissions = submitStats.getJSONArray("totalSubmissionNum");
80+
81+
// Fill in total counts
82+
totalQuestions = allQuestions.getJSONObject(0).getInt("count");
83+
totalEasy = allQuestions.getJSONObject(1).getInt("count");
84+
totalMedium = allQuestions.getJSONObject(2).getInt("count");
85+
totalHard = allQuestions.getJSONObject(3).getInt("count");
86+
87+
// Fill in solved counts
88+
totalSolved = actualSubmissions.getJSONObject(0).getInt("count");
89+
easySolved = actualSubmissions.getJSONObject(1).getInt("count");
90+
mediumSolved = actualSubmissions.getJSONObject(2).getInt("count");
91+
hardSolved = actualSubmissions.getJSONObject(3).getInt("count");
92+
93+
// Fill in etc
94+
float totalAcceptCount = actualSubmissions.getJSONObject(0).getInt("submissions");
95+
float totalSubCount = totalSubmissions.getJSONObject(0).getInt("submissions");
96+
if (totalSubCount != 0) {
97+
acceptanceRate = round((totalAcceptCount / totalSubCount) * 100, 2);
98+
}
99+
100+
contributionPoints = matchedUser.getJSONObject("contributions").getInt("points");
101+
reputation = matchedUser.getJSONObject("profile").getInt("reputation");
102+
ranking = matchedUser.getJSONObject("profile").getInt("ranking");
103+
104+
} catch (JSONException ex) {
105+
return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
46106
}
107+
108+
return new StatsResponse("success", "retrieved", totalSolved, totalQuestions, easySolved, totalEasy, mediumSolved, totalMedium, hardSolved, totalHard, acceptanceRate, ranking, contributionPoints, reputation);
109+
}
110+
111+
private float round(float d, int decimalPlace) {
112+
BigDecimal bd = new BigDecimal(Float.toString(d));
113+
bd = bd.setScale(decimalPlace, RoundingMode.HALF_UP);
114+
return bd.floatValue();
47115
}
48116
}

src/main/java/leetcode/api/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public StatsResponse getStats(@PathVariable Optional<String> username) {
1818
} else {
1919
String status = "error";
2020
String msg = "please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)";
21-
return new StatsResponse(status, msg, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
21+
return new StatsResponse(status, msg, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)