Move fixtures to gist

This commit is contained in:
2020-10-14 23:10:22 +02:00
parent ae2be9c425
commit e4186457e2
4 changed files with 136 additions and 21 deletions

View File

@@ -1,6 +1,7 @@
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;
@@ -11,10 +12,7 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
@@ -22,16 +20,18 @@ import java.util.HashSet;
public class APIFootballUpdater {
private int season;
private GistProvider provider;
public APIFootballUpdater(int season) {
this.season = season;
this.provider = new GistProvider();
}
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);
String resourcePath = this.season + "/API-Football/matches_league_" + league + ".json";
writeStringToFile(content, resourcePath);
writeStringToGist(filename, content);
}
public void updateAllFixtures(String configPath) throws IOException {
@@ -41,6 +41,11 @@ public class APIFootballUpdater {
}
}
private void writeStringToGist(String filename, String content) throws UnsupportedEncodingException {
String id = this.provider.getGistID("Matches_" + this.season);
this.provider.updateGist(id, filename, content);
}
private HashSet<Integer> getAllLeaguesFromLeagueConfig(String configPath) {
HashSet<Integer> leagues = new HashSet<>();
@@ -73,13 +78,6 @@ public class APIFootballUpdater {
return leagues;
}
private void writeStringToFile(String content, String resourcePath) throws IOException {
URL url = Resources.getResource(resourcePath);
FileWriter myWriter = new FileWriter(url.getPath());
myWriter.write(content);
myWriter.close();
}
private JSONArray getDataAsJSONArray(String requestUrl) {
String rawData = getRawData(requestUrl);
@@ -94,7 +92,7 @@ public class APIFootballUpdater {
return data;
}
private JSONObject getDataAsJSONObject(String requestUrl) {
public JSONObject getDataAsJSONObject(String requestUrl) {
String rawData = getRawData(requestUrl);
JSONParser parser = new JSONParser();
@@ -108,7 +106,7 @@ public class APIFootballUpdater {
return data;
}
String getRawData(String requestUrl) {
public String getRawData(String requestUrl) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(requestUrl);