Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Notebook

scripts/backfill.livemd

Notebook

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

Section

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()
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((?\d+)H)?((?\d+)M)?((?\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
data |> Enum.map(&Video.backfill/1) |> Enum.join("\n") |> IO.puts()