Here’s a thing you should totally not do.

Manton has made an archive of the posts from App.Net (App Dot Net?) available.

Normally I’d download my own posts with something simple and fast like wget. But since I’ve been trying to write more code in Elixir, I decided to write a small script to download my posts using that.

This script is a .exs file, meaning it is not compiled, and is meant to be run as elixir adn_download.exs with one argument, your user name.

For me, it’s elixir adn_download.exs jbecker.

This will create a folder in your current directory called posts with a JSON file for each post.

I’ll probably do something to parse and make that JSON useful in Elixir later too, but for now, my Saturday-morning-avoiding-responsibilities computer time is over.

Code below in line, and also here in a gist.

 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
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)