[WIP] updated the output of VideoCollection to include playlists
This commit is contained in:
parent
bdef6c75bb
commit
bb2ca0fb30
4 changed files with 52 additions and 14 deletions
36
.iex.exs
36
.iex.exs
|
|
@ -12,3 +12,39 @@ alias Pinchflat.Profiles
|
||||||
alias Pinchflat.MediaSource
|
alias Pinchflat.MediaSource
|
||||||
|
|
||||||
alias Pinchflat.MediaClient.{SourceDetails, VideoDownloader}
|
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
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollection do
|
||||||
videos (aka: a source [ie: channels, playlists]).
|
videos (aka: a source [ie: channels, playlists]).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias Pinchflat.MediaClient.SourceDetails
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns a list of strings representing the video ids in the collection.
|
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)
|
instead we're fetching just the first video (using playlist_end: 1)
|
||||||
and parsing the source ID and name from _its_ metadata
|
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
|
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, 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
|
else
|
||||||
err -> err
|
err -> err
|
||||||
end
|
end
|
||||||
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
|
defp backend_runner do
|
||||||
Application.get_env(:pinchflat, :yt_dlp_runner)
|
Application.get_env(:pinchflat, :yt_dlp_runner)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,9 @@ defmodule Pinchflat.MediaClient.SourceDetails do
|
||||||
Technically hardcodes the yt-dlp backend for now, but should leave
|
Technically hardcodes the yt-dlp backend for now, but should leave
|
||||||
it open-ish for future expansion (just in case).
|
it open-ish for future expansion (just in case).
|
||||||
"""
|
"""
|
||||||
@enforce_keys [:id, :name]
|
|
||||||
defstruct [:id, :name]
|
|
||||||
|
|
||||||
alias Pinchflat.MediaClient.Backends.YtDlp.VideoCollection, as: YtDlpSource
|
alias Pinchflat.MediaClient.Backends.YtDlp.VideoCollection, as: YtDlpSource
|
||||||
|
|
||||||
@doc false
|
|
||||||
def new(id, name) do
|
|
||||||
%__MODULE__{id: id, name: name}
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a source's ID and name from its URL, using the given backend.
|
Gets a source's ID and name from its URL, using the given backend.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ defmodule Pinchflat.MediaSource do
|
||||||
%Ecto.Changeset{changes: changes} = changeset
|
%Ecto.Changeset{changes: changes} = changeset
|
||||||
|
|
||||||
case SourceDetails.get_source_details(changes.original_url) do
|
case SourceDetails.get_source_details(changes.original_url) do
|
||||||
{:ok, %SourceDetails{} = source_details} ->
|
{:ok, source_details} ->
|
||||||
change_source(
|
change_source(
|
||||||
source,
|
source,
|
||||||
Map.merge(changes, %{
|
Map.merge(changes, %{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue