145 lines
5.0 KiB
Java
145 lines
5.0 KiB
Java
package de.jeyp91.apifootball;
|
|
|
|
import com.google.common.io.Resources;
|
|
import de.jeyp91.gists.GistProvider;
|
|
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.*;
|
|
import java.util.HashSet;
|
|
|
|
public class APIFootballUpdater {
|
|
|
|
private int season;
|
|
private GistProvider provider;
|
|
|
|
private final String exceededLimitError = "{\"api\":{\"results\":0,\"error\":\"You have reached the request limit for the day\"}}";
|
|
|
|
public APIFootballUpdater(int season) {
|
|
this.season = season;
|
|
this.provider = GistProvider.getInstance();
|
|
}
|
|
|
|
public void updateFixtures(int league) throws IOException {
|
|
String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin";
|
|
String filename = "matches_league_" + league + ".json";
|
|
String content = getRawData(apiFootballUrl);
|
|
if(!content.equals(this.exceededLimitError)) {
|
|
writeStringToGist(filename, content);
|
|
}
|
|
}
|
|
|
|
public void updateAllFixtures(String configPath) throws IOException {
|
|
HashSet<Integer> leagues = getAllLeaguesFromLeagueConfig(configPath);
|
|
for (Integer league : leagues) {
|
|
updateFixtures(league);
|
|
}
|
|
}
|
|
|
|
public void updateRounds(int league) throws IOException {
|
|
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
|
|
String filename = "rounds_" + league + ".json";
|
|
String content = getRawData(apiFootballUrl);
|
|
if(!content.equals(this.exceededLimitError)) {
|
|
writeStringToGist(filename, content);
|
|
}
|
|
}
|
|
|
|
public void updateAllRounds(String configPath) throws IOException {
|
|
HashSet<Integer> leagues = getAllLeaguesFromLeagueConfig(configPath);
|
|
for (Integer league : leagues) {
|
|
updateRounds(league);
|
|
}
|
|
}
|
|
|
|
private void writeStringToGist(String filename, String content) throws UnsupportedEncodingException {
|
|
String id = this.provider.getGistID("APIFootball_" + this.season);
|
|
this.provider.updateGist(id, filename, content);
|
|
}
|
|
|
|
private HashSet<Integer> getAllLeaguesFromLeagueConfig(String config) {
|
|
|
|
HashSet<Integer> leagues = new HashSet<>();
|
|
|
|
JSONParser jsonParser = new JSONParser();
|
|
GistProvider prov = GistProvider.getInstance();
|
|
String jsonConfig = prov.getTippligaConfig(config);
|
|
JSONObject configObject = null;
|
|
try {
|
|
configObject = (JSONObject) jsonParser.parse(jsonConfig);
|
|
} catch (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;
|
|
}
|
|
|
|
public 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();
|
|
}
|
|
}
|