From a086d13430f8a3c1a6fae30b54b875157ec9a10b Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Tue, 6 Dec 2022 08:59:21 -0600
Subject: [PATCH] Renaming to clarify that the 'API key' is really the
 adventofcode.com session cookie

---
 .gitignore                                    |  1 +
 .../{apikeys.TEMPLATE => cookies.TEMPLATE}    |  0
 .../{apikeys.TEMPLATE => cookies.TEMPLATE}    |  0
 scaffolding/ImportData.java                   | 36 +++++++++----------
 4 files changed, 19 insertions(+), 18 deletions(-)
 rename 2021/src/main/resources/{apikeys.TEMPLATE => cookies.TEMPLATE} (100%)
 rename 2022/src/main/resources/{apikeys.TEMPLATE => cookies.TEMPLATE} (100%)

diff --git a/.gitignore b/.gitignore
index 5e0864d..81a8e5b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
 # Project-specific
 ImportData.py
 apikeys.json
+cookies.json
 
 # Mac file finder metadata
 .DS_Store
diff --git a/2021/src/main/resources/apikeys.TEMPLATE b/2021/src/main/resources/cookies.TEMPLATE
similarity index 100%
rename from 2021/src/main/resources/apikeys.TEMPLATE
rename to 2021/src/main/resources/cookies.TEMPLATE
diff --git a/2022/src/main/resources/apikeys.TEMPLATE b/2022/src/main/resources/cookies.TEMPLATE
similarity index 100%
rename from 2022/src/main/resources/apikeys.TEMPLATE
rename to 2022/src/main/resources/cookies.TEMPLATE
diff --git a/scaffolding/ImportData.java b/scaffolding/ImportData.java
index c6459ae..4f404db 100644
--- a/scaffolding/ImportData.java
+++ b/scaffolding/ImportData.java
@@ -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);
-- 
GitLab