Added playlist_index as a media item field

This commit is contained in:
Kieran Eglin 2024-07-15 14:55:13 -07:00
parent 41524cd01b
commit 285368e60b
No known key found for this signature in database
GPG key ID: 193984967FCF432D
7 changed files with 35 additions and 2 deletions

View file

@ -18,6 +18,8 @@ defmodule Pinchflat.Media.MediaItem do
alias Pinchflat.Media.MediaItemsSearchIndex alias Pinchflat.Media.MediaItemsSearchIndex
@allowed_fields [ @allowed_fields [
# these fields are only captured on index
:playlist_index,
# these fields are captured on indexing (and again on download) # these fields are captured on indexing (and again on download)
:title, :title,
:media_id, :media_id,
@ -72,6 +74,7 @@ defmodule Pinchflat.Media.MediaItem do
field :uploaded_at, :utc_datetime field :uploaded_at, :utc_datetime
field :upload_date_index, :integer, default: 0 field :upload_date_index, :integer, default: 0
field :duration_seconds, :integer field :duration_seconds, :integer
field :playlist_index, :integer, default: 0
field :media_filepath, :string field :media_filepath, :string
field :media_size_bytes, :integer field :media_size_bytes, :integer

View file

@ -32,6 +32,8 @@ defmodule Pinchflat.Metadata.MetadataParser do
media_filepath: metadata["filepath"] 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 end
defp parse_subtitle_metadata(metadata) do defp parse_subtitle_metadata(metadata) do

View file

@ -65,7 +65,7 @@ defmodule Pinchflat.YtDlp.Media do
@doc """ @doc """
Returns a map representing the media at the given URL. 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 def get_media_attributes(url) do
runner = Application.get_env(:pinchflat, :yt_dlp_runner) runner = Application.get_env(:pinchflat, :yt_dlp_runner)
@ -109,7 +109,7 @@ defmodule Pinchflat.YtDlp.Media do
duration_seconds: response["duration"] && round(response["duration"]), duration_seconds: response["duration"] && round(response["duration"]),
short_form_content: response["webpage_url"] && short_form_content?(response), short_form_content: response["webpage_url"] && short_form_content?(response),
uploaded_at: response["upload_date"] && parse_uploaded_at(response), uploaded_at: response["upload_date"] && parse_uploaded_at(response),
playlist_index: response["playlist_index"] || 0 playlist_index: response["playlist_index"]
} }
end end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 493 KiB

After

Width:  |  Height:  |  Size: 424 KiB

View file

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

View file

@ -59,6 +59,14 @@ defmodule Pinchflat.Metadata.MetadataParserTest do
assert result.duration_seconds == round(metadata["duration"]) assert result.duration_seconds == round(metadata["duration"])
end 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 end
describe "parse_for_media_item/1 when testing subtitle metadata" do describe "parse_for_media_item/1 when testing subtitle metadata" do

View file

@ -221,6 +221,17 @@ defmodule Pinchflat.YtDlp.MediaTest do
assert %Media{livestream: false} = Media.response_to_struct(response) assert %Media{livestream: false} = Media.response_to_struct(response)
end 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 end
describe "response_to_struct/1 when testing uploaded_at" do describe "response_to_struct/1 when testing uploaded_at" do