-
Notifications
You must be signed in to change notification settings - Fork 6
/
gs.fetch.ex
70 lines (53 loc) · 1.96 KB
/
gs.fetch.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
defmodule Mix.Tasks.Gs.Fetch do
use Mix.Task
require Logger
alias GoogleSheets.Loader.Docs
@shortdoc "Fetch Google Spreadsheet and save raw CSV to disk"
@moduledoc """
Loads a Google spreadsheet and all worksheets in CSV format for the given document key.
If no parameters are given, it looks through all configured spreadsheets in :google_sheets configuration.
## Examples
mix gs.fetch -u https://spreadsheets.google.com/feeds/worksheets/1k-N20RmT62RyocEu4-MIJm11DZqlZrzV89fGIddDzIs/public/basic -d priv/data
## Command line options
* -u, --url - Source URL to a published Spreadsheet, see README.md for more information.
* -d, --dir - Directory where to save all CSV files, relative to application root path.
"""
@doc false
def run(args) do
Application.ensure_all_started(:httpoison)
{options, _, _} =
OptionParser.parse(
args,
switches: [url: :string, dir: :string],
aliases: [u: :url, d: :dir]
)
# If no commandline options are given, we load all spreadsheets configured for the application
options =
if options == [] do
Application.fetch_env!(:google_sheets, :spreadsheets)
else
# Make a list of lists from parsed options
[{:id, options}]
end
fetch_spreadsheets(options)
end
defp fetch_spreadsheets([]), do: :ok
defp fetch_spreadsheets([{_id, config} | rest]) do
url = Keyword.fetch!(config, :url)
path =
case Keyword.fetch!(config, :dir) do
{app, dir} ->
Application.app_dir(app, dir)
dir ->
dir
end
Mix.shell().info("Loading spreadsheet from url #{inspect(url)} and saving to #{path}")
{:ok, _version, worksheets} = Docs.load(nil, :id, config)
Enum.map(worksheets, fn ws ->
filename = Path.join(path, ws.name <> ".csv")
# Write in raw mode so that we are not changing newlines etc
File.write!(filename, ws.csv, [:raw])
end)
fetch_spreadsheets(rest)
end
end