|
|
@ -11,18 +11,29 @@ Input getInput(int day, string key, bool useTestInput) { |
|
|
|
string postfix = useTestInput ? "_testInput.txt" : "_input.txt"; |
|
|
|
string postfix = useTestInput ? "_testInput.txt" : "_input.txt"; |
|
|
|
string localFilePath = "input/" + string(dayStrPadded) + postfix; |
|
|
|
string localFilePath = "input/" + string(dayStrPadded) + postfix; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::filesystem::remove(localFilePath); |
|
|
|
|
|
|
|
|
|
|
|
if (!std::filesystem::exists({localFilePath})) { |
|
|
|
if (!std::filesystem::exists({localFilePath})) { |
|
|
|
|
|
|
|
string dayStr = std::to_string(day); |
|
|
|
|
|
|
|
string url = "https://adventofcode.com/2022/day/" + dayStr; |
|
|
|
|
|
|
|
auto cookies = cpr::Cookies{{"session", key}}; |
|
|
|
|
|
|
|
if (!useTestInput){ |
|
|
|
if (key.empty()) |
|
|
|
if (key.empty()) |
|
|
|
return input; |
|
|
|
return input; |
|
|
|
string dayStr = std::to_string(day); |
|
|
|
url += "/input"; |
|
|
|
string url = "https://adventofcode.com/2022/day/" + dayStr + "/input"; |
|
|
|
|
|
|
|
cout << "Input does not exist. Fetching from " + url << endl; |
|
|
|
cout << "Input does not exist. Fetching from " + url << endl; |
|
|
|
std::ofstream file(localFilePath); |
|
|
|
std::ofstream file(localFilePath); |
|
|
|
file << cpr::Get(cpr::Url{url}, cpr::Cookies{{"session", key}}).text; |
|
|
|
file << cpr::Get(cpr::Url{url}, cookies).text; |
|
|
|
file.close(); |
|
|
|
file.close(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
// TODO fetch testInput from first <code>-Tag in HTML
|
|
|
|
// TODO fetch testInput from first <code>-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(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::ifstream file(localFilePath); |
|
|
|
std::ifstream file(localFilePath); |
|
|
|
|
|
|
|
|
|
|
|