master
Benjamin Kraft 2 years ago
parent 521917cd68
commit a3f03395c7
  1. 29
      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 <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();
}
}
// TODO fetch testInput from first <code>-Tag in HTML
std::ifstream file(localFilePath);
string line;

Loading…
Cancel
Save