From 5f3ac1eb7668fc0939aed7eb40c402dc6eb55348 Mon Sep 17 00:00:00 2001 From: Caleb Webber Date: Sun, 3 Dec 2023 19:06:46 -0500 Subject: [PATCH] add get_input task call with `mix get_input ` and it will write to `input/_.txt` --- lib/get_input.ex | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/get_input.ex diff --git a/lib/get_input.ex b/lib/get_input.ex new file mode 100644 index 0000000..2081b31 --- /dev/null +++ b/lib/get_input.ex @@ -0,0 +1,23 @@ +defmodule Mix.Tasks.GetInput do + defp get_session() do + {_, content} = File.read("./.session") + content |> String.trim() + end + + defp get_request_headers() do + [Cookie: "session=#{get_session()}"] + end + + def run(args) do + [year, day] = args + write_file = &File.write("./input/#{year}_#{day}.txt", &1) + HTTPoison.start + HTTPoison.get!( + "https://adventofcode.com/#{year}/day/#{day}/input", + get_request_headers() + ) + |> Map.get(:body) + |> then(fn i -> IO.puts(i); i end) + |> write_file.() + end +end