Adds description to media items; hooks it up to indexing/media downloading

This commit is contained in:
Kieran Eglin 2024-02-09 21:06:00 -08:00
parent 219320ce11
commit 00f7aef8dc
No known key found for this signature in database
GPG key ID: 193984967FCF432D
11 changed files with 36 additions and 7 deletions

View file

@ -13,6 +13,7 @@ defmodule Pinchflat.Media.MediaItem do
@allowed_fields ~w(
title
media_id
description
original_url
livestream
media_downloaded_at
@ -27,6 +28,7 @@ defmodule Pinchflat.Media.MediaItem do
schema "media_items" do
field :title, :string
field :media_id, :string
field :description, :string
field :original_url, :string
field :livestream, :boolean, default: false
field :media_downloaded_at, :utc_datetime

View file

@ -32,6 +32,7 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MetadataParser do
defp parse_media_metadata(metadata) do
%{
title: metadata["title"],
description: metadata["description"],
media_filepath: metadata["filepath"]
}
end

View file

@ -14,8 +14,9 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollection do
def get_media_attributes(url, command_opts \\ []) do
runner = Application.get_env(:pinchflat, :yt_dlp_runner)
opts = command_opts ++ [:simulate, :skip_download]
output_template = "%(.{id,title,was_live,original_url,description})j"
case runner.run(url, opts, "%(.{id,title,was_live,original_url})j") do
case runner.run(url, opts, output_template) do
{:ok, output} ->
output
|> String.split("\n", trim: true)

View file

@ -52,7 +52,8 @@ defmodule Pinchflat.Tasks.SourceTasks do
title: media_attrs["title"],
media_id: media_attrs["id"],
original_url: media_attrs["original_url"],
livestream: media_attrs["was_live"]
livestream: media_attrs["was_live"],
description: media_attrs["description"]
}
case Media.create_media_item(attrs) do

View file

@ -0,0 +1,9 @@
defmodule Pinchflat.Repo.Migrations.AddDescriptionToMediaItems do
use Ecto.Migration
def change do
alter table(:media_items) do
add :description, :text
end
end
end

View file

@ -36,6 +36,12 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MediaParserTest do
assert result.title == "Trying to Wheelie Without the Rear Brake"
end
test "it extracts the description", %{metadata: metadata} do
result = Parser.parse_for_media_item(metadata)
assert is_binary(result.description)
end
test "it returns the metadata as a map", %{metadata: metadata} do
result = Parser.parse_for_media_item(metadata)

View file

@ -20,7 +20,7 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollectionTest do
test "it passes the expected default args" do
expect(YtDlpRunnerMock, :run, fn _url, opts, ot ->
assert opts == [:simulate, :skip_download]
assert ot == "%(.{id,title,was_live,original_url})j"
assert ot == "%(.{id,title,was_live,original_url,description})j"
{:ok, ""}
end)

View file

@ -47,7 +47,7 @@ defmodule Pinchflat.MediaClient.SourceDetailsTest do
test "it passes the expected arguments to the backend" do
expect(YtDlpRunnerMock, :run, fn @channel_url, opts, ot ->
assert opts == [:simulate, :skip_download]
assert ot == "%(.{id,title,was_live,original_url})j"
assert ot == "%(.{id,title,was_live,original_url,description})j"
{:ok, ""}
end)

View file

@ -68,6 +68,11 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
assert updated_media_item.title == "Trying to Wheelie Without the Rear Brake"
end
test "it extracts the description", %{media_item: media_item} do
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
assert is_binary(updated_media_item.description)
end
test "it extracts the media_filepath", %{media_item: media_item} do
assert media_item.media_filepath == nil
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)

View file

@ -63,6 +63,7 @@ defmodule Pinchflat.Tasks.SourceTasksTest do
assert Enum.count(media_items) == 3
assert ["video1", "video2", "video3"] == Enum.map(media_items, & &1.media_id)
assert ["Video 1", "Video 2", "Video 3"] == Enum.map(media_items, & &1.title)
assert ["desc1", "desc2", "desc3"] == Enum.map(media_items, & &1.description)
assert Enum.all?(media_items, fn mi -> mi.original_url end)
assert Enum.all?(media_items, fn %MediaItem{} -> true end)
end

View file

@ -36,19 +36,22 @@ defmodule Pinchflat.MediaSourceFixtures do
id: "video1",
title: "Video 1",
original_url: "https://example.com/video1",
was_live: false
was_live: false,
description: "desc1"
},
%{
id: "video2",
title: "Video 2",
original_url: "https://example.com/video2",
was_live: true
was_live: true,
description: "desc2"
},
%{
id: "video3",
title: "Video 3",
original_url: "https://example.com/video3",
was_live: false
was_live: false,
description: "desc3"
}
]