Added NFO download fields; hooked up NFO generation to downloading pipeline
This commit is contained in:
parent
b8add0201e
commit
afad769d0b
10 changed files with 87 additions and 449 deletions
401
dump_all.sql
401
dump_all.sql
File diff suppressed because one or more lines are too long
|
|
@ -8,11 +8,12 @@ defmodule Pinchflat.Downloading.MediaDownloader do
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
alias Pinchflat.Media
|
alias Pinchflat.Media
|
||||||
alias Pinchflat.Media.MediaItem
|
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.YtDlp.Media, as: YtDlpMedia
|
||||||
alias Pinchflat.Downloading.DownloadOptionBuilder, as: YtDlpDownloadOptionBuilder
|
|
||||||
alias Pinchflat.Metadata.MetadataParser, as: YtDlpMetadataParser
|
|
||||||
alias Pinchflat.Metadata.MetadataFileHelpers, as: YtDlpMetadataHelpers
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Downloads media for a media item, updating the media item based on the metadata
|
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
|
case download_with_options(media_item.original_url, item_with_preloads) do
|
||||||
{:ok, parsed_json} ->
|
{:ok, parsed_json} ->
|
||||||
{parser, helpers} = {YtDlpMetadataParser, YtDlpMetadataHelpers}
|
|
||||||
|
|
||||||
parsed_attrs =
|
parsed_attrs =
|
||||||
parsed_json
|
parsed_json
|
||||||
|> parser.parse_for_media_item()
|
|> MetadataParser.parse_for_media_item()
|
||||||
|> Map.merge(%{
|
|> Map.merge(%{
|
||||||
media_downloaded_at: DateTime.utc_now(),
|
media_downloaded_at: DateTime.utc_now(),
|
||||||
|
nfo_filepath: determine_nfo_filepath(item_with_preloads, parsed_json),
|
||||||
metadata: %{
|
metadata: %{
|
||||||
metadata_filepath: helpers.compress_and_store_metadata_for(media_item, parsed_json),
|
metadata_filepath: MetadataFileHelpers.compress_and_store_metadata_for(media_item, parsed_json),
|
||||||
thumbnail_filepath: helpers.download_and_store_thumbnail_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
|
||||||
end
|
end
|
||||||
|
|
||||||
# def download_for_source(source, url) do
|
defp determine_nfo_filepath(media_item, parsed_json) do
|
||||||
# # Create MI from source and URL
|
if media_item.source.media_profile.download_nfo do
|
||||||
# media_item = nil
|
NfoBuilder.build_and_store_for_media_item(parsed_json)
|
||||||
# end
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp download_with_options(url, item_with_preloads) do
|
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)
|
YtDlpMedia.download(url, options)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,8 @@ defmodule Pinchflat.Media.MediaItem do
|
||||||
:media_size_bytes,
|
:media_size_bytes,
|
||||||
:subtitle_filepaths,
|
:subtitle_filepaths,
|
||||||
:thumbnail_filepath,
|
:thumbnail_filepath,
|
||||||
:metadata_filepath
|
:metadata_filepath,
|
||||||
|
:nfo_filepath
|
||||||
]
|
]
|
||||||
# Pretty much all the fields captured at index are required.
|
# Pretty much all the fields captured at index are required.
|
||||||
@required_fields ~w(
|
@required_fields ~w(
|
||||||
|
|
@ -54,6 +55,7 @@ defmodule Pinchflat.Media.MediaItem do
|
||||||
field :media_size_bytes, :integer
|
field :media_size_bytes, :integer
|
||||||
field :thumbnail_filepath, :string
|
field :thumbnail_filepath, :string
|
||||||
field :metadata_filepath, :string
|
field :metadata_filepath, :string
|
||||||
|
field :nfo_filepath, :string
|
||||||
# This is an array of [iso-2 language, filepath] pairs. Probably could
|
# 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.
|
# 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.
|
# Will very likely revisit because I can't leave well-enough alone.
|
||||||
|
|
@ -82,6 +84,6 @@ defmodule Pinchflat.Media.MediaItem do
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def filepath_attributes do
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ defmodule Pinchflat.Profiles.MediaProfile do
|
||||||
embed_thumbnail
|
embed_thumbnail
|
||||||
download_metadata
|
download_metadata
|
||||||
embed_metadata
|
embed_metadata
|
||||||
|
download_nfo
|
||||||
shorts_behaviour
|
shorts_behaviour
|
||||||
livestream_behaviour
|
livestream_behaviour
|
||||||
preferred_resolution
|
preferred_resolution
|
||||||
|
|
@ -32,17 +33,18 @@ defmodule Pinchflat.Profiles.MediaProfile do
|
||||||
field :output_path_template, :string,
|
field :output_path_template, :string,
|
||||||
default: "/{{ source_custom_name }}/{{ upload_yyyy_mm_dd }} {{ title }}/{{ title }} [{{ id }}].{{ ext }}"
|
default: "/{{ source_custom_name }}/{{ upload_yyyy_mm_dd }} {{ title }}/{{ title }} [{{ id }}].{{ ext }}"
|
||||||
|
|
||||||
field :download_subs, :boolean, default: true
|
field :download_subs, :boolean, default: false
|
||||||
field :download_auto_subs, :boolean, default: true
|
field :download_auto_subs, :boolean, default: false
|
||||||
field :embed_subs, :boolean, default: true
|
field :embed_subs, :boolean, default: true
|
||||||
field :sub_langs, :string, default: "en"
|
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 :embed_thumbnail, :boolean, default: true
|
||||||
|
|
||||||
field :download_metadata, :boolean, default: true
|
field :download_metadata, :boolean, default: false
|
||||||
field :embed_metadata, :boolean, default: true
|
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
|
# 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
|
# through the entire collection to determine if a media is a short or
|
||||||
# a livestream.
|
# a livestream.
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
|
@ -2,6 +2,8 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
|
||||||
use Pinchflat.DataCase
|
use Pinchflat.DataCase
|
||||||
import Mox
|
import Mox
|
||||||
import Pinchflat.MediaFixtures
|
import Pinchflat.MediaFixtures
|
||||||
|
import Pinchflat.SourcesFixtures
|
||||||
|
import Pinchflat.ProfilesFixtures
|
||||||
|
|
||||||
alias Pinchflat.Downloading.MediaDownloader
|
alias Pinchflat.Downloading.MediaDownloader
|
||||||
|
|
||||||
|
|
@ -103,4 +105,37 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
|
||||||
assert String.ends_with?(updated_media_item.metadata_filepath, ".info.json")
|
assert String.ends_with?(updated_media_item.metadata_filepath, ".info.json")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,7 @@ defmodule Pinchflat.Metadata.MetadataParserTest do
|
||||||
alias Pinchflat.Metadata.MetadataParser, as: Parser
|
alias Pinchflat.Metadata.MetadataParser, as: Parser
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
json_filepath =
|
{:ok, %{metadata: render_parsed_metadata(:media_metadata)}}
|
||||||
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
|
|
||||||
}}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "parse_for_media_item/1 when testing media metadata" do
|
describe "parse_for_media_item/1 when testing media metadata" do
|
||||||
|
|
|
||||||
|
|
@ -4,19 +4,7 @@ defmodule Pinchflat.Metadata.NfoBuilderTest do
|
||||||
alias Pinchflat.Metadata.NfoBuilder
|
alias Pinchflat.Metadata.NfoBuilder
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
json_filepath =
|
{:ok, %{metadata: render_parsed_metadata(:media_metadata)}}
|
||||||
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}}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "build_and_store_for_media_item/1" do
|
describe "build_and_store_for_media_item/1" do
|
||||||
|
|
|
||||||
|
|
@ -48,4 +48,10 @@ defmodule Pinchflat.TestingHelperMethods do
|
||||||
|
|
||||||
File.read!(json_filepath)
|
File.read!(json_filepath)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_parsed_metadata(metadata_name) do
|
||||||
|
metadata_name
|
||||||
|
|> render_metadata()
|
||||||
|
|> Phoenix.json_library().decode!()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue