diff --git a/lib/pinchflat/media/media_item.ex b/lib/pinchflat/media/media_item.ex index 99a53c0..f85fcfb 100644 --- a/lib/pinchflat/media/media_item.ex +++ b/lib/pinchflat/media/media_item.ex @@ -18,6 +18,8 @@ defmodule Pinchflat.Media.MediaItem do alias Pinchflat.Media.MediaItemsSearchIndex @allowed_fields [ + # these fields are only captured on index + :playlist_index, # these fields are captured on indexing (and again on download) :title, :media_id, @@ -72,6 +74,7 @@ defmodule Pinchflat.Media.MediaItem do field :uploaded_at, :utc_datetime field :upload_date_index, :integer, default: 0 field :duration_seconds, :integer + field :playlist_index, :integer, default: 0 field :media_filepath, :string field :media_size_bytes, :integer diff --git a/lib/pinchflat/metadata/metadata_parser.ex b/lib/pinchflat/metadata/metadata_parser.ex index 60456d8..258b95e 100644 --- a/lib/pinchflat/metadata/metadata_parser.ex +++ b/lib/pinchflat/metadata/metadata_parser.ex @@ -32,6 +32,8 @@ defmodule Pinchflat.Metadata.MetadataParser do media_filepath: metadata["filepath"] } ) + # This should only ever be written during indexing and this whole module gets run during download + |> Map.drop([:playlist_index]) end defp parse_subtitle_metadata(metadata) do diff --git a/lib/pinchflat/yt_dlp/media.ex b/lib/pinchflat/yt_dlp/media.ex index 9dc0dad..eafa6f9 100644 --- a/lib/pinchflat/yt_dlp/media.ex +++ b/lib/pinchflat/yt_dlp/media.ex @@ -65,7 +65,7 @@ defmodule Pinchflat.YtDlp.Media do @doc """ Returns a map representing the media at the given URL. - Returns {:ok, [map()]} | {:error, any, ...}. + Returns {:ok, %Media{}} | {:error, any, ...}. """ def get_media_attributes(url) do runner = Application.get_env(:pinchflat, :yt_dlp_runner) @@ -109,7 +109,7 @@ defmodule Pinchflat.YtDlp.Media do duration_seconds: response["duration"] && round(response["duration"]), short_form_content: response["webpage_url"] && short_form_content?(response), uploaded_at: response["upload_date"] && parse_uploaded_at(response), - playlist_index: response["playlist_index"] || 0 + playlist_index: response["playlist_index"] } end diff --git a/priv/repo/erd.png b/priv/repo/erd.png index e88df38..50e0ec4 100644 Binary files a/priv/repo/erd.png and b/priv/repo/erd.png differ diff --git a/priv/repo/migrations/20240715212133_add_playlist_index_to_media_items.exs b/priv/repo/migrations/20240715212133_add_playlist_index_to_media_items.exs new file mode 100644 index 0000000..e629efd --- /dev/null +++ b/priv/repo/migrations/20240715212133_add_playlist_index_to_media_items.exs @@ -0,0 +1,9 @@ +defmodule Pinchflat.Repo.Migrations.AddPlaylistIndexToMediaItems do + use Ecto.Migration + + def change do + alter table(:media_items) do + add :playlist_index, :integer, null: false, default: 0 + end + end +end diff --git a/test/pinchflat/metadata/metadata_parser_test.exs b/test/pinchflat/metadata/metadata_parser_test.exs index 48015b1..7153358 100644 --- a/test/pinchflat/metadata/metadata_parser_test.exs +++ b/test/pinchflat/metadata/metadata_parser_test.exs @@ -59,6 +59,14 @@ defmodule Pinchflat.Metadata.MetadataParserTest do assert result.duration_seconds == round(metadata["duration"]) end + + test "drops the playlist_index` field", %{metadata: metadata} do + metadata = Map.put(metadata, "playlist_index", 1) + + result = Parser.parse_for_media_item(metadata) + + assert Map.get(result, :playlist_index) == nil + end end describe "parse_for_media_item/1 when testing subtitle metadata" do diff --git a/test/pinchflat/yt_dlp/media_test.exs b/test/pinchflat/yt_dlp/media_test.exs index e775cf6..1e8b110 100644 --- a/test/pinchflat/yt_dlp/media_test.exs +++ b/test/pinchflat/yt_dlp/media_test.exs @@ -221,6 +221,17 @@ defmodule Pinchflat.YtDlp.MediaTest do assert %Media{livestream: false} = Media.response_to_struct(response) end + + test "doesn't blow up if playlist_index is missing" do + response = %{ + "webpage_url" => "https://www.youtube.com/watch?v=TiZPUDkDYbk", + "aspect_ratio" => 1.0, + "duration" => nil, + "upload_date" => "20210101" + } + + assert %Media{playlist_index: 0} = Media.response_to_struct(response) + end end describe "response_to_struct/1 when testing uploaded_at" do