Added NFO download fields; hooked up NFO generation to downloading pipeline

This commit is contained in:
Kieran Eglin 2024-03-13 11:20:47 -07:00
parent b8add0201e
commit afad769d0b
No known key found for this signature in database
GPG key ID: 193984967FCF432D
10 changed files with 87 additions and 449 deletions

File diff suppressed because one or more lines are too long

View file

@ -8,11 +8,12 @@ defmodule Pinchflat.Downloading.MediaDownloader do
alias Pinchflat.Repo
alias Pinchflat.Media
alias Pinchflat.Media.MediaItem
alias Pinchflat.Metadata.NfoBuilder
alias Pinchflat.Metadata.MetadataParser
alias Pinchflat.Metadata.MetadataFileHelpers
alias Pinchflat.Downloading.DownloadOptionBuilder
alias Pinchflat.YtDlp.Media, as: YtDlpMedia
alias Pinchflat.Downloading.DownloadOptionBuilder, as: YtDlpDownloadOptionBuilder
alias Pinchflat.Metadata.MetadataParser, as: YtDlpMetadataParser
alias Pinchflat.Metadata.MetadataFileHelpers, as: YtDlpMetadataHelpers
@doc """
Downloads media for a media item, updating the media item based on the metadata
@ -30,16 +31,15 @@ defmodule Pinchflat.Downloading.MediaDownloader do
case download_with_options(media_item.original_url, item_with_preloads) do
{:ok, parsed_json} ->
{parser, helpers} = {YtDlpMetadataParser, YtDlpMetadataHelpers}
parsed_attrs =
parsed_json
|> parser.parse_for_media_item()
|> MetadataParser.parse_for_media_item()
|> Map.merge(%{
media_downloaded_at: DateTime.utc_now(),
nfo_filepath: determine_nfo_filepath(item_with_preloads, parsed_json),
metadata: %{
metadata_filepath: helpers.compress_and_store_metadata_for(media_item, parsed_json),
thumbnail_filepath: helpers.download_and_store_thumbnail_for(media_item, parsed_json)
metadata_filepath: MetadataFileHelpers.compress_and_store_metadata_for(media_item, parsed_json),
thumbnail_filepath: MetadataFileHelpers.download_and_store_thumbnail_for(media_item, parsed_json)
}
})
@ -52,13 +52,16 @@ defmodule Pinchflat.Downloading.MediaDownloader do
end
end
# def download_for_source(source, url) do
# # Create MI from source and URL
# media_item = nil
# end
defp determine_nfo_filepath(media_item, parsed_json) do
if media_item.source.media_profile.download_nfo do
NfoBuilder.build_and_store_for_media_item(parsed_json)
else
nil
end
end
defp download_with_options(url, item_with_preloads) do
{:ok, options} = YtDlpDownloadOptionBuilder.build(item_with_preloads)
{:ok, options} = DownloadOptionBuilder.build(item_with_preloads)
YtDlpMedia.download(url, options)
end

View file

@ -27,7 +27,8 @@ defmodule Pinchflat.Media.MediaItem do
:media_size_bytes,
:subtitle_filepaths,
:thumbnail_filepath,
:metadata_filepath
:metadata_filepath,
:nfo_filepath
]
# Pretty much all the fields captured at index are required.
@required_fields ~w(
@ -54,6 +55,7 @@ defmodule Pinchflat.Media.MediaItem do
field :media_size_bytes, :integer
field :thumbnail_filepath, :string
field :metadata_filepath, :string
field :nfo_filepath, :string
# This is an array of [iso-2 language, filepath] pairs. Probably could
# be an associated record, but I don't see the benefit right now.
# Will very likely revisit because I can't leave well-enough alone.
@ -82,6 +84,6 @@ defmodule Pinchflat.Media.MediaItem do
@doc false
def filepath_attributes do
~w(media_filepath thumbnail_filepath metadata_filepath subtitle_filepaths)a
~w(media_filepath thumbnail_filepath metadata_filepath subtitle_filepaths nfo_filepath)a
end
end

View file

@ -19,6 +19,7 @@ defmodule Pinchflat.Profiles.MediaProfile do
embed_thumbnail
download_metadata
embed_metadata
download_nfo
shorts_behaviour
livestream_behaviour
preferred_resolution
@ -32,17 +33,18 @@ defmodule Pinchflat.Profiles.MediaProfile do
field :output_path_template, :string,
default: "/{{ source_custom_name }}/{{ upload_yyyy_mm_dd }} {{ title }}/{{ title }} [{{ id }}].{{ ext }}"
field :download_subs, :boolean, default: true
field :download_auto_subs, :boolean, default: true
field :download_subs, :boolean, default: false
field :download_auto_subs, :boolean, default: false
field :embed_subs, :boolean, default: true
field :sub_langs, :string, default: "en"
field :download_thumbnail, :boolean, default: true
field :download_thumbnail, :boolean, default: false
field :embed_thumbnail, :boolean, default: true
field :download_metadata, :boolean, default: true
field :download_metadata, :boolean, default: false
field :embed_metadata, :boolean, default: true
field :download_nfo, :boolean, default: false
# NOTE: these do NOT speed up indexing - the indexer still has to go
# through the entire collection to determine if a media is a short or
# a livestream.

View file

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

View file

@ -0,0 +1,9 @@
defmodule Pinchflat.Repo.Migrations.AddDownloadNfoToMediaProfile do
use Ecto.Migration
def change do
alter table(:media_profiles) do
add :download_nfo, :boolean, default: false, null: false
end
end
end

View file

@ -2,6 +2,8 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
use Pinchflat.DataCase
import Mox
import Pinchflat.MediaFixtures
import Pinchflat.SourcesFixtures
import Pinchflat.ProfilesFixtures
alias Pinchflat.Downloading.MediaDownloader
@ -103,4 +105,37 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
assert String.ends_with?(updated_media_item.metadata_filepath, ".info.json")
end
end
describe "download_for_media_item/3 when testing NFO generation" do
setup do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot ->
{:ok, render_metadata(:media_metadata)}
end)
:ok
end
test "it generates an NFO file if the source is set to download NFOs" do
profile = media_profile_fixture(%{download_nfo: true})
source = source_fixture(%{media_profile_id: profile.id})
media_item = media_item_fixture(%{source_id: source.id})
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
assert String.ends_with?(updated_media_item.nfo_filepath, ".nfo")
assert File.exists?(updated_media_item.nfo_filepath)
File.rm!(updated_media_item.nfo_filepath)
end
test "it does not generate an NFO file if the source is set to not download NFOs" do
profile = media_profile_fixture(%{download_nfo: false})
source = source_fixture(%{media_profile_id: profile.id})
media_item = media_item_fixture(%{source_id: source.id})
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
assert updated_media_item.nfo_filepath == nil
end
end
end

View file

@ -4,22 +4,7 @@ defmodule Pinchflat.Metadata.MetadataParserTest do
alias Pinchflat.Metadata.MetadataParser, as: Parser
setup do
json_filepath =
Path.join([
File.cwd!(),
"test",
"support",
"files",
"media_metadata.json"
])
{:ok, file_body} = File.read(json_filepath)
{:ok, parsed_json} = Phoenix.json_library().decode(file_body)
{:ok,
%{
metadata: parsed_json
}}
{:ok, %{metadata: render_parsed_metadata(:media_metadata)}}
end
describe "parse_for_media_item/1 when testing media metadata" do

View file

@ -4,19 +4,7 @@ defmodule Pinchflat.Metadata.NfoBuilderTest do
alias Pinchflat.Metadata.NfoBuilder
setup do
json_filepath =
Path.join([
File.cwd!(),
"test",
"support",
"files",
"media_metadata.json"
])
{:ok, file_body} = File.read(json_filepath)
{:ok, parsed_json} = Phoenix.json_library().decode(file_body)
{:ok, %{metadata: parsed_json}}
{:ok, %{metadata: render_parsed_metadata(:media_metadata)}}
end
describe "build_and_store_for_media_item/1" do

View file

@ -48,4 +48,10 @@ defmodule Pinchflat.TestingHelperMethods do
File.read!(json_filepath)
end
def render_parsed_metadata(metadata_name) do
metadata_name
|> render_metadata()
|> Phoenix.json_library().decode!()
end
end