Skip to content

Instantly share code, notes, and snippets.

@jsonbecker
Created June 3, 2023 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsonbecker/1eceaeb3c41ce0ae2da7c72a5a47fd1d to your computer and use it in GitHub Desktop.
Save jsonbecker/1eceaeb3c41ce0ae2da7c72a5a47fd1d to your computer and use it in GitHub Desktop.
An inefficient way to download your App Dot Net posts using Elixir
Mix.install([{:httpoison, "~> 2.0"}])
defmodule ADNDownloader do
require HTTPoison
@base_url "https://adn.micro.blog/"
def get_posts(user_name) do
url = @base_url <> "users/" <> String.at(user_name, 0) <> "/" <> user_name <> ".txt"
%HTTPoison.Response{body: body} = HTTPoison.get!(url)
post_urls =
body
|> String.split("\n")
post_urls
|> Enum.reject(fn post_url -> !String.match?(post_url, ~r/json$/) end)
|> Enum.map(fn post_url -> @base_url <> post_url end)
end
def get_json(url) do
%HTTPoison.Response{body: body} = HTTPoison.get!(url)
file =
Regex.named_captures(~r/\/(?<post>[0-9]*\.json$)/, url)
|> Map.get("post")
filepath = File.cwd!() <> "/posts/" <> file
IO.puts(filepath)
{filepath, body}
end
end
[user_name] = System.argv()
posts = ADNDownloader.get_posts(user_name)
File.mkdir!(File.cwd!() <> "/posts")
posts
|> Enum.map(&ADNDownloader.get_json(&1))
|> Enum.map(fn {filename, contents} -> File.write!(filename, contents) end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment