From a3f03395c76bf0e70f349dbbc8994e6fa20fa3de Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 8 Dec 2022 14:15:08 +0100 Subject: [PATCH] start --- src/main.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cbd796c..c85f209 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,19 +11,30 @@ Input getInput(int day, string key, bool useTestInput) { string postfix = useTestInput ? "_testInput.txt" : "_input.txt"; string localFilePath = "input/" + string(dayStrPadded) + postfix; + std::filesystem::remove(localFilePath); + if (!std::filesystem::exists({localFilePath})) { - if (key.empty()) - return input; string dayStr = std::to_string(day); - string url = "https://adventofcode.com/2022/day/" + dayStr + "/input"; - cout << "Input does not exist. Fetching from " + url << endl; - std::ofstream file(localFilePath); - file << cpr::Get(cpr::Url{url}, cpr::Cookies{{"session", key}}).text; - file.close(); + string url = "https://adventofcode.com/2022/day/" + dayStr; + auto cookies = cpr::Cookies{{"session", key}}; + if (!useTestInput){ + if (key.empty()) + return input; + url += "/input"; + cout << "Input does not exist. Fetching from " + url << endl; + std::ofstream file(localFilePath); + file << cpr::Get(cpr::Url{url}, cookies).text; + file.close(); + } else { + // TODO fetch testInput from first -Tag in HTML + std::ofstream file(localFilePath); + auto header = cpr::Header{{"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0"}, + {"Connection", "keep-alive"}}; + file << cpr::Get(cpr::Url{url}, cookies, header).text; + file.close(); + } } - // TODO fetch testInput from first -Tag in HTML - std::ifstream file(localFilePath); string line;