Skip to content

Commit

Permalink
add getBeatmap void and fix beatmap returning null
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Jun 13, 2021
1 parent c860ddb commit cc05855
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
.idea
.idea
src/main/java/tw/kane/osu4j/Osu.java
15 changes: 13 additions & 2 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>tw.kane</groupId>
<artifactId>osu4j</artifactId>
<version>v2.1</version>
<version>v2.3</version>
<build>
<plugins>
<plugin>
Expand All @@ -18,6 +18,17 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>tw.kane.osu4j.Osu</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
Expand All @@ -28,7 +39,7 @@
</repository>
</distributionManagement>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
</properties>
</project>
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tw.kane</groupId>
<artifactId>osu4j</artifactId>
<version>v2.2</version>
<version>v2.3.1</version>

<distributionManagement>
<repository>
Expand All @@ -31,6 +31,18 @@
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-jar-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifest>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- <mainClass>tw.kane.osu4j.Osu</mainClass>-->
<!-- </manifest>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/tw/kane/osu4j/API.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tw.kane.osu4j;

import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.URLEncoder;

import static tw.kane.osu4j.OsuClient.httpClient;
import static tw.kane.osu4j.OsuClient.oauth;

public class API {

public static String ENDPOINT = "https://osu.ppy.sh/api/v2";

public static String makeRequest(String url, String ...args) throws IOException {
Request request = new Request.Builder()
.url(String.format(ENDPOINT + url, args))
.headers(oauth.header())
.build();
Response response = httpClient.newCall(request).execute();
return response.body().string();
}
}
2 changes: 2 additions & 0 deletions src/main/java/tw/kane/osu4j/Base/Beatmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public class Beatmap {

public Beatmap(String json) {
object = JsonPath.parse(json);
init();
}

public Beatmap(JSONObject json) {
object = JsonPath.parse(json.toString());
init();
}

public void init() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tw/kane/osu4j/Base/BeatmapSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public class BeatmapSet {

public BeatmapSet(String json) {
object = JsonPath.parse(json);
init();
}

public BeatmapSet(JSONObject json) {
object = JsonPath.parse(json.toString());
init();
}

public void init() {
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/tw/kane/osu4j/JSON.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package tw.kane.osu4j;

import com.jayway.jsonpath.DocumentContext;
import org.json.JSONObject;

public class JSON {
// public static boolean checkExist(JSONObject object, String path) {
// if(object.isNull(path.split("\\.")[0]))
// return false;
// if(path.split("\\.").length > 1)
// return checkExist(
// object.getJSONObject(path.split("\\.")[0]),
// path.substring(path.split("\\.")[0].length() + 1)
// );
// return true;
// }
public static <T> T get(DocumentContext json, String path, Class<T> _class, T whenNull) {
T obj;
try {
obj = json.read(path, _class);
if(obj == null) obj = whenNull;
} catch (Exception e) {
obj = whenNull;
}
Expand Down
51 changes: 26 additions & 25 deletions src/main/java/tw/kane/osu4j/OsuClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import tw.kane.osu4j.Base.Beatmap;
import tw.kane.osu4j.Base.Score;
import tw.kane.osu4j.Base.User;
import tw.kane.osu4j.Exception.InvalidTokenException;
import tw.kane.osu4j.Exception.NotFoundException;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -38,16 +40,11 @@ public User getUser(String id, Mode mode, boolean allowCached) throws IOExceptio
if(allowCached && Cache.userCache.containsKey(id))
return Cache.userCache.get(id);

Request request = new Request.Builder()
.url(String.format("https://osu.ppy.sh/api/v2/users/%s/%s",
id,
mode.getName()
))
.headers(oauth.header())
.build();
Response response = httpClient.newCall(request).execute();
String responseString = response.body().string();
JSONObject result = new JSONObject(responseString);
String response = API.makeRequest(
"/users/%s/%s",
URLEncoder.encode(id, "UTF-8"),
mode.getName());
JSONObject result = new JSONObject(response);
if(!result.isNull("authentication"))
throw new InvalidTokenException("token wrong");
if(!result.isNull("error"))
Expand All @@ -59,7 +56,7 @@ public User getUser(String id, Mode mode, boolean allowCached) throws IOExceptio

/**
* get user's recent plays in 24hrs
* @param id user's name or id
* @param id user's ID ONLY
* @param type score type best or recent
* @param includeFails whether include failed scores
* @param mode get specify mode's score
Expand All @@ -68,24 +65,18 @@ public User getUser(String id, Mode mode, boolean allowCached) throws IOExceptio
* @throws NotFoundException if user not found
*/
public Score[] getUserScores(String id, ScoreType type, boolean includeFails, Mode mode) throws IOException, InvalidTokenException, NotFoundException {
Request request = new Request.Builder()
.url(String.format("https://osu.ppy.sh/api/v2/users/%s/scores/%s?include_fails=%c&mode=%s",
id,
type.getName(),
includeFails ? 1 : 0,
mode.getName()
))
.headers(oauth.header())
.build();
Response response = httpClient.newCall(request).execute();
String responseString = response.body().string();
if(responseString.startsWith("{")) {
JSONObject result = new JSONObject(responseString);
String response = API.makeRequest("/users/%s/scores/%s?include_fails=%s&mode=%s",
URLEncoder.encode(id, "UTF-8"),
type.getName(),
includeFails ? "1" : "0",
mode.getName());
if(response.startsWith("{")) {
JSONObject result = new JSONObject(response);
if(!result.isNull("authentication"))
throw new InvalidTokenException("token wrong");
}
else {
JSONArray resultArray = new JSONArray(responseString);
JSONArray resultArray = new JSONArray(response);
if(resultArray.length() == 0)
throw new NotFoundException();
List<Score> scores = new ArrayList<>();
Expand All @@ -96,4 +87,14 @@ public Score[] getUserScores(String id, ScoreType type, boolean includeFails, Mo
}
return null;
}

public Beatmap getBeatmap(String id) throws IOException, InvalidTokenException, NotFoundException {
String response = API.makeRequest("/beatmaps/%s", URLEncoder.encode(id, "UTF-8"));
JSONObject result = new JSONObject(response);
if(!result.isNull("authentication"))
throw new InvalidTokenException("token wrong");
if(!result.isNull("error"))
throw new NotFoundException();
return new Beatmap(result);
}
}

0 comments on commit cc05855

Please sign in to comment.