<!-- livebook:{"file_entries":[{"name":"by-ids-1.json","type":"attachment"},{"name":"by-ids-2.json","type":"attachment"},{"name":"by-ids-3.json","type":"attachment"}]} -->

# Notebook

```elixir
Mix.install([
  {:kino, "~> 0.12.0"},
  {:jason, "~> 1.4"}
])
```

## Section

```elixir
data =
  [1, 2, 3]
  |> Enum.map(&"by-ids-#{&1}.json")
  |> Enum.flat_map(fn path ->
    Kino.FS.file_path(path)
    |> File.read!()
    |> Jason.decode!()
    |> then(& &1["items"])
  end)

data |> Enum.take(6) |> Kino.Tree.new()
```

```elixir
defmodule Video do
  def parse_duration(""), do: 0

  def parse_duration(x) do
    {duration, ""} = Integer.parse(x)
    duration
  end

  def backfill(video) do
    %{"hours" => hours, "minutes" => minutes, "seconds" => seconds} =
      Regex.named_captures(
        ~r/^PT((?<hours>\d+)H)?((?<minutes>\d+)M)?((?<seconds>\d+)S)?$/,
        video["contentDetails"]["duration"]
      )

    duration =
      3600 * parse_duration(hours) +
        60 * parse_duration(minutes) +
        1 * parse_duration(seconds)

    thumbnail_url =
      video["snippet"]["thumbnails"]
      |> Map.values()
      |> Enum.sort_by(& &1["height"], :desc)
      |> Enum.at(0)
      |> then(& &1["url"])

    url = "https://youtube.com/watch?v=#{video["id"]}"

    title = video["snippet"]["title"]

    "{1, nil} = Repo.update_all(from(v in Library.Video, where: v.url == \"#{url}\"), set: [title: \"#{title}\", duration: #{duration}, thumbnail_url: \"#{thumbnail_url}\"])"
  end
end
```

```elixir
data |> Enum.map(&Video.backfill/1) |> Enum.join("\n") |> IO.puts()
```
