package de.jeyp91.apifootball; import de.jeyp91.ResourceProvider; import de.jeyp91.S3Provider; 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.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import java.io.*; import java.util.HashSet; public class APIFootballUpdater { private static final Logger logger = LogManager.getLogger(APIFootballUpdater.class); public APIFootballUpdater() { } public void updateFixtures(int league) { String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin"; S3Provider prov = new S3Provider(); try { String content = getRawData(apiFootballUrl); prov.writeFixturesToS3(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); } catch (InterruptedException interruptedException) { interruptedException.printStackTrace(); } catch (Exception exception) { logger.error(e.getMessage()); } } else { logger.error(e.getMessage()); } } } public void updateAllFixtures(int season) { HashSet leagues = getLeagues(season); for (Integer league : leagues) { updateFixtures(league); try { Thread.sleep(1000*6); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public void updateAllRounds(int season) { HashSet leagues = getLeagues(season); for (Integer league : leagues) { updateRounds(league); try { Thread.sleep(1000*6); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public void updateRounds(int league) { String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league; S3Provider prov = new S3Provider(); try { String content = getRawData(apiFootballUrl); prov.writeRoundsToS3(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); } catch (InterruptedException interruptedException) { interruptedException.printStackTrace(); } catch (Exception exception) { logger.error(e.getMessage()); } } else { logger.error(e.getMessage()); } } } public String getRawData(String requestUrl) throws Exception { 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 (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(); } 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; result.append(line); } checkErrors(requestUrl, result.toString()); return result.toString(); } public HashSet getLeagues(int season) { JSONArray leaguesArray = ResourceProvider.getLigenConfig(season); HashSet 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 = ((JSONObject) resultObject.get("api")).containsKey("error"); int results = Integer.parseInt(((JSONObject) resultObject.get("api")).get("results").toString()); if(containsError) { String errorMessage = ((JSONObject) resultObject.get("api")).get("error").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; } }