diff --git a/.gitignore b/.gitignore index 5e0864dbf43df3ae4c635e0b9408d0df08ca94e0..81a8e5be541864a49100a83ff0bf264cc3fe439a 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 c6459ae7c58d3fe8cdd4e984f1b20cd66eda76cb..4f404dbe38bf29604891b46ba3645d8d9febf2ae 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);