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

@@ -71,7 +71,7 @@ public class APIFootballConnector {
return matchesList;
}
private JSONObject readFromFile(String filename) {
public JSONObject readFromFile(String filename) {
JSONObject object = null;
//JSON parser object to parse read file

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);

View File

@@ -1,11 +1,13 @@
package de.jeyp91.gists;
import com.google.api.client.util.DateTime;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders;
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.client.methods.HttpPatch;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -15,6 +17,7 @@ import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Set;
@@ -33,8 +36,94 @@ public class GistProvider {
return stringToJSONArray(gistsRaw);
}
public String getTimestamp() {
return "";
public DateTime getGistUpdatedTimestamp(String gistDescription) {
DateTime updated = null;
for (Object o : this.gistList) {
JSONObject gist = (JSONObject) o;
String desc = gist.get("description").toString();
if (gistDescription.equals(desc)) {
updated = new DateTime(gist.get("updated_at").toString());
}
}
return updated;
}
public String getGistID(String gistDescription) {
String id = null;
for (Object o : this.gistList) {
JSONObject gist = (JSONObject) o;
String desc = gist.get("description").toString();
if (gistDescription.equals(desc)) {
id = gist.get("id").toString();
}
}
return id;
}
public String updateGist(String id, String file, String content) throws UnsupportedEncodingException {
String requestUrl = "https://api.github.com/gists/" + id;
String username = "tippligawuerzburg";
String password = "1154f448eae29212971cb82ca8850f54bff23629";
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
HttpClient client = HttpClientBuilder
.create()
.build();
HttpPatch request = new HttpPatch(requestUrl);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
request.setHeader(HttpHeaders.ACCEPT, "application/vnd.github.v3+json");
JSONObject contentObject = new JSONObject();
contentObject.put("content", content);
JSONObject fileObject = new JSONObject();
fileObject.put(file, contentObject);
JSONObject filesObject = new JSONObject();
filesObject.put("files", fileObject);
String bodyString = filesObject.toString();
StringEntity body = new StringEntity(filesObject.toString());
body.setContentType("application/json");
body.setContentEncoding("UTF-8");
request.setEntity(body);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
/* TODO */
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)
);
} 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();
}
public JSONArray getTeamIdMatcher() {
@@ -68,8 +157,8 @@ public class GistProvider {
private String getRawData(String requestUrl) {
String username = "jeyp91";
String password = "e73485180c52e66e7e6f5550bb53aae3ff5c745a";
String username = "tippligawuerzburg";
String password = "1154f448eae29212971cb82ca8850f54bff23629";
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1));

View File

@@ -1,8 +1,13 @@
package de.jeyp91.gists;
import com.google.api.client.util.DateTime;
import de.jeyp91.apifootball.APIFootballConnector;
import de.jeyp91.apifootball.APIFootballUpdater;
import org.json.simple.JSONArray;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
public class GistProviderTest {
@Test
@@ -25,4 +30,27 @@ public class GistProviderTest {
JSONArray matcher = prov.getTeamIdMatcher();
System.out.println(matcher);
}
@Test
public void getGistUpdatedTimestampTest() {
GistProvider prov = new GistProvider();
DateTime date = prov.getGistUpdatedTimestamp("Matches");
System.out.println(date);
}
@Test
public void getGistIDTest() {
GistProvider prov = new GistProvider();
String id = prov.getGistID("Matches");
System.out.println(id);
}
@Test
public void updateGistTest() throws UnsupportedEncodingException {
GistProvider prov = new GistProvider();
APIFootballUpdater updater = new APIFootballUpdater(2021);
String body = updater.getRawData("https://v2.api-football.com/fixtures/league/2738?timezone=Europe/Berlin");
String content = prov.updateGist("6ec51d59cac70c9f4c040eeea1c0cdd9", "matches_league_2738.json", body);
System.out.println(content);
}
}