Created
October 25, 2022 10:42
-
-
Save ream88/b69c073462810d9ca7276f05bfb50253 to your computer and use it in GitHub Desktop.
A small Elixir script that exports Favro collections and associated cards as JSON files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mix.install([ | |
{:jason, "~> 1.4"}, | |
{:req, "~> 0.3.1"}, | |
{:zarex, "~> 1.0"} | |
]) | |
organization_id = "INSERT_ORGANIZATION_ID" | |
email = "INSERT_EMAIL" | |
api_token = "INSERT_API_TOKEN" | |
defmodule Favro do | |
def resource(url, opts \\ []) do | |
Stream.resource( | |
fn -> {url, %{}} end, | |
fn {url, params} -> | |
{:ok, %Req.Response{body: body}} = Req.get(build_url(url, params), opts) | |
if body["page"] == body["pages"] do | |
{:halt, {url, %{}}} | |
else | |
{body["entities"], {url, %{page: body["page"] + 1, requestId: body["requestId"]}}} | |
end | |
end, | |
& &1 | |
) | |
end | |
defp build_url(url, params) do | |
url | |
|> URI.parse() | |
|> then(fn %URI{query: query} = uri -> | |
query = | |
(query || "") | |
|> URI.decode_query() | |
|> Map.merge(params) | |
|> URI.encode_query() | |
%URI{uri | query: query} | |
end) | |
|> URI.to_string() | |
end | |
end | |
headers = %{"organizationId" => organization_id} | |
auth = {email, api_token} | |
collections = | |
Favro.resource("https://favro.com/api/v1/collections", headers: headers, auth: auth) | |
|> Enum.to_list() | |
File.mkdir_p!("export") | |
File.write!("export/collections.json", Jason.encode!(collections)) | |
for collection <- collections do | |
cards = | |
Favro.resource("https://favro.com/api/v1/cards?collectionId=#{collection["collectionId"]}", | |
headers: headers, | |
auth: auth | |
) | |
|> Enum.to_list() | |
path = "export/#{collection["collectionId"]} - #{Zarex.sanitize(collection["name"])}" | |
File.mkdir_p!(path) | |
File.write!(Path.join(path, "cards.json"), Jason.encode!(cards)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment