Add api football files updater

This commit is contained in:
2020-10-07 22:14:13 +02:00
parent bce0f3800c
commit f22f9a929b
18 changed files with 15808 additions and 1048 deletions

View File

@@ -91,80 +91,4 @@ public class APIFootballConnector {
return object;
}
private JSONArray getDataAsJSONArray(String requestUrl) {
String rawData = getRawData(requestUrl);
JSONParser parser = new JSONParser();
JSONArray data = null;
try {
data = (JSONArray) parser.parse(rawData);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return data;
}
private JSONObject getDataAsJSONObject(String requestUrl) {
String rawData = getRawData(requestUrl);
JSONParser parser = new JSONParser();
JSONObject data = null;
try {
data = (JSONObject) parser.parse(rawData);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return data;
}
String getRawData(String requestUrl) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(requestUrl);
// add request header
request.addHeader("Content-Type", "application/json");
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
/* TODO */
e.printStackTrace();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())
);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
StringBuilder result = new StringBuilder();
String line = "";
while (true) {
try {
line = rd.readLine();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
// Stop reading if last line was found.
if (line == null) break;
result.append(line);
}
return result.toString();
}
}

View File

@@ -17,6 +17,8 @@ public class APIFootballMatch extends BaseMatch {
private int matchId;
private int leagueId;
private String matchStatus;
private String teamNameHome;
private String teamNameGuest;
public APIFootballMatch(JSONObject json, int season) {
this.season = season;
@@ -25,6 +27,8 @@ public class APIFootballMatch extends BaseMatch {
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.matchday = getMatchdayFromRoundString(json.get("round").toString(), this.leagueId);
@@ -73,4 +77,12 @@ 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;
}
}

View File

@@ -0,0 +1,158 @@
package de.jeyp91.apifootball;
import com.google.common.io.Resources;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
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.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
public class APIFootballUpdater {
private int season;
public APIFootballUpdater(int season) {
this.season = season;
}
public void updateFixtures(int league) throws IOException {
String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
String content = getRawData(apiFootballUrl);
String resourcePath = this.season + "/API-Football/matches_league_" + league + ".json";
writeStringToFile(content, resourcePath);
}
public void updateAllFixtures(String configPath) throws IOException {
HashSet<Integer> leagues = getAllLeaguesFromLeagueConfig(configPath);
for (Integer league : leagues) {
updateFixtures(league);
}
}
private HashSet<Integer> getAllLeaguesFromLeagueConfig(String configPath) {
HashSet<Integer> leagues = new HashSet<>();
JSONParser jsonParser = new JSONParser();
URL url = Resources.getResource(season + "\\" + configPath);
String jsonConfig = null;
JSONObject configObject = null;
try {
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
configObject = (JSONObject) jsonParser.parse(jsonConfig);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
JSONArray matchdayConfigs = (JSONArray) configObject.get("matchdayConfig");
for (Object matchdayConfig : matchdayConfigs) {
JSONArray matchesConfig = (JSONArray) ((JSONObject) matchdayConfig).get("matchesConfig");
for (Object matchConfig : matchesConfig) {
if (((JSONObject) matchConfig).containsKey("matchesLeague")) {
int league = Integer.parseInt(((JSONObject) matchConfig).get("matchesLeague").toString());
leagues.add(league);
}
if (((JSONObject) matchConfig).containsKey("matchLeague")) {
int league = Integer.parseInt(((JSONObject) matchConfig).get("matchLeague").toString());
leagues.add(league);
}
}
}
return leagues;
}
private void writeStringToFile(String content, String resourcePath) throws IOException {
URL url = Resources.getResource(resourcePath);
FileWriter myWriter = new FileWriter(url.getPath());
myWriter.write(content);
myWriter.close();
}
private JSONArray getDataAsJSONArray(String requestUrl) {
String rawData = getRawData(requestUrl);
JSONParser parser = new JSONParser();
JSONArray data = null;
try {
data = (JSONArray) parser.parse(rawData);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return data;
}
private JSONObject getDataAsJSONObject(String requestUrl) {
String rawData = getRawData(requestUrl);
JSONParser parser = new JSONParser();
JSONObject data = null;
try {
data = (JSONObject) parser.parse(rawData);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return data;
}
String getRawData(String requestUrl) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(requestUrl);
// add request header
request.addHeader("X-RapidAPI-Key", "a607e6a7437d9d52f3ac73e0b2704d0b");
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
/* TODO */
e.printStackTrace();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())
);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
StringBuilder result = new StringBuilder();
String line = "";
while (true) {
try {
line = rd.readLine();
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
// Stop reading if last line was found.
if (line == null) break;
result.append(line);
}
return result.toString();
}
}

View File

@@ -110,8 +110,8 @@ public class TLWMatch extends BaseMatch{
this.league = league;
this.matchday = matchday;
this.matchNo = matchNo;
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
this.teamIdHome = this.getTeamIdHomeFromApiFootballMatch(apiFootballMatch);
this.teamIdGuest = this.getTeamIdGuestFromApiFootballMatch(apiFootballMatch);
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
@@ -270,4 +270,20 @@ public class TLWMatch extends BaseMatch{
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
}
private Integer getTeamIdHomeFromApiFootballMatch(APIFootballMatch match) {
Integer id = TeamIDMatcher.getTippligaIdFromApiFootballId(match.getTeamIdHome());
if(id == null) {
System.out.println("Could not find id " + match.getTeamIdHome() + " for " + match.getTeamNameHome());
}
return id;
}
private Integer getTeamIdGuestFromApiFootballMatch(APIFootballMatch match) {
Integer id = TeamIDMatcher.getTippligaIdFromApiFootballId(match.getTeamIdGuest());
if(id == null) {
System.out.println("Could not find id " + match.getTeamIdGuest() + " for " + match.getTeamNameGuest());
}
return id;
}
}

View File

@@ -53,7 +53,7 @@ public class TLWMatchdaysUpdater {
if(earliestDate.after(now)) {
String updateStart = "UPDATE phpbb_footb_matches SET ";
String updateStart = "UPDATE phpbb_footb_matchdays SET ";
String condition = "WHERE season = " + matchdayOriginal.getSeason() + " " +
"AND league = " + matchdayOriginal.getLeague() + " " +
"AND matchday = " + matchdayOriginal.getMatchday() + ";\n";

View File

@@ -24,9 +24,10 @@ public class TippligaSQLConnector {
}
public TippligaSQLConnector() {
String jdbcUrl = "jdbc:mysql://localhost/d0144ddb?user=root&password=&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String jdbcUrlLocalhost = "jdbc:mysql://localhost/d0144ddb?user=root&password=&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String jdbcUrlProd = "jdbc:mysql://dd16124.kasserver.com/d0144ddb?user=d0144ddb&password=TippligaWuerzb_1&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
try {
con = DriverManager.getConnection(jdbcUrl);
con = DriverManager.getConnection(jdbcUrlProd);
} catch (SQLException e) {
/* TODO */
e.printStackTrace();