Add support for episode NFO files (#84)
* Added nfo builder for 'episodes' * Added NFO download fields; hooked up NFO generation to downloading pipeline * Added NFO option to media profile
This commit is contained in:
parent
5bde1205cc
commit
c67278ab5c
17 changed files with 238 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.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
|
||||
|
|
|
|||
|
|
@ -15,12 +15,25 @@ defmodule Pinchflat.Filesystem.FilesystemHelpers do
|
|||
tmpfile_directory = Application.get_env(:pinchflat, :tmpfile_directory)
|
||||
filepath = Path.join([tmpfile_directory, "#{StringUtils.random_string(64)}.#{type}"])
|
||||
|
||||
:ok = File.mkdir_p!(Path.dirname(filepath))
|
||||
:ok = File.write(filepath, "")
|
||||
:ok = write_p!(filepath, "")
|
||||
|
||||
filepath
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes content to a file, creating directories as needed.
|
||||
Takes the same args as File.write!/3.
|
||||
|
||||
Returns :ok | raises on error
|
||||
"""
|
||||
def write_p!(filepath, content, modes \\ []) do
|
||||
filepath
|
||||
|> Path.dirname()
|
||||
|> File.mkdir_p!()
|
||||
|
||||
File.write!(filepath, content, modes)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the file size of a media item and saves it to the database.
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do
|
|||
needed
|
||||
"""
|
||||
|
||||
alias Pinchflat.Filesystem.FilesystemHelpers
|
||||
|
||||
@doc """
|
||||
Compresses and stores metadata for a media item, returning the filepath.
|
||||
|
||||
|
|
@ -18,8 +20,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do
|
|||
filepath = generate_filepath_for(database_record, "metadata.json.gz")
|
||||
{:ok, json} = Phoenix.json_library().encode(metadata_map)
|
||||
|
||||
File.mkdir_p!(Path.dirname(filepath))
|
||||
:ok = File.write(filepath, json, [:compressed])
|
||||
:ok = FilesystemHelpers.write_p!(filepath, json, [:compressed])
|
||||
|
||||
filepath
|
||||
end
|
||||
|
|
@ -45,12 +46,23 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do
|
|||
filepath = generate_filepath_for(database_record, Path.basename(thumbnail_url))
|
||||
thumbnail_blob = fetch_thumbnail_from_url(thumbnail_url)
|
||||
|
||||
File.mkdir_p!(Path.dirname(filepath))
|
||||
:ok = File.write(filepath, thumbnail_blob)
|
||||
:ok = FilesystemHelpers.write_p!(filepath, thumbnail_blob)
|
||||
|
||||
filepath
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses an upload date from the YYYYMMDD string returned in yt-dlp metadata
|
||||
and returns a Date struct.
|
||||
|
||||
Returns Date.t()
|
||||
"""
|
||||
def parse_upload_date(upload_date) do
|
||||
<<year::binary-size(4)>> <> <<month::binary-size(2)>> <> <<day::binary-size(2)>> = upload_date
|
||||
|
||||
Date.from_iso8601!("#{year}-#{month}-#{day}")
|
||||
end
|
||||
|
||||
defp fetch_thumbnail_from_url(url) do
|
||||
http_client = Application.get_env(:pinchflat, :http_client, Pinchflat.HTTP.HTTPClient)
|
||||
{:ok, body} = http_client.get(url, [], body_format: :binary)
|
||||
|
|
|
|||
44
lib/pinchflat/metadata/nfo_builder.ex
Normal file
44
lib/pinchflat/metadata/nfo_builder.ex
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
defmodule Pinchflat.Metadata.NfoBuilder do
|
||||
@moduledoc """
|
||||
Provides methods for building and storing NFO files for
|
||||
use by Kodi/Jellyfin and other media center software.
|
||||
"""
|
||||
|
||||
alias Pinchflat.Metadata.MetadataFileHelpers
|
||||
alias Pinchflat.Filesystem.FilesystemHelpers
|
||||
|
||||
@doc """
|
||||
Builds an NFO file for a media item (read: single "episode") and
|
||||
stores it in the same directory as the media file. Has the same name
|
||||
as the media file, but with a .nfo extension.
|
||||
|
||||
Returns the filepath of the NFO file.
|
||||
"""
|
||||
def build_and_store_for_media_item(metadata) do
|
||||
filepath = Path.rootname(metadata["filepath"]) <> ".nfo"
|
||||
nfo = build_for_media_item(metadata)
|
||||
|
||||
FilesystemHelpers.write_p!(filepath, nfo)
|
||||
|
||||
filepath
|
||||
end
|
||||
|
||||
defp build_for_media_item(metadata) do
|
||||
upload_date = MetadataFileHelpers.parse_upload_date(metadata["upload_date"])
|
||||
# Cribbed from a combination of the Kodi wiki, ytdl-nfo, and ytdl-sub.
|
||||
# WHO NEEDS A FANCY XML PARSER ANYWAY?!
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<episodedetails>
|
||||
<title>#{metadata["title"]}</title>
|
||||
<showtitle>#{metadata["uploader"]}</showtitle>
|
||||
<uniqueid type="youtube" default="true">#{metadata["id"]}</uniqueid>
|
||||
<plot>#{metadata["description"]}</plot>
|
||||
<premiered>#{upload_date}</premiered>
|
||||
<season>#{upload_date.year}</season>
|
||||
<episode>#{Calendar.strftime(upload_date, "%m%d")}</episode>
|
||||
<genre>YouTube</genre>
|
||||
</episodedetails>
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ defmodule Pinchflat.YtDlp.Media do
|
|||
|
||||
alias __MODULE__
|
||||
alias Pinchflat.Utils.FunctionUtils
|
||||
alias Pinchflat.Metadata.MetadataFileHelpers
|
||||
|
||||
@doc """
|
||||
Downloads a single piece of media (and possibly its metadata) directly to its
|
||||
|
|
@ -86,7 +87,7 @@ defmodule Pinchflat.YtDlp.Media do
|
|||
original_url: response["webpage_url"],
|
||||
livestream: response["was_live"],
|
||||
short_form_content: response["webpage_url"] && short_form_content?(response),
|
||||
upload_date: response["upload_date"] && parse_upload_date(response["upload_date"])
|
||||
upload_date: response["upload_date"] && MetadataFileHelpers.parse_upload_date(response["upload_date"])
|
||||
}
|
||||
end
|
||||
|
||||
|
|
@ -106,12 +107,6 @@ defmodule Pinchflat.YtDlp.Media do
|
|||
end
|
||||
end
|
||||
|
||||
defp parse_upload_date(upload_date) do
|
||||
<<year::binary-size(4)>> <> <<month::binary-size(2)>> <> <<day::binary-size(2)>> = upload_date
|
||||
|
||||
Date.from_iso8601!("#{year}-#{month}-#{day}")
|
||||
end
|
||||
|
||||
defp backend_runner do
|
||||
# This approach lets us mock the command for testing
|
||||
Application.get_env(:pinchflat, :yt_dlp_runner)
|
||||
|
|
|
|||
|
|
@ -81,6 +81,12 @@
|
|||
label="Embed Metadata"
|
||||
help="Downloads and embeds metadata in the media file itself, if supported. Uneffected by 'Download Metadata' (recommended)"
|
||||
/>
|
||||
<.input
|
||||
field={f[:download_nfo]}
|
||||
type="toggle"
|
||||
label="Download NFO data"
|
||||
help="Downloads NFO data alongside media file for use with Jellyfin, Kodi, etc."
|
||||
/>
|
||||
|
||||
<h3 class="mt-8 text-2xl text-black dark:text-white">
|
||||
Release Format Options
|
||||
|
|
|
|||
|
|
@ -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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -33,4 +33,27 @@ defmodule Pinchflat.Filesystem.FilesystemHelpersTest do
|
|||
assert {:error, _} = FilesystemHelpers.compute_and_save_media_filesize(media_item)
|
||||
end
|
||||
end
|
||||
|
||||
describe "write_p!/3" do
|
||||
test "writes content to a file" do
|
||||
filepath = FilesystemHelpers.generate_metadata_tmpfile(:json)
|
||||
content = "{}"
|
||||
|
||||
assert :ok = FilesystemHelpers.write_p!(filepath, content)
|
||||
assert File.read!(filepath) == content
|
||||
|
||||
File.rm!(filepath)
|
||||
end
|
||||
|
||||
test "creates directories as needed" do
|
||||
tmpfile_directory = Application.get_env(:pinchflat, :tmpfile_directory)
|
||||
filepath = Path.join([tmpfile_directory, "foo", "bar", "file.json"])
|
||||
content = "{}"
|
||||
|
||||
assert :ok = FilesystemHelpers.write_p!(filepath, content)
|
||||
assert File.read!(filepath) == content
|
||||
|
||||
File.rm!(filepath)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -84,4 +84,12 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
|
|||
assert Path.basename(filepath) == "maxres.webp"
|
||||
end
|
||||
end
|
||||
|
||||
describe "parse_upload_date/1" do
|
||||
test "returns a date from the given metadata upload date" do
|
||||
upload_date = "20210101"
|
||||
|
||||
assert Helpers.parse_upload_date(upload_date) == ~D[2021-01-01]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
38
test/pinchflat/metadata/nfo_builder_test.exs
Normal file
38
test/pinchflat/metadata/nfo_builder_test.exs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
defmodule Pinchflat.Metadata.NfoBuilderTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
alias Pinchflat.Metadata.NfoBuilder
|
||||
|
||||
setup do
|
||||
{:ok, %{metadata: render_parsed_metadata(:media_metadata)}}
|
||||
end
|
||||
|
||||
describe "build_and_store_for_media_item/1" do
|
||||
test "returns the filepath", %{metadata: metadata} do
|
||||
result = NfoBuilder.build_and_store_for_media_item(metadata)
|
||||
|
||||
assert File.exists?(result)
|
||||
|
||||
File.rm!(result)
|
||||
end
|
||||
|
||||
test "builds filepath based on media location", %{metadata: metadata} do
|
||||
result = NfoBuilder.build_and_store_for_media_item(metadata)
|
||||
|
||||
assert String.contains?(result, Path.rootname(metadata["filepath"]))
|
||||
assert String.ends_with?(result, ".nfo")
|
||||
|
||||
File.rm!(result)
|
||||
end
|
||||
|
||||
test "builds an NFO file", %{metadata: metadata} do
|
||||
result = NfoBuilder.build_and_store_for_media_item(metadata)
|
||||
nfo = File.read!(result)
|
||||
|
||||
assert String.contains?(nfo, ~S(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>))
|
||||
assert String.contains?(nfo, "<title>#{metadata["title"]}</title>")
|
||||
|
||||
File.rm!(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue