Skip to content
Snippets Groups Projects
Commit a086d134 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Renaming to clarify that the 'API key' is really the adventofcode.com session cookie

parent 687effee
No related branches found
No related tags found
No related merge requests found
# Project-specific
ImportData.py
apikeys.json
cookies.json
# Mac file finder metadata
.DS_Store
......
......@@ -19,12 +19,12 @@ import java.util.Objects;
public class ImportData {
public static final String FILENAME = "apikeys.json";
public static final String FILENAME = "cookies.json";
protected final String protocol;
protected final String host;
protected final String path;
protected final String apiKey;
protected final String sessionCookie;
@SuppressWarnings("unused")
public static List<String> readFile(String filename) throws IOException {
......@@ -37,25 +37,25 @@ public class ImportData {
return data;
}
public ImportData(String apiKeyName, int year, int day) {
String apiKey;
public ImportData(String cookieName, int year, int day) {
String cookie;
protocol = "https";
host = "adventofcode.com";
path = "/" + year + "/day/" + day + "/input";
try {
apiKey = getApiKey(apiKeyName);
cookie = getCookie(cookieName);
} catch (IOException ioException) {
System.err.println("Could not retrieve API key: " + ioException.getMessage());
apiKey = null;
System.err.println("Could not retrieve Session Cookie key: " + ioException.getMessage());
cookie = null;
}
this.apiKey = apiKey;
this.sessionCookie = cookie;
}
protected String getApiKey(String apiKeyName) throws IOException {
JSONObject apiKeyJson;
protected String getCookie(String cookieName) throws IOException {
JSONObject cookieJson;
try (InputStreamReader inputStreamReader = new InputStreamReader(
Objects.requireNonNull(ImportData.class.getClassLoader().getResourceAsStream(FILENAME)))) {
apiKeyJson = (JSONObject)new JSONParser().parse(inputStreamReader);
cookieJson = (JSONObject)new JSONParser().parse(inputStreamReader);
} catch (NullPointerException nullPointerException) {
FileNotFoundException newException = new FileNotFoundException("File " + FILENAME + " not found.");
newException.initCause(nullPointerException);
......@@ -63,14 +63,14 @@ public class ImportData {
} catch (ParseException parseException) {
throw new IOException("Error while parsing file " + FILENAME + ".", parseException);
}
if (!apiKeyJson.containsKey(apiKeyName)) {
System.err.println("WARNING! Could not locate API key named " + apiKeyName + " in file " + FILENAME + ".");
if (!cookieJson.containsKey(cookieName)) {
System.err.println("WARNING! Could not locate Cookie named " + cookieName + " in file " + FILENAME + ".");
}
String apiKey = apiKeyJson.get(apiKeyName).toString();
if (apiKey.equals("")) {
System.err.println("WARNING! API key named " + apiKeyName + " in file " + FILENAME + " is blank.");
String cookie = cookieJson.get(cookieName).toString();
if (cookie.equals("")) {
System.err.println("WARNING! Cookie named " + cookieName + " in file " + FILENAME + " is blank.");
}
return apiKey;
return cookie;
}
public List<String> importData() throws IOException {
......@@ -78,7 +78,7 @@ public class ImportData {
BufferedReader bufferedReader;
try {
URLConnection connection = new URI(protocol, host, path, null).toURL().openConnection();
connection.setRequestProperty("Cookie", "session=" + apiKey);
connection.setRequestProperty("Cookie", "session=" + sessionCookie);
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} catch (URISyntaxException | MalformedURLException originalException) {
throw new IOException("Could not retrieve usable data from " + host + ".", originalException);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment