Remove Gist
This commit is contained in:
@@ -1,29 +1,22 @@
|
||||
package de.jeyp91.apifootball;
|
||||
|
||||
import de.jeyp91.gists.GistProvider;
|
||||
import de.jeyp91.S3Provider;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class APIFootballConnector {
|
||||
|
||||
private static APIFootballConnector conn = null;
|
||||
private final int season;
|
||||
private final String gistId;
|
||||
private final HashMap<String, JSONObject> rounds = new HashMap<>();
|
||||
private final HashMap<String, JSONObject> matches = new HashMap<>();
|
||||
private final HashMap<Integer, JSONObject> rounds = new HashMap<>();
|
||||
private final HashMap<Integer, JSONObject> matches = new HashMap<>();
|
||||
|
||||
private APIFootballConnector(int season) {
|
||||
this.season = season;
|
||||
GistProvider prov = GistProvider.getInstance();
|
||||
this.gistId = prov.getGistID("APIFootball_" + (season + 1));
|
||||
}
|
||||
|
||||
public static APIFootballConnector getAPIFootballConnectorInstance(int season) {
|
||||
@@ -62,14 +55,14 @@ public class APIFootballConnector {
|
||||
return matchesOfMatchday;
|
||||
}
|
||||
|
||||
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int id) {
|
||||
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int leagueId) {
|
||||
ArrayList<APIFootballMatch> matchesList = new ArrayList<>();
|
||||
S3Provider prov = new S3Provider();
|
||||
|
||||
String filename = "matches_league_" + id + ".json";
|
||||
if(!this.matches.containsKey(filename)) {
|
||||
this.matches.put(filename, readObjectFromFile(filename));
|
||||
if(!this.matches.containsKey(leagueId)) {
|
||||
this.matches.put(leagueId, prov.getFixturesJSONFromS3(leagueId));
|
||||
}
|
||||
JSONObject matches = this.matches.get(filename);
|
||||
JSONObject matches = this.matches.get(leagueId);
|
||||
JSONArray matchArray = (JSONArray) (((JSONObject)matches.get("api")).get("fixtures"));
|
||||
|
||||
for(int i = 0; i < matchArray.size(); i++) {
|
||||
@@ -79,52 +72,17 @@ public class APIFootballConnector {
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
public JSONObject getMatchdays(int id) {
|
||||
|
||||
String filename = "rounds_" + id + ".json";
|
||||
if(!this.rounds.containsKey(filename)) {
|
||||
this.rounds.put(filename, readObjectFromFile(filename));
|
||||
public JSONObject getMatchdays(int leagueId) {
|
||||
S3Provider prov = new S3Provider();
|
||||
if(!this.rounds.containsKey(leagueId)) {
|
||||
this.rounds.put(leagueId, prov.getRoundsJSONFromS3(leagueId));
|
||||
}
|
||||
return this.rounds.get(filename);
|
||||
}
|
||||
|
||||
public JSONObject readObjectFromFile(String filename) {
|
||||
|
||||
JSONObject object = null;
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
|
||||
GistProvider prov = GistProvider.getInstance();
|
||||
String jsonConfig = prov.getFileFromGist(filename);
|
||||
|
||||
try {
|
||||
object = (JSONObject) jsonParser.parse(jsonConfig);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
public JSONArray readArrayFromFile(String filename) {
|
||||
|
||||
JSONArray object = null;
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
|
||||
GistProvider prov = GistProvider.getInstance();
|
||||
String jsonConfig = prov.getFileFromGist(filename);
|
||||
|
||||
try {
|
||||
object = (JSONArray) jsonParser.parse(jsonConfig);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return object;
|
||||
return this.rounds.get(leagueId);
|
||||
}
|
||||
|
||||
public JSONObject getTeamsForLeague(int league) {
|
||||
String url = "https://v2.api-football.com/teams/league/" + league;
|
||||
String content = new APIFootballUpdater(0).getRawData(url);
|
||||
String content = new APIFootballUpdater().getRawData(url);
|
||||
return stringToJSONObject(content);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +1,18 @@
|
||||
package de.jeyp91.apifootball;
|
||||
|
||||
import com.google.common.io.Resources;
|
||||
import de.jeyp91.BaseMatch;
|
||||
import de.jeyp91.gists.GistProvider;
|
||||
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.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class APIFootballMatch extends BaseMatch {
|
||||
|
||||
private final int season;
|
||||
private final int matchId;
|
||||
private final int leagueId;
|
||||
private String matchStatus;
|
||||
private final String teamNameHome;
|
||||
private final String teamNameGuest;
|
||||
private Boolean showTable = null;
|
||||
|
||||
public APIFootballMatch(JSONObject json, int season) {
|
||||
this.season = season;
|
||||
this.matchId = Integer.parseInt(json.get("fixture_id").toString());
|
||||
// TODO
|
||||
this.leagueId = Integer.parseInt(json.get("league_id").toString());
|
||||
@@ -33,8 +22,7 @@ public class APIFootballMatch extends BaseMatch {
|
||||
this.teamNameGuest = ((JSONObject) json.get("awayTeam")).get("team_name").toString();
|
||||
this.goalsHome = getNumberOrNull(json.get("goalsHomeTeam"));
|
||||
this.goalsGuest = getNumberOrNull(json.get("goalsAwayTeam"));
|
||||
this.matchday = getMatchdayFromRoundString(json.get("round").toString(), this.leagueId);
|
||||
this.matchStatus = json.get("statusShort").toString();
|
||||
this.matchday = getMatchdayFromRoundString(season, json.get("round").toString(), this.leagueId);
|
||||
this.matchDatetime = (String) json.get("event_date");
|
||||
}
|
||||
|
||||
@@ -42,10 +30,10 @@ public class APIFootballMatch extends BaseMatch {
|
||||
return this.matchId;
|
||||
}
|
||||
|
||||
private int getMatchdayFromRoundString(String round, int leagueId) {
|
||||
public static int getMatchdayFromRoundString(int season, String round, int leagueId) {
|
||||
round = round.replace(" ", "_");
|
||||
Integer matchday = null;
|
||||
APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(this.season);
|
||||
APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(season);
|
||||
JSONObject roundsObject = con.getMatchdays(leagueId);
|
||||
JSONArray roundsArray = (JSONArray)(((JSONObject) roundsObject.get("api")).get("fixtures"));
|
||||
for (int i = 0; i < roundsArray.size(); i++) {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package de.jeyp91.apifootball;
|
||||
|
||||
import de.jeyp91.gists.GistProvider;
|
||||
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.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
@@ -12,51 +14,42 @@ import java.util.HashSet;
|
||||
|
||||
public class APIFootballUpdater {
|
||||
|
||||
private final int season;
|
||||
private final 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 APIFootballUpdater() {
|
||||
|
||||
}
|
||||
|
||||
public void updateFixtures(int league) throws IOException {
|
||||
public void updateFixtures(int league) {
|
||||
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);
|
||||
S3Provider prov = new S3Provider();
|
||||
prov.writeFixturesToS3(league, content);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAllFixtures() throws IOException {
|
||||
HashSet<Integer> leagues = provider.getLeagues();
|
||||
public void updateAllFixtures() {
|
||||
HashSet<Integer> leagues = getLeagues();
|
||||
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() throws IOException {
|
||||
HashSet<Integer> leagues = provider.getLeagues();
|
||||
public void updateAllRounds() {
|
||||
HashSet<Integer> leagues = getLeagues();
|
||||
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);
|
||||
public void updateRounds(int league) {
|
||||
String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league;
|
||||
String content = getRawData(apiFootballUrl);
|
||||
if(!content.equals(this.exceededLimitError)) {
|
||||
S3Provider prov = new S3Provider();
|
||||
prov.writeRoundsToS3(league, content);
|
||||
}
|
||||
}
|
||||
|
||||
public String getRawData(String requestUrl) {
|
||||
@@ -103,4 +96,15 @@ public class APIFootballUpdater {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user