Refactor API Football to v3

This commit is contained in:
2025-01-26 22:03:10 +01:00
parent bed7c64189
commit baffe58204
24 changed files with 1719 additions and 242 deletions

View File

@@ -1,35 +1,31 @@
package de.jeyp91.apifootball;
import de.jeyp91.BaseMatch;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import de.jeyp91.BaseMatch;
public class APIFootballMatch extends BaseMatch {
private int matchId;
private int leagueId;
private String teamNameHome;
private String teamNameGuest;
private String round;
public APIFootballMatch(JSONObject json, int season) throws Exception {
this.matchId = Integer.parseInt(json.get("fixture_id").toString());
// TODO
this.leagueId = Integer.parseInt(json.get("league_id").toString());
this.teamIdHome = Integer.parseInt(((JSONObject) json.get("homeTeam")).get("team_id").toString());
this.teamIdGuest = Integer.parseInt(((JSONObject) json.get("awayTeam")).get("team_id").toString());
this.teamNameHome = ((JSONObject) json.get("homeTeam")).get("team_name").toString();
this.teamNameGuest = ((JSONObject) json.get("awayTeam")).get("team_name").toString();
this.goalsHome = getNumberOrNull(json.get("goalsHomeTeam"));
this.goalsGuest = getNumberOrNull(json.get("goalsAwayTeam"));
this.round = json.get("round").toString();
this.matchId = Integer.parseInt(((JSONObject) json.get("fixture")).get("id").toString());
this.leagueId = Integer.parseInt(((JSONObject) json.get("league")).get("id").toString());
this.teamIdHome = Integer.valueOf(((JSONObject) ((JSONObject) json.get("teams")).get("home")).get("id").toString());
this.teamIdGuest = Integer.valueOf(((JSONObject) ((JSONObject) json.get("teams")).get("away")).get("id").toString());
this.teamNameHome = ((JSONObject) ((JSONObject) json.get("teams")).get("home")).get("name").toString();
this.teamNameGuest = ((JSONObject) ((JSONObject) json.get("teams")).get("away")).get("name").toString();
this.round = ((JSONObject) json.get("league")).get("round").toString();
try {
this.matchday = getMatchdayFromRoundString(season, this.round, this.leagueId);
} catch (Exception e) {
throw new Exception("Did not find matchday for league '" + this.leagueId + "': '" + json.get("round").toString() + "'");
}
this.matchDatetime = (String) json.get("event_date");
this.status = parseStatus(json.get("statusShort").toString());
this.matchDatetime = (String) ((JSONObject) json.get("fixture")).get("date");
this.status = parseStatus(((JSONObject) ((JSONObject) json.get("fixture")).get("status")).get("short").toString());
this.parseResult(json);
}
@@ -39,15 +35,15 @@ public class APIFootballMatch extends BaseMatch {
public static int getMatchdayFromRoundString(int season, String round, int leagueId) {
round = round.replace(" ", "_");
Integer matchday = null;
Integer matchday = -1;
if (round.startsWith("Regular_Season_-_")) {
matchday = Integer.parseInt(round.replace("Regular_Season_-_", ""));
matchday = Integer.valueOf(round.replace("Regular_Season_-_", ""));
} else {
APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(season);
JSONObject roundsObject = con.getMatchdays(leagueId);
JSONArray roundsArray = (JSONArray)(((JSONObject) roundsObject.get("api")).get("fixtures"));
JSONArray roundsArray = (JSONArray)(roundsObject.get("response"));
for (int i = 0; i < roundsArray.size(); i++) {
if(roundsArray.get(i).toString().equals(round)) {
if(roundsArray.get(i).toString().replace("_", " ").equals(round.replace("_", " "))) {
matchday = i + 1;
break;
}
@@ -57,15 +53,7 @@ public class APIFootballMatch extends BaseMatch {
}
private Integer getNumberOrNull(Object object) {
return object != null ? Integer.parseInt(object.toString()) : null;
}
public String getTeamNameHome() {
return this.teamNameHome;
}
public String getTeamNameGuest() {
return this.teamNameGuest;
return object != null ? Integer.valueOf(object.toString()) : null;
}
public String getRound() {
@@ -73,36 +61,24 @@ public class APIFootballMatch extends BaseMatch {
}
private int parseStatus(String statusShort) {
switch (statusShort) {
case "TBD":
case "NS": return 0;
case "FT":
case "AET":
case "PEN": return 3;
default: return 1;
}
return switch (statusShort) {
case "TBD", "NS" -> 0;
case "FT", "AET", "PEN" -> 3;
default -> 1;
};
}
private void parseResult(JSONObject json) {
Object resultFulltime = ((JSONObject) json.get("score")).get("fulltime");
if(resultFulltime != null) {
String[] result = resultFulltime.toString().split("-");
this.goalsHome = Integer.parseInt(result[0]);
this.goalsGuest = Integer.parseInt(result[1]);
}
this.goalsHome = getNumberOrNull(((JSONObject) resultFulltime).get("home"));
this.goalsGuest = getNumberOrNull(((JSONObject) resultFulltime).get("away"));
Object resultExtratime = ((JSONObject) json.get("score")).get("extratime");
if(resultExtratime != null) {
String[] result = resultExtratime.toString().split("-");
this.goalsOvertimeHome = this.goalsHome + Integer.parseInt(result[0]);
this.goalsOvertimeGuest = this.goalsGuest + Integer.parseInt(result[1]);
}
this.goalsOvertimeHome = getNumberOrNull(((JSONObject) resultExtratime).get("home"));
this.goalsOvertimeGuest = getNumberOrNull(((JSONObject) resultExtratime).get("away"));
Object resultPenalty = ((JSONObject) json.get("score")).get("penalty");
if(resultPenalty != null) {
String[] result = resultPenalty.toString().split("-");
this.goalsPenaltyHome = this.goalsOvertimeHome + Integer.parseInt(result[0]);
this.goalsPenaltyGuest = this.goalsOvertimeGuest + Integer.parseInt(result[1]);
}
this.goalsPenaltyHome = getNumberOrNull(((JSONObject) resultPenalty).get("home"));
this.goalsPenaltyGuest = getNumberOrNull(((JSONObject) resultPenalty).get("away"));
}
}