Allow subtitle downloading (#11)
* Added subtitle options to media profile model * Updated media profile form * Adds subtitle-based options in options builder * Updates metadata parser to include subtitles * Adds subtitle_filepaths to media_item * renamed video_filepath to media_filepath * Added more fields to media profile show page
This commit is contained in:
@@ -22,12 +22,12 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MediaParserTest do
|
||||
}}
|
||||
end
|
||||
|
||||
describe "parse_for_media_item/1" do
|
||||
describe "parse_for_media_item/1 when testing media metadata" do
|
||||
test "it extracts the video filepath", %{metadata: metadata} do
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert String.contains?(result.video_filepath, "bwRHIkYqYJo")
|
||||
assert String.ends_with?(result.video_filepath, ".mkv")
|
||||
assert String.contains?(result.media_filepath, "bwRHIkYqYJo")
|
||||
assert String.ends_with?(result.media_filepath, ".mkv")
|
||||
end
|
||||
|
||||
test "it extracts the title", %{metadata: metadata} do
|
||||
@@ -42,4 +42,45 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MediaParserTest do
|
||||
assert result.metadata.client_response == metadata
|
||||
end
|
||||
end
|
||||
|
||||
describe "parse_for_media_item/1 when testing subtitle metadata" do
|
||||
test "extracts the subtitle filepaths", %{metadata: metadata} do
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert [["de", german_filepath], ["en", english_filepath]] = result.subtitle_filepaths
|
||||
|
||||
assert String.ends_with?(english_filepath, ".en.srt")
|
||||
assert String.ends_with?(german_filepath, ".de.srt")
|
||||
end
|
||||
|
||||
test "sorts the subtitle filepaths by language", %{metadata: metadata} do
|
||||
metadata =
|
||||
Map.put(metadata, "requested_subtitles", %{
|
||||
"en" => %{"filepath" => "en.srt"},
|
||||
"za" => %{"filepath" => "za.srt"},
|
||||
"de" => %{"filepath" => "de.srt"},
|
||||
"al" => %{"filepath" => "al.srt"}
|
||||
})
|
||||
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert [["al", _], ["de", _], ["en", _], ["za", _]] = result.subtitle_filepaths
|
||||
end
|
||||
|
||||
test "doesn't freak out if the video has no subtitles", %{metadata: metadata} do
|
||||
metadata = Map.put(metadata, "requested_subtitles", %{})
|
||||
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert result.subtitle_filepaths == []
|
||||
end
|
||||
|
||||
test "doesn't freak out if the requested_subtitles key is missing", %{metadata: metadata} do
|
||||
metadata = Map.delete(metadata, "requested_subtitles")
|
||||
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert result.subtitle_filepaths == []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,7 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
|
||||
setup do
|
||||
media_item =
|
||||
Repo.preload(
|
||||
media_item_fixture(%{title: nil, video_filepath: nil}),
|
||||
media_item_fixture(%{title: nil, media_filepath: nil}),
|
||||
[:metadata, channel: :media_profile]
|
||||
)
|
||||
|
||||
@@ -33,10 +33,11 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
|
||||
{:ok, render_metadata(:media_metadata)}
|
||||
end)
|
||||
|
||||
assert %{video_filepath: nil, title: nil} = media_item
|
||||
assert %{media_filepath: nil, title: nil, subtitle_filepaths: []} = media_item
|
||||
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
|
||||
assert updated_media_item.video_filepath
|
||||
assert updated_media_item.media_filepath
|
||||
assert updated_media_item.title
|
||||
assert length(updated_media_item.subtitle_filepaths) > 0
|
||||
end
|
||||
|
||||
test "it saves the metadata to the database", %{media_item: media_item} do
|
||||
|
||||
@@ -8,7 +8,7 @@ defmodule Pinchflat.MediaTest do
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Media.MediaItem
|
||||
|
||||
@invalid_attrs %{title: nil, media_id: nil, video_filepath: nil}
|
||||
@invalid_attrs %{title: nil, media_id: nil, media_filepath: nil}
|
||||
|
||||
describe "schema" do
|
||||
test "media_metadata is deleted when media_item is deleted" do
|
||||
@@ -32,18 +32,18 @@ defmodule Pinchflat.MediaTest do
|
||||
describe "list_pending_media_items_for/1" do
|
||||
test "it returns pending media_items for a given channel" do
|
||||
channel = channel_fixture()
|
||||
media_item = media_item_fixture(%{channel_id: channel.id, video_filepath: nil})
|
||||
media_item = media_item_fixture(%{channel_id: channel.id, media_filepath: nil})
|
||||
|
||||
assert Media.list_pending_media_items_for(channel) == [media_item]
|
||||
end
|
||||
|
||||
test "it does not return media_items with video_filepath" do
|
||||
test "it does not return media_items with media_filepath" do
|
||||
channel = channel_fixture()
|
||||
|
||||
_media_item =
|
||||
media_item_fixture(%{
|
||||
channel_id: channel.id,
|
||||
video_filepath: "/video/#{Faker.File.file_name(:video)}"
|
||||
media_filepath: "/video/#{Faker.File.file_name(:video)}"
|
||||
})
|
||||
|
||||
assert Media.list_pending_media_items_for(channel) == []
|
||||
@@ -62,14 +62,14 @@ defmodule Pinchflat.MediaTest do
|
||||
valid_attrs = %{
|
||||
media_id: Faker.String.base64(12),
|
||||
title: Faker.Commerce.product_name(),
|
||||
video_filepath: "/video/#{Faker.File.file_name(:video)}",
|
||||
media_filepath: "/video/#{Faker.File.file_name(:video)}",
|
||||
channel_id: channel_fixture().id
|
||||
}
|
||||
|
||||
assert {:ok, %MediaItem{} = media_item} = Media.create_media_item(valid_attrs)
|
||||
assert media_item.title == valid_attrs.title
|
||||
assert media_item.media_id == valid_attrs.media_id
|
||||
assert media_item.video_filepath == valid_attrs.video_filepath
|
||||
assert media_item.media_filepath == valid_attrs.media_filepath
|
||||
end
|
||||
|
||||
test "creating with invalid data returns error changeset" do
|
||||
@@ -84,14 +84,14 @@ defmodule Pinchflat.MediaTest do
|
||||
update_attrs = %{
|
||||
media_id: Faker.String.base64(12),
|
||||
title: Faker.Commerce.product_name(),
|
||||
video_filepath: "/video/#{Faker.File.file_name(:video)}",
|
||||
media_filepath: "/video/#{Faker.File.file_name(:video)}",
|
||||
channel_id: channel_fixture().id
|
||||
}
|
||||
|
||||
assert {:ok, %MediaItem{} = media_item} = Media.update_media_item(media_item, update_attrs)
|
||||
assert media_item.title == update_attrs.title
|
||||
assert media_item.media_id == update_attrs.media_id
|
||||
assert media_item.video_filepath == update_attrs.video_filepath
|
||||
assert media_item.media_filepath == update_attrs.media_filepath
|
||||
end
|
||||
|
||||
test "updating with invalid data returns error changeset" do
|
||||
|
||||
@@ -15,4 +15,83 @@ defmodule Pinchflat.Profiles.Options.YtDlp.OptionBuilderTest do
|
||||
assert {:output, "/tmp/videos/%(title)S.%(ext)s"} in res
|
||||
end
|
||||
end
|
||||
|
||||
describe "build/1 when testing subtitle options" do
|
||||
test "includes :write_subs option when specified" do
|
||||
media_profile = %MediaProfile{@media_profile | download_subs: true}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
assert :write_subs in res
|
||||
end
|
||||
|
||||
test "forces SRT format when download_subs is true" do
|
||||
media_profile = %MediaProfile{@media_profile | download_subs: true}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
assert {:convert_subs, "srt"} in res
|
||||
end
|
||||
|
||||
test "includes :write_auto_subs option when specified" do
|
||||
media_profile = %MediaProfile{@media_profile | download_subs: true, download_auto_subs: true}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
assert :write_auto_subs in res
|
||||
end
|
||||
|
||||
test "doesn't include :write_auto_subs option when download_subs is false" do
|
||||
media_profile = %MediaProfile{@media_profile | download_subs: false, download_auto_subs: true}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
refute :write_auto_subs in res
|
||||
end
|
||||
|
||||
test "includes :embed_subs option when specified" do
|
||||
media_profile = %MediaProfile{@media_profile | embed_subs: true}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
assert :embed_subs in res
|
||||
end
|
||||
|
||||
test "includes sub_langs option when download_subs is true" do
|
||||
media_profile = %MediaProfile{@media_profile | download_subs: true, sub_langs: "en"}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
assert {:sub_langs, "en"} in res
|
||||
end
|
||||
|
||||
test "includes sub_langs option when embed_subs is true" do
|
||||
media_profile = %MediaProfile{@media_profile | embed_subs: true, sub_langs: "en"}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
assert {:sub_langs, "en"} in res
|
||||
end
|
||||
|
||||
test "doesn't include sub_langs option when neither downloading nor embedding" do
|
||||
media_profile = %MediaProfile{
|
||||
@media_profile
|
||||
| embed_subs: false,
|
||||
download_subs: false,
|
||||
sub_langs: "en"
|
||||
}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
refute {:sub_langs, "en"} in res
|
||||
end
|
||||
|
||||
test "other struct attributes are ignored" do
|
||||
media_profile = %MediaProfile{@media_profile | id: -1}
|
||||
|
||||
assert {:ok, res} = OptionBuilder.build(media_profile)
|
||||
|
||||
refute {:id, -1} in res
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -50,7 +50,7 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, "video1"} end)
|
||||
|
||||
channel = channel_fixture(index_frequency_minutes: 10)
|
||||
media_item_fixture(%{channel_id: channel.id, video_filepath: nil})
|
||||
media_item_fixture(%{channel_id: channel.id, media_filepath: nil})
|
||||
perform_job(MediaIndexingWorker, %{id: channel.id})
|
||||
|
||||
assert [_, _] = all_enqueued(worker: VideoDownloadWorker)
|
||||
|
||||
@@ -11,7 +11,7 @@ defmodule Pinchflat.Workers.VideoDownloadWorkerTest do
|
||||
setup do
|
||||
media_item =
|
||||
Repo.preload(
|
||||
media_item_fixture(%{video_filepath: nil}),
|
||||
media_item_fixture(%{media_filepath: nil}),
|
||||
[:metadata, channel: :media_profile]
|
||||
)
|
||||
|
||||
@@ -24,9 +24,9 @@ defmodule Pinchflat.Workers.VideoDownloadWorkerTest do
|
||||
{:ok, render_metadata(:media_metadata)}
|
||||
end)
|
||||
|
||||
assert media_item.video_filepath == nil
|
||||
assert media_item.media_filepath == nil
|
||||
perform_job(VideoDownloadWorker, %{id: media_item.id})
|
||||
assert Repo.reload(media_item).video_filepath != nil
|
||||
assert Repo.reload(media_item).media_filepath != nil
|
||||
end
|
||||
|
||||
test "it saves the metadata to the media_item", %{media_item: media_item} do
|
||||
|
||||
+4844
-831
File diff suppressed because one or more lines are too long
@@ -15,7 +15,7 @@ defmodule Pinchflat.MediaFixtures do
|
||||
|> Enum.into(%{
|
||||
media_id: Faker.String.base64(12),
|
||||
title: Faker.Commerce.product_name(),
|
||||
video_filepath: "/video/#{Faker.File.file_name(:video)}",
|
||||
media_filepath: "/video/#{Faker.File.file_name(:video)}",
|
||||
channel_id: MediaSourceFixtures.channel_fixture().id
|
||||
})
|
||||
|> Pinchflat.Media.create_media_item()
|
||||
|
||||
Reference in New Issue
Block a user