elixir-amazon-history/lib/amazon_history/cli.ex

62 lines
1.4 KiB
Elixir
Raw Permalink Normal View History

2018-04-07 08:07:31 +00:00
defmodule AmazonHistory.CLI do
@moduledoc """
Handle the command-line parsing for the amazon_history tool
"""
2018-04-07 11:29:36 +00:00
require Logger
2018-04-07 08:07:31 +00:00
def run(argv) do
argv
|> parse_args
2018-04-07 11:29:36 +00:00
|> scrape
|> CSV.encode
|> Enum.each(&IO.write/1)
2018-04-07 08:07:31 +00:00
end
2018-04-07 11:29:36 +00:00
def scrape(email: email, password: password, start_year: start_year) do
Logger.debug("Will scrape with email: #{email}, password: ****")
AmazonHistory.Scraper.fetch(email, password, start_year)
2018-04-07 08:07:31 +00:00
end
2018-04-07 11:29:36 +00:00
def scrape(_) do
IO.puts(:stderr, "Usage: --email <email> --password <password> --start-year <start year>")
2018-04-07 08:07:31 +00:00
end
@doc """
Options:
2018-04-07 11:29:36 +00:00
-e/--email: Amazon email
2018-04-07 08:07:31 +00:00
-p/--password: Amazon password
"""
def parse_args(argv) do
OptionParser.parse(
argv,
switches: [
help: :boolean,
2018-04-07 11:29:36 +00:00
email: :string,
password: :string,
start_year: :integer,
2018-04-07 08:07:31 +00:00
],
aliases: [
h: :help,
2018-04-07 11:29:36 +00:00
e: :email,
p: :password,
y: :start_year,
2018-04-07 08:07:31 +00:00
]
)
|> elem(0)
|> args_to_internal_representation
end
2018-04-07 11:29:36 +00:00
defp args_to_internal_representation(email: email, password: password, start_year: start_year) do
[email: email, password: password, start_year: start_year]
end
defp args_to_internal_representation(email: email, password: password) do
[email: email, password: password, start_year: 2000]
2018-04-07 08:07:31 +00:00
end
defp args_to_internal_representation(_) do
:help
end
end