Finish gist
This commit is contained in:
@@ -1,32 +1,36 @@
|
||||
package de.jeyp91.apifootball;
|
||||
|
||||
import com.google.common.io.Resources;
|
||||
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 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.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class APIFootballConnector {
|
||||
|
||||
private int season;
|
||||
private static APIFootballConnector conn = null;
|
||||
private final int season;
|
||||
private final String gistId;
|
||||
private HashMap<String, JSONObject> rounds = new HashMap<>();
|
||||
private HashMap<String, JSONObject> matches = new HashMap<>();
|
||||
|
||||
public APIFootballConnector(int season) {
|
||||
private APIFootballConnector(int season) {
|
||||
this.season = season;
|
||||
GistProvider prov = GistProvider.getInstance();
|
||||
this.gistId = prov.getGistID("APIFootball_" + (season + 1));
|
||||
}
|
||||
|
||||
public static APIFootballConnector getAPIFootballConnectorInstance(int season) {
|
||||
if(conn == null) {
|
||||
conn = new APIFootballConnector(season);
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
public APIFootballMatch getMatchDataByLeagueAndMatchID(int league, int id) {
|
||||
@@ -58,10 +62,14 @@ public class APIFootballConnector {
|
||||
return matchesOfMatchday;
|
||||
}
|
||||
|
||||
private ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int id) {
|
||||
public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int id) {
|
||||
ArrayList<APIFootballMatch> matchesList = new ArrayList<>();
|
||||
|
||||
JSONObject matches = readFromFile("matches_league_" + id);
|
||||
String filename = "matches_league_" + id + ".json";
|
||||
if(!this.matches.containsKey(filename)) {
|
||||
this.matches.put(filename, readObjectFromFile(filename));
|
||||
}
|
||||
JSONObject matches = this.matches.get(filename);
|
||||
JSONArray matchArray = (JSONArray) (((JSONObject)matches.get("api")).get("fixtures"));
|
||||
|
||||
for(int i = 0; i < matchArray.size(); i++) {
|
||||
@@ -71,20 +79,43 @@ public class APIFootballConnector {
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
public JSONObject readFromFile(String filename) {
|
||||
public JSONObject getMatchdays(int id) {
|
||||
|
||||
String filename = "rounds_" + id + ".json";
|
||||
if(!this.rounds.containsKey(filename)) {
|
||||
this.rounds.put(filename, readObjectFromFile(filename));
|
||||
}
|
||||
return this.rounds.get(filename);
|
||||
}
|
||||
|
||||
public JSONObject readObjectFromFile(String filename) {
|
||||
|
||||
JSONObject object = null;
|
||||
//JSON parser object to parse read file
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
URL url = Resources.getResource((this.season + 1) + "\\API-Football\\" + filename + ".json");
|
||||
String jsonConfig = null;
|
||||
|
||||
GistProvider prov = GistProvider.getInstance();
|
||||
String jsonConfig = prov.getFileFromGist(this.gistId, filename);
|
||||
|
||||
try {
|
||||
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
|
||||
//Read JSON file
|
||||
object = (JSONObject) jsonParser.parse(jsonConfig);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (IOException | ParseException e) {
|
||||
return object;
|
||||
}
|
||||
|
||||
public JSONArray readArrayFromFile(String filename) {
|
||||
|
||||
JSONArray object = null;
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
|
||||
GistProvider prov = GistProvider.getInstance();
|
||||
String jsonConfig = prov.getFileFromGist(this.gistId, filename);
|
||||
|
||||
try {
|
||||
object = (JSONArray) jsonParser.parse(jsonConfig);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user