From 42b6917614a9362ec832e99308d47fdbcd55ec8b Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] feat: fetch_input mix task --- 2021/.gitignore | 2 + 2021/config/config.exs | 4 ++ 2021/config/runtime.exs | 3 ++ 2021/lib/advent_of_code.ex | 2 + 2021/lib/mix/advent_of_code.ex | 1 + .../mix/tasks/advent_of_code.fetch_input.ex | 54 +++++++++++++++++++ 2021/mix.exs | 1 + 2021/mix.lock | 6 +++ 8 files changed, 73 insertions(+) create mode 100644 2021/config/config.exs create mode 100644 2021/config/runtime.exs create mode 100644 2021/lib/mix/tasks/advent_of_code.fetch_input.ex diff --git a/2021/.gitignore b/2021/.gitignore index 7eeb530..b7baa17 100644 --- a/2021/.gitignore +++ b/2021/.gitignore @@ -25,3 +25,5 @@ advent_of_code-*.tar # Temporary files for e.g. tests /tmp + +.envrc diff --git a/2021/config/config.exs b/2021/config/config.exs new file mode 100644 index 0000000..b319e95 --- /dev/null +++ b/2021/config/config.exs @@ -0,0 +1,4 @@ +import Config + +config :advent_of_code, + event: "2021" diff --git a/2021/config/runtime.exs b/2021/config/runtime.exs new file mode 100644 index 0000000..3a98dbe --- /dev/null +++ b/2021/config/runtime.exs @@ -0,0 +1,3 @@ +import Config + +config :advent_of_code, session: System.get_env("ADVENT_OF_CODE_SESSION") diff --git a/2021/lib/advent_of_code.ex b/2021/lib/advent_of_code.ex index 42478ec..26304d6 100644 --- a/2021/lib/advent_of_code.ex +++ b/2021/lib/advent_of_code.ex @@ -16,6 +16,8 @@ defmodule AdventOfCode do Module.concat(AdventOfCode, Macro.camelize("Day#{day_number(day)}")) end + def day_input_path(day), do: "priv/inputs/#{day_number(day)}.input" + defp day_number(day), do: String.pad_leading(day, 2, "0") defp part_number(part), do: String.trim_leading(part, "0") end diff --git a/2021/lib/mix/advent_of_code.ex b/2021/lib/mix/advent_of_code.ex index 8e8cd91..3776ee2 100644 --- a/2021/lib/mix/advent_of_code.ex +++ b/2021/lib/mix/advent_of_code.ex @@ -5,4 +5,5 @@ defmodule Mix.AdventOfCode do defdelegate day_module(day), to: AdventOfCode defdelegate part_module(day, part), to: AdventOfCode + defdelegate day_input_path(day), to: AdventOfCode end diff --git a/2021/lib/mix/tasks/advent_of_code.fetch_input.ex b/2021/lib/mix/tasks/advent_of_code.fetch_input.ex new file mode 100644 index 0000000..33e0f2a --- /dev/null +++ b/2021/lib/mix/tasks/advent_of_code.fetch_input.ex @@ -0,0 +1,54 @@ +defmodule Mix.Tasks.AdventOfCode.FetchInput do + use Mix.Task + + import Mix.Generator + import Mix.AdventOfCode + + @shortdoc "Fetches the inputs for day and saves them in `priv/inputs`" + + @moduledoc """ + #{@shortdoc}. + + ## Examples + + # Fetch day 1's inputs + $ mix advent_of_code.fetch_input 1 + """ + + @finch Mix.AdventOfCode.FetchInput.Finch + + @switches [event: :string, session: :string] + + @impl Mix.Task + def run(args) do + Mix.Task.run("app.config") + :ok = Application.ensure_started(:telemetry) + {:ok, _pid} = Finch.start_link(name: @finch) + + default_event = Application.get_env(:advent_of_code, :event) + default_session = Application.get_env(:advent_of_code, :session) + + case OptionParser.parse!(args, strict: @switches) do + {opts, [day]} -> + input_file_path = day_input_path(day) + + if overwrite?(input_file_path) do + event = Keyword.get(opts, :event, default_event) + session = Keyword.get(opts, :session, default_session) + + {:ok, %{body: data}} = + Finch.build( + :get, + "https://adventofcode.com/#{event}/day/#{day}/input", + [{"Cookie", "session=#{session}"}] + ) + |> Finch.request(@finch) + + create_file(input_file_path, data, force: true) + end + + _ -> + Mix.raise("Unexpected arguments.") + end + end +end diff --git a/2021/mix.exs b/2021/mix.exs index a8ad075..08075e1 100644 --- a/2021/mix.exs +++ b/2021/mix.exs @@ -25,6 +25,7 @@ defmodule AdventOfCode.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ + {:finch, "~> 0.9.0"}, {:progress_bar, "~> 2.0"} ] end diff --git a/2021/mix.lock b/2021/mix.lock index e43918e..c1a4815 100644 --- a/2021/mix.lock +++ b/2021/mix.lock @@ -1,4 +1,10 @@ %{ + "castore": {:hex, :castore, "0.1.13", "ccf3ab251ffaebc4319f41d788ce59a6ab3f42b6c27e598ad838ffecee0b04f9", [:mix], [], "hexpm", "a14a7eecfec7e20385493dbb92b0d12c5d77ecfd6307de10102d58c94e8c49c0"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, + "finch": {:hex, :finch, "0.9.0", "8b772324aebafcaba763f1dffaa3e7f52f8c4e52485f50f48bbb2f42219a2e87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a93bfcad9ca50fa3cb2d459f27667d9a87cfbb7fecf9b29b2e78a50bc2ab445d"}, + "mint": {:hex, :mint, "1.4.0", "cd7d2451b201fc8e4a8fd86257fb3878d9e3752899eb67b0c5b25b180bde1212", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "10a99e144b815cbf8522dccbc8199d15802440fc7a64d67b6853adb6fa170217"}, + "nimble_options": {:hex, :nimble_options, "0.3.7", "1e52dd7673d36138b1a5dede183b5d86dff175dc46d104a8e98e396b85b04670", [:mix], [], "hexpm", "2086907e6665c6b6579be54ef5001928df5231f355f71ed258f80a55e9f63633"}, + "nimble_pool": {:hex, :nimble_pool, "0.2.4", "1db8e9f8a53d967d595e0b32a17030cdb6c0dc4a451b8ac787bf601d3f7704c3", [:mix], [], "hexpm", "367e8071e137b787764e6a9992ccb57b276dc2282535f767a07d881951ebeac6"}, "progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"}, + "telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"}, }