Fixed filepath bug when parsing metadata (#118)
This commit is contained in:
parent
5e58dcd879
commit
8b5dcc15d6
7 changed files with 9912 additions and 18 deletions
|
|
@ -58,23 +58,35 @@ defmodule Pinchflat.Metadata.MetadataParser do
|
|||
# NOTE: whole ordeal needed due to a bug I found in yt-dlp
|
||||
# https://github.com/yt-dlp/yt-dlp/issues/9445
|
||||
# Can be reverted to remove this entire conditional once fixed
|
||||
filepath =
|
||||
thumbnail_filepath
|
||||
|> String.split(~r{\.}, include_captures: true)
|
||||
|> List.insert_at(-3, "-thumb")
|
||||
|> Enum.join()
|
||||
|
||||
%{
|
||||
thumbnail_filepath:
|
||||
thumbnail_filepath
|
||||
|> String.split(~r{\.}, include_captures: true)
|
||||
|> List.insert_at(-3, "-thumb")
|
||||
|> Enum.join()
|
||||
thumbnail_filepath: filepath_if_exists(filepath)
|
||||
}
|
||||
else
|
||||
%{
|
||||
thumbnail_filepath: thumbnail_filepath
|
||||
thumbnail_filepath: nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_infojson_metadata(metadata) do
|
||||
%{
|
||||
metadata_filepath: metadata["infojson_filename"]
|
||||
metadata_filepath: filepath_if_exists(metadata["infojson_filename"])
|
||||
}
|
||||
end
|
||||
|
||||
defp filepath_if_exists(nil), do: nil
|
||||
|
||||
defp filepath_if_exists(filepath) do
|
||||
if File.exists?(filepath) do
|
||||
filepath
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
|
|||
|
||||
describe "download_for_media_item/3 when testing media_item attributes" do
|
||||
setup do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot ->
|
||||
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot ->
|
||||
{:ok, render_metadata(:media_metadata)}
|
||||
end)
|
||||
|
||||
|
|
@ -94,15 +94,44 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
|
|||
end
|
||||
|
||||
test "it extracts the thumbnail_filepath", %{media_item: media_item} do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot ->
|
||||
metadata = render_parsed_metadata(:media_metadata)
|
||||
|
||||
thumbnail_filepath =
|
||||
metadata["thumbnails"]
|
||||
|> Enum.reverse()
|
||||
|> Enum.find_value(fn attrs -> attrs["filepath"] end)
|
||||
|> String.split(~r{\.}, include_captures: true)
|
||||
|> List.insert_at(-3, "-thumb")
|
||||
|> Enum.join()
|
||||
|
||||
:ok = File.cp(thumbnail_filepath_fixture(), thumbnail_filepath)
|
||||
|
||||
{:ok, Phoenix.json_library().encode!(metadata)}
|
||||
end)
|
||||
|
||||
assert media_item.thumbnail_filepath == nil
|
||||
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
|
||||
assert String.ends_with?(updated_media_item.thumbnail_filepath, ".webp")
|
||||
|
||||
File.rm(updated_media_item.thumbnail_filepath)
|
||||
end
|
||||
|
||||
test "it extracts the metadata_filepath", %{media_item: media_item} do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot ->
|
||||
metadata = render_parsed_metadata(:media_metadata)
|
||||
|
||||
infojson_filepath = metadata["infojson_filename"]
|
||||
:ok = File.cp(infojson_filepath_fixture(), infojson_filepath)
|
||||
|
||||
{:ok, Phoenix.json_library().encode!(metadata)}
|
||||
end)
|
||||
|
||||
assert media_item.metadata_filepath == nil
|
||||
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
|
||||
assert String.ends_with?(updated_media_item.metadata_filepath, ".info.json")
|
||||
|
||||
File.rm(updated_media_item.metadata_filepath)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
defmodule Pinchflat.Metadata.MetadataParserTest do
|
||||
use Pinchflat.DataCase
|
||||
import Pinchflat.MediaFixtures
|
||||
|
||||
alias Pinchflat.Metadata.MetadataParser, as: Parser
|
||||
|
||||
|
|
@ -88,6 +89,22 @@ defmodule Pinchflat.Metadata.MetadataParserTest do
|
|||
end
|
||||
|
||||
describe "parse_for_media_item/1 when testing thumbnail metadata" do
|
||||
setup %{metadata: metadata} do
|
||||
thumbnail_filepath =
|
||||
metadata["thumbnails"]
|
||||
|> Enum.reverse()
|
||||
|> Enum.find_value(fn attrs -> attrs["filepath"] end)
|
||||
|> String.split(~r{\.}, include_captures: true)
|
||||
|> List.insert_at(-3, "-thumb")
|
||||
|> Enum.join()
|
||||
|
||||
:ok = File.cp(thumbnail_filepath_fixture(), thumbnail_filepath)
|
||||
|
||||
on_exit(fn -> File.rm(thumbnail_filepath) end)
|
||||
|
||||
{:ok, filepath: thumbnail_filepath}
|
||||
end
|
||||
|
||||
test "extracts the thumbnail filepath", %{metadata: metadata} do
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
|
|
@ -103,6 +120,14 @@ defmodule Pinchflat.Metadata.MetadataParserTest do
|
|||
assert String.contains?(result.thumbnail_filepath, "-thumb.webp")
|
||||
end
|
||||
|
||||
test "doesn't include thumbnail if the file doesn't exist on-disk", %{metadata: metadata, filepath: filepath} do
|
||||
File.rm(filepath)
|
||||
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert result.thumbnail_filepath == nil
|
||||
end
|
||||
|
||||
test "doesn't freak out if the media has no thumbnails", %{metadata: metadata} do
|
||||
metadata = Map.put(metadata, "thumbnails", %{})
|
||||
|
||||
|
|
@ -121,12 +146,29 @@ defmodule Pinchflat.Metadata.MetadataParserTest do
|
|||
end
|
||||
|
||||
describe "parse_for_media_item/1 when testing infojson metadata" do
|
||||
setup %{metadata: metadata} do
|
||||
infojson_filepath = metadata["infojson_filename"]
|
||||
:ok = File.cp(infojson_filepath_fixture(), infojson_filepath)
|
||||
|
||||
on_exit(fn -> File.rm(infojson_filepath) end)
|
||||
|
||||
{:ok, filepath: infojson_filepath}
|
||||
end
|
||||
|
||||
test "extracts the metadata filepath", %{metadata: metadata} do
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert String.ends_with?(result.metadata_filepath, ".info.json")
|
||||
end
|
||||
|
||||
test "doesn't include metadata if the file doesn't exist on-disk", %{metadata: metadata, filepath: filepath} do
|
||||
File.rm(filepath)
|
||||
|
||||
result = Parser.parse_for_media_item(metadata)
|
||||
|
||||
assert result.metadata_filepath == nil
|
||||
end
|
||||
|
||||
test "doesn't freak out if the media has no infojson", %{metadata: metadata} do
|
||||
metadata = Map.put(metadata, "infojson_filename", nil)
|
||||
|
||||
|
|
|
|||
9788
test/support/files/example.info.json
Normal file
9788
test/support/files/example.info.json
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -1513,7 +1513,7 @@
|
|||
"url": "https://i.ytimg.com/vi_webp/ABC123/maxresdefault.webp",
|
||||
"preference": 0,
|
||||
"id": "41",
|
||||
"filepath": "/app/tmp/videos/ABC123/Pinchflat Example Video-ABC123.webp"
|
||||
"filepath": "/tmp/test/tmpfiles/Pinchflat Example Video-ABC123.webp"
|
||||
}
|
||||
],
|
||||
"thumbnail": "https://i.ytimg.com/vi/ABC123/maxresdefault.jpg",
|
||||
|
|
@ -9769,7 +9769,7 @@
|
|||
"release_git_head": "5498729c59b03a9511c64552da3ba2f802166f8d",
|
||||
"repository": "yt-dlp/yt-dlp-nightly-builds"
|
||||
},
|
||||
"infojson_filename": "/app/tmp/videos/ABC123/Pinchflat Example Video-ABC123.info.json",
|
||||
"infojson_filename": "/tmp/test/tmpfiles/Pinchflat Example Video-ABC123.info.json",
|
||||
"__infojson_filename": "/app/tmp/videos/ABC123/Pinchflat Example Video-ABC123.info.json",
|
||||
"__real_download": true,
|
||||
"__files_to_merge": [
|
||||
|
|
|
|||
BIN
test/support/files/thumbnail.jpg
Normal file
BIN
test/support/files/thumbnail.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 824 B |
|
|
@ -52,14 +52,7 @@ defmodule Pinchflat.MediaFixtures do
|
|||
"#{:rand.uniform(1_000_000)}_media.mkv"
|
||||
])
|
||||
|
||||
fixture_media_filepath =
|
||||
Path.join([
|
||||
File.cwd!(),
|
||||
"test",
|
||||
"support",
|
||||
"files",
|
||||
"media.mkv"
|
||||
])
|
||||
fixture_media_filepath = media_filepath_fixture()
|
||||
|
||||
:ok = File.mkdir_p(Path.dirname(stored_media_filepath))
|
||||
:ok = File.cp(fixture_media_filepath, stored_media_filepath)
|
||||
|
|
@ -82,4 +75,34 @@ defmodule Pinchflat.MediaFixtures do
|
|||
|
||||
Phoenix.json_library().encode!(media_attributes)
|
||||
end
|
||||
|
||||
def media_filepath_fixture do
|
||||
Path.join([
|
||||
File.cwd!(),
|
||||
"test",
|
||||
"support",
|
||||
"files",
|
||||
"media.mkv"
|
||||
])
|
||||
end
|
||||
|
||||
def thumbnail_filepath_fixture do
|
||||
Path.join([
|
||||
File.cwd!(),
|
||||
"test",
|
||||
"support",
|
||||
"files",
|
||||
"thumbnail.jpg"
|
||||
])
|
||||
end
|
||||
|
||||
def infojson_filepath_fixture do
|
||||
Path.join([
|
||||
File.cwd!(),
|
||||
"test",
|
||||
"support",
|
||||
"files",
|
||||
"example.info.json"
|
||||
])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue