First version with ability to create database for new season
This commit is contained in:
170
src/main/java/de/jeyp91/apifootball/APIFootballConnector.java
Normal file
170
src/main/java/de/jeyp91/apifootball/APIFootballConnector.java
Normal file
@@ -0,0 +1,170 @@
|
||||
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 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;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class APIFootballConnector {
|
||||
|
||||
private final String API_FOOTBALL_URL = "https://v2.api-football.com/";
|
||||
private int season;
|
||||
|
||||
public APIFootballConnector(int season) {
|
||||
this.season = season;
|
||||
}
|
||||
|
||||
public APIFootballMatch getMatchDataByLeagueAndMatchID(int league, int id) {
|
||||
|
||||
APIFootballMatch matchWithID = null;
|
||||
|
||||
ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(league);
|
||||
for (APIFootballMatch singleMatch : allMatches) {
|
||||
if (singleMatch.getAPIFootBallMatchID() == id) {
|
||||
matchWithID = singleMatch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return matchWithID;
|
||||
}
|
||||
|
||||
public ArrayList<APIFootballMatch> getMatchDataByLeagueAndMatchday(int league, int matchday) {
|
||||
|
||||
ArrayList<APIFootballMatch> matchesOfMatchday = new ArrayList<>();
|
||||
|
||||
ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(league);
|
||||
for (APIFootballMatch singleMatch : allMatches) {
|
||||
if (singleMatch.getMatchday() == matchday) {
|
||||
matchesOfMatchday.add(singleMatch);
|
||||
}
|
||||
}
|
||||
|
||||
return matchesOfMatchday;
|
||||
}
|
||||
|
||||
private ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int id) {
|
||||
ArrayList<APIFootballMatch> matchesList = new ArrayList<>();
|
||||
|
||||
JSONObject matches = readFromFile("matches_league_" + id);
|
||||
JSONArray matchArray = (JSONArray) (((JSONObject)matches.get("api")).get("fixtures"));
|
||||
|
||||
for(int i = 0; i < matchArray.size(); i++) {
|
||||
matchesList.add(new APIFootballMatch((JSONObject) matchArray.get(i), this.season));
|
||||
}
|
||||
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
private JSONObject readFromFile(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;
|
||||
|
||||
try {
|
||||
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
|
||||
//Read JSON file
|
||||
object = (JSONObject) jsonParser.parse(jsonConfig);
|
||||
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
private JSONArray getDataAsJSONArray(String requestUrl) {
|
||||
|
||||
String rawData = getRawData(requestUrl);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONArray data = null;
|
||||
try {
|
||||
data = (JSONArray) parser.parse(rawData);
|
||||
} catch (ParseException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private JSONObject getDataAsJSONObject(String requestUrl) {
|
||||
|
||||
String rawData = getRawData(requestUrl);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject data = null;
|
||||
try {
|
||||
data = (JSONObject) parser.parse(rawData);
|
||||
} catch (ParseException e) {
|
||||
/* TODO */
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
String getRawData(String requestUrl) {
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(requestUrl);
|
||||
|
||||
// add request header
|
||||
request.addHeader("Content-Type", "application/json");
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user