diff --git a/.iex.exs b/.iex.exs index d23dd0c..dfe1942 100644 --- a/.iex.exs +++ b/.iex.exs @@ -12,3 +12,39 @@ alias Pinchflat.Profiles alias Pinchflat.MediaSource alias Pinchflat.MediaClient.{SourceDetails, VideoDownloader} + +defmodule IexHelpers do + def playlist_url do + "https://www.youtube.com/playlist?list=PLmqC3wPkeL8kSlTCcSMDD63gmSi7evcXS" + end + + def channel_url do + "https://www.youtube.com/c/TheUselessTrials" + end + + def video_url do + "https://www.youtube.com/watch?v=bR52O78ZIUw" + end + + def details(type) do + source = + case type do + :playlist -> playlist_url() + :channel -> channel_url() + end + + SourceDetails.get_source_details(source) + end + + def ids(type) do + source = + case type do + :playlist -> playlist_url() + :channel -> channel_url() + end + + SourceDetails.get_video_ids(source) + end +end + +import IexHelpers diff --git a/lib/pinchflat/media_client/backends/yt_dlp/video_collection.ex b/lib/pinchflat/media_client/backends/yt_dlp/video_collection.ex index 624689b..8724d8d 100644 --- a/lib/pinchflat/media_client/backends/yt_dlp/video_collection.ex +++ b/lib/pinchflat/media_client/backends/yt_dlp/video_collection.ex @@ -4,8 +4,6 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollection do videos (aka: a source [ie: channels, playlists]). """ - alias Pinchflat.MediaClient.SourceDetails - @doc """ Returns a list of strings representing the video ids in the collection. @@ -28,19 +26,30 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollection do instead we're fetching just the first video (using playlist_end: 1) and parsing the source ID and name from _its_ metadata - Returns {:ok, %SourceDetails{}} | {:error, any, ...}. + Returns {:ok, map()} | {:error, any, ...}. """ def get_source_details(source_url) do - opts = [:skip_download, playlist_end: 1] + opts = [:simulate, :skip_download, playlist_end: 1] + output_template = "%(.{channel,channel_id,playlist_id,playlist_title})j" - with {:ok, output} <- backend_runner().run(source_url, opts, "%(.{channel,channel_id})j"), + with {:ok, output} <- backend_runner().run(source_url, opts, output_template), {:ok, parsed_json} <- Phoenix.json_library().decode(output) do - {:ok, SourceDetails.new(parsed_json["channel_id"], parsed_json["channel"])} + {:ok, format_source_details(parsed_json)} else err -> err end end + # TODO: test + defp format_source_details(response) do + %{ + channel_id: response["channel_id"], + channel_name: response["channel"], + playlist_id: response["playlist_id"], + playlist_name: response["playlist_title"] + } + end + defp backend_runner do Application.get_env(:pinchflat, :yt_dlp_runner) end diff --git a/lib/pinchflat/media_client/source_details.ex b/lib/pinchflat/media_client/source_details.ex index 75c7077..0294600 100644 --- a/lib/pinchflat/media_client/source_details.ex +++ b/lib/pinchflat/media_client/source_details.ex @@ -5,16 +5,9 @@ defmodule Pinchflat.MediaClient.SourceDetails do Technically hardcodes the yt-dlp backend for now, but should leave it open-ish for future expansion (just in case). """ - @enforce_keys [:id, :name] - defstruct [:id, :name] alias Pinchflat.MediaClient.Backends.YtDlp.VideoCollection, as: YtDlpSource - @doc false - def new(id, name) do - %__MODULE__{id: id, name: name} - end - @doc """ Gets a source's ID and name from its URL, using the given backend. diff --git a/lib/pinchflat/media_source.ex b/lib/pinchflat/media_source.ex index 88dddd7..76117e4 100644 --- a/lib/pinchflat/media_source.ex +++ b/lib/pinchflat/media_source.ex @@ -115,7 +115,7 @@ defmodule Pinchflat.MediaSource do %Ecto.Changeset{changes: changes} = changeset case SourceDetails.get_source_details(changes.original_url) do - {:ok, %SourceDetails{} = source_details} -> + {:ok, source_details} -> change_source( source, Map.merge(changes, %{