First version with ability to create database for new season
This commit is contained in:
76
src/main/java/de/jeyp91/apifootball/APIFootballMatch.java
Normal file
76
src/main/java/de/jeyp91/apifootball/APIFootballMatch.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package de.jeyp91.apifootball;
|
||||
|
||||
import com.google.common.io.Resources;
|
||||
import de.jeyp91.BaseMatch;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class APIFootballMatch extends BaseMatch {
|
||||
|
||||
private int season;
|
||||
private int matchId;
|
||||
private int leagueId;
|
||||
private String matchStatus;
|
||||
|
||||
public APIFootballMatch(JSONObject json, int season) {
|
||||
this.season = season;
|
||||
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.goalsHome = getNumberOrNull(json.get("goalsHomeTeam"));
|
||||
this.goalsGuest = getNumberOrNull(json.get("goalsAwayTeam"));
|
||||
this.matchday = getMatchdayFromRoundString(json.get("round").toString(), this.leagueId);
|
||||
this.matchStatus = json.get("statusShort").toString();
|
||||
this.matchDatetime = (String) json.get("event_date");
|
||||
}
|
||||
|
||||
public int getAPIFootBallMatchID() {
|
||||
return this.matchId;
|
||||
}
|
||||
|
||||
private int getMatchdayFromRoundString(String round, int leagueId) {
|
||||
round = round.replace(" ", "_");
|
||||
Integer matchday = null;
|
||||
JSONObject roundsObject = readFromFile("rounds_" + leagueId + ".json");
|
||||
JSONArray roundsArray = (JSONArray)(((JSONObject) roundsObject.get("api")).get("fixtures"));
|
||||
for (int i = 0; i < roundsArray.size(); i++) {
|
||||
if(roundsArray.get(i).toString().equals(round)) {
|
||||
matchday = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return matchday;
|
||||
}
|
||||
|
||||
private JSONObject readFromFile(String filename) {
|
||||
|
||||
JSONObject object = null;
|
||||
//JSON parser object to parse read file
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
URL url = Resources.getResource((this.season + 1) + "\\API-Football\\" + filename);
|
||||
String jsonConfig = null;
|
||||
|
||||
try {
|
||||
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
|
||||
//Read JSON file
|
||||
object = (JSONObject) jsonParser.parse(jsonConfig);
|
||||
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
private Integer getNumberOrNull(Object object) {
|
||||
return object != null ? Integer.parseInt(object.toString()) : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user