diff --git a/lib/pinchflat/metadata/metadata_file_helpers.ex b/lib/pinchflat/metadata/metadata_file_helpers.ex index c4320fa..b728f47 100644 --- a/lib/pinchflat/metadata/metadata_file_helpers.ex +++ b/lib/pinchflat/metadata/metadata_file_helpers.ex @@ -124,6 +124,21 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do end end + @doc """ + Attempts to determine the season and episode number from a media filepath. + + Returns {:ok, {binary(), binary()}} | {:error, :indeterminable} + """ + def season_and_episode_from_media_filepath(media_filepath) do + # matches s + 1 or more digits + e + 1 or more digits (case-insensitive) + season_episode_regex = ~r/s(\d+)e(\d+)/i + + case Regex.scan(season_episode_regex, media_filepath) do + [[_, season, episode] | _] -> {:ok, {season, episode}} + _ -> {:error, :indeterminable} + end + end + defp generate_filepath_for(database_record, filename) do Path.join([ metadata_directory_for(database_record), diff --git a/lib/pinchflat/metadata/nfo_builder.ex b/lib/pinchflat/metadata/nfo_builder.ex index 3db237f..38a26dd 100644 --- a/lib/pinchflat/metadata/nfo_builder.ex +++ b/lib/pinchflat/metadata/nfo_builder.ex @@ -6,8 +6,8 @@ defmodule Pinchflat.Metadata.NfoBuilder do import Pinchflat.Utils.XmlUtils, only: [safe: 1] - alias Pinchflat.Metadata.MetadataFileHelpers alias Pinchflat.Utils.FilesystemUtils + alias Pinchflat.Metadata.MetadataFileHelpers @doc """ Builds an NFO file for a media item (read: single "episode") and @@ -15,12 +15,12 @@ defmodule Pinchflat.Metadata.NfoBuilder do Returns the filepath of the NFO file. """ - def build_and_store_for_media_item(filepath, metadata) do - nfo = build_for_media_item(metadata) + def build_and_store_for_media_item(nfo_filepath, metadata) do + nfo = build_for_media_item(nfo_filepath, metadata) - FilesystemUtils.write_p!(filepath, nfo) + FilesystemUtils.write_p!(nfo_filepath, nfo) - filepath + nfo_filepath end @doc """ @@ -37,10 +37,15 @@ defmodule Pinchflat.Metadata.NfoBuilder do filepath end - defp build_for_media_item(metadata) do + defp build_for_media_item(nfo_filepath, metadata) do upload_date = MetadataFileHelpers.parse_upload_date(metadata["upload_date"]) + # NOTE: the filepath here isn't the path of the media item, it's the path that + # the NFO should be saved to. This works because the NFO's path is the same as + # the media's path, just with a different extension. If this ever changes I'll + # need to pass in the media item's path as well. + {season, episode} = determine_season_and_episode_number(nfo_filepath, upload_date) + # Cribbed from a combination of the Kodi wiki, ytdl-nfo, and ytdl-sub. - # WHO NEEDS A FANCY XML PARSER ANYWAY?! """ @@ -49,8 +54,8 @@ defmodule Pinchflat.Metadata.NfoBuilder do #{safe(metadata["id"])} #{safe(metadata["description"])} #{safe(upload_date)} - #{safe(upload_date.year)} - #{Calendar.strftime(upload_date, "%m%d")} + #{safe(season)} + #{episode} YouTube """ @@ -67,4 +72,11 @@ defmodule Pinchflat.Metadata.NfoBuilder do """ end + + defp determine_season_and_episode_number(filepath, upload_date) do + case MetadataFileHelpers.season_and_episode_from_media_filepath(filepath) do + {:ok, {season, episode}} -> {season, episode} + {:error, _} -> {upload_date.year, Calendar.strftime(upload_date, "%m%d")} + end + end end