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