Files
tlw-database-tool/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java
T
2025-01-26 23:01:35 +01:00

157 lines
5.8 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import de.jeyp91.ResourceProvider;
import de.jeyp91.S3Provider;
public class APIFootballUpdater {
private static final Logger logger = LoggerFactory.getLogger(APIFootballUpdater.class);
private static final String baseurl = "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(season);
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(season);
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 {
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-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(int season) {
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) {
String statusMessage = ((JSONObject) resultObject.get("api")).get("status").toString();
throw new Exception(requestUrl + " did not have any results with status: '" + statusMessage + "'");
}
}
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;
}
}