Files
tlw-database-tool/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java
2025-10-27 17:24:54 +01:00

165 lines
6.0 KiB
Java

package de.jeyp91.apifootball;
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;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
public class APIFootballUpdater {
private static final Logger logger = LoggerFactory.getLogger(APIFootballUpdater.class);
private static final String BASE_URL = "https://v3.football.api-sports.io/";
public APIFootballUpdater() {
}
public void updateFixtures(int season, int league) {
String requestPath = "fixtures";
S3Provider prov = new S3Provider();
try {
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(requestPath, season, league);
prov.writeFixturesToS3(season, league, content);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (Exception exception) {
logger.error(e.getMessage());
}
} else {
logger.error(e.getMessage());
}
}
}
public void updateAllFixtures(int season) {
HashSet<Integer> leagues = getLeagues();
for (Integer league : leagues) {
updateFixtures(season, league);
try {
Thread.sleep(1000*6);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void updateAllRounds(int season) {
HashSet<Integer> leagues = getLeagues();
for (Integer league : leagues) {
updateRounds(season, league);
try {
Thread.sleep(1000*6);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void updateRounds(int season, int league) {
String requestPath = "fixtures/rounds";
S3Provider prov = new S3Provider();
try {
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(requestPath, season, league);
prov.writeRoundsToS3(season, league, content);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (Exception exception) {
logger.error(e.getMessage());
}
} else {
logger.error(e.getMessage());
}
}
}
public String getRawData(String requestPath, int season, int league) throws Exception {
if(league != 1 && league != 4) {
/**
* Season usually is one higher in Tippliga than in API-Football.
* e.g. 2023/24 is 2024 in Tippliga and 2023 in API-Football.
* Because Liga Cup in Tippliga is at the end of a season, season does match World Cup (1) and Euro Championship (4)
*/
season--;
}
HttpClient client = HttpClientBuilder.create().build();
String requestUrl = BASE_URL + requestPath + "?season=" + (season) + "&league=" + league + "&timezone=Europe/Berlin";
HttpGet request = new HttpGet(requestUrl);
// add request header
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 ((line = rd.readLine()) != null) {
result.append(line);
}
checkErrors(requestUrl, result.toString());
return result.toString();
}
public HashSet<Integer> getLeagues() {
JSONArray leaguesArray = ResourceProvider.getLigenConfig();
HashSet<Integer> leagues = new HashSet<>();
for (Object leagueObject : leaguesArray) {
JSONObject leagueJSONObject = (JSONObject) leagueObject;
Integer leagueId = ((Long) leagueJSONObject.get("league_id")).intValue();
leagues.add(leagueId);
}
return leagues;
}
public void checkErrors(String requestUrl, String result) throws Exception {
JSONObject resultObject = stringToJSONObject(result);
boolean containsError = !((JSONArray) resultObject.get("errors")).isEmpty();
// int results = Integer.parseInt(resultObject.get("results").toString());
if(containsError) {
String errorMessage = resultObject.get("errors").toString();
throw new Exception(requestUrl + " returned error: '" + errorMessage + "'");
}
// else if (results == 0) {
// throw new Exception(requestUrl + " did not have any results.");
// }
}
private JSONObject stringToJSONObject(String rawData) {
JSONParser parser = new JSONParser();
JSONObject data = null;
try {
data = (JSONObject) parser.parse(rawData);
} catch (Exception e) {
/* TODO */
e.printStackTrace();
}
return data;
}
}