advent_of_code/lib/get_input.ex
Caleb Webber 5f3ac1eb76 add get_input task
call with `mix get_input <year> <day>` and it
will write to `input/<year>_<day>.txt`
2023-12-03 19:06:47 -05:00

23 lines
546 B
Elixir

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