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,7 +1,9 @@
package de.jeyp91.apifootball;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
@@ -12,29 +14,30 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.*;
import java.util.HashSet;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
public class APIFootballUpdater {
private static final Logger logger = LogManager.getLogger(APIFootballUpdater.class);
private static final String baseurl = "https://v3.football.api-sports.io/";
public APIFootballUpdater() {
}
public void updateFixtures(int league) {
String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
public void updateFixtures(int season, int league) {
String requestPath = "fixtures";
S3Provider prov = new S3Provider();
try {
String content = getRawData(apiFootballUrl);
prov.writeFixturesToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeFixturesToS3(season, league, content);
} catch (Exception e) {
if(e.getMessage().toLowerCase().contains("rate limit'")) {
try {
Thread.sleep(1000 * 60);
String content = getRawData(apiFootballUrl);
prov.writeFixturesToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeFixturesToS3(season, league, content);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (Exception exception) {
@@ -49,7 +52,7 @@ public class APIFootballUpdater {
public void updateAllFixtures(int season) {
HashSet<Integer> leagues = getLeagues(season);
for (Integer league : leagues) {
updateFixtures(league);
updateFixtures(season, league);
try {
Thread.sleep(1000*6);
} catch (InterruptedException e) {
@@ -61,7 +64,7 @@ public class APIFootballUpdater {
public void updateAllRounds(int season) {
HashSet<Integer> leagues = getLeagues(season);
for (Integer league : leagues) {
updateRounds(league);
updateRounds(season, league);
try {
Thread.sleep(1000*6);
} catch (InterruptedException e) {
@@ -70,18 +73,18 @@ public class APIFootballUpdater {
}
}
public void updateRounds(int league) {
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
public void updateRounds(int season, int league) {
String requestPath = "fixtures/rounds";
S3Provider prov = new S3Provider();
try {
String content = getRawData(apiFootballUrl);
prov.writeRoundsToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeRoundsToS3(season, league, content);
} catch (Exception e) {
if(e.getMessage().toLowerCase().contains("rate limit'")) {
try {
Thread.sleep(1000 * 60);
String content = getRawData(apiFootballUrl);
prov.writeRoundsToS3(league, content);
String content = getRawData(requestPath, season, league);
prov.writeRoundsToS3(season, league, content);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (Exception exception) {
@@ -93,46 +96,21 @@ public class APIFootballUpdater {
}
}
public String getRawData(String requestUrl) throws Exception {
public String getRawData(String requestPath, int season, int league) throws Exception {
HttpClient client = HttpClientBuilder.create().build();
String requestUrl = baseurl + requestPath + "?season=" + (season-1) + "&league=" + league + "&timezone=Europe/Berlin";
HttpGet request = new HttpGet(requestUrl);
// add request header
request.addHeader("X-RapidAPI-Key", "a607e6a7437d9d52f3ac73e0b2704d0b");
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
assert response != null;
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())
);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
request.addHeader("x-rapidapi-host", "v3.football.api-sports.io");
request.addHeader("x-rapidapi-key", "a607e6a7437d9d52f3ac73e0b2704d0b");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = "";
while (true) {
try {
line = rd.readLine();
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}
// Stop reading if last line was found.
if (line == null) break;
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
checkErrors(requestUrl, result.toString());
@@ -140,7 +118,7 @@ public class APIFootballUpdater {
}
public HashSet<Integer> getLeagues(int season) {
JSONArray leaguesArray = ResourceProvider.getLigenConfig(season);
JSONArray leaguesArray = ResourceProvider.getLigenConfig();
HashSet<Integer> leagues = new HashSet<>();
for (Object leagueObject : leaguesArray) {
JSONObject leagueJSONObject = (JSONObject) leagueObject;
@@ -152,10 +130,10 @@ public class APIFootballUpdater {
public void checkErrors(String requestUrl, String result) throws Exception {
JSONObject resultObject = stringToJSONObject(result);
boolean containsError = ((JSONObject) resultObject.get("api")).containsKey("error");
int results = Integer.parseInt(((JSONObject) resultObject.get("api")).get("results").toString());
boolean containsError = !((JSONArray) resultObject.get("errors")).isEmpty();
int results = Integer.parseInt(resultObject.get("results").toString());
if(containsError) {
String errorMessage = ((JSONObject) resultObject.get("api")).get("error").toString();
String errorMessage = resultObject.get("errors").toString();
throw new Exception(requestUrl + " returned error: '" + errorMessage + "'");
} else if (results == 0) {
String statusMessage = ((JSONObject) resultObject.get("api")).get("status").toString();