Skip to content

Commit bdef6c7

Browse files
authored
Change Channel to Source (#12)
* Ensure channel detail lookup doesn't download the video - oops * Ran the needful migrations for replacing channels with sources * got media source test back working * channel tasks test * Media indexing worker test * media tests * Got all tests working except controller tests * got all tests working * Renamed Channel struct to Source * renamed ChannelTasks * More renaming; renamed channel details module * Removed Channel yt-dlp module, instead throwing things in the VideoCollection module * Renamed what looks like the last of the outstanding data
1 parent d25e65f commit bdef6c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+806
-763
lines changed

.iex.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ alias Pinchflat.Repo
33
alias Pinchflat.Tasks.Task
44
alias Pinchflat.Media.MediaItem
55
alias Pinchflat.Media.MediaMetadata
6-
alias Pinchflat.MediaSource.Channel
6+
alias Pinchflat.MediaSource.Source
77
alias Pinchflat.Profiles.MediaProfile
88

99
alias Pinchflat.Tasks
1010
alias Pinchflat.Media
1111
alias Pinchflat.Profiles
1212
alias Pinchflat.MediaSource
1313

14-
alias Pinchflat.MediaClient.{ChannelDetails, VideoDownloader}
14+
alias Pinchflat.MediaClient.{SourceDetails, VideoDownloader}

lib/pinchflat/media.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Pinchflat.Media do
88
alias Pinchflat.Repo
99
alias Pinchflat.Tasks
1010
alias Pinchflat.Media.MediaItem
11-
alias Pinchflat.MediaSource.Channel
11+
alias Pinchflat.MediaSource.Source
1212

1313
@doc """
1414
Returns the list of media_items. Returns [%MediaItem{}, ...].
@@ -18,15 +18,15 @@ defmodule Pinchflat.Media do
1818
end
1919

2020
@doc """
21-
Returns a list of pending media_items for a given channel, where
21+
Returns a list of pending media_items for a given source, where
2222
pending means the `media_filepath` is `nil`.
2323
2424
Returns [%MediaItem{}, ...].
2525
"""
26-
def list_pending_media_items_for(%Channel{} = channel) do
26+
def list_pending_media_items_for(%Source{} = source) do
2727
from(
2828
m in MediaItem,
29-
where: m.channel_id == ^channel.id and is_nil(m.media_filepath)
29+
where: m.source_id == ^source.id and is_nil(m.media_filepath)
3030
)
3131
|> Repo.all()
3232
end

lib/pinchflat/media/media_item.ex

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ defmodule Pinchflat.Media.MediaItem do
77
import Ecto.Changeset
88

99
alias Pinchflat.Tasks.Task
10-
alias Pinchflat.MediaSource.Channel
10+
alias Pinchflat.MediaSource.Source
1111
alias Pinchflat.Media.MediaMetadata
1212

13-
@required_fields ~w(media_id channel_id)a
14-
@allowed_fields ~w(title media_id media_filepath channel_id subtitle_filepaths)a
13+
@required_fields ~w(media_id source_id)a
14+
@allowed_fields ~w(title media_id media_filepath source_id subtitle_filepaths)a
1515

1616
schema "media_items" do
1717
field :title, :string
@@ -22,7 +22,7 @@ defmodule Pinchflat.Media.MediaItem do
2222
# Will very likely revisit because I can't leave well-enough alone.
2323
field :subtitle_filepaths, {:array, {:array, :string}}, default: []
2424

25-
belongs_to :channel, Channel
25+
belongs_to :source, Source
2626

2727
has_one :metadata, MediaMetadata, on_replace: :update
2828

@@ -37,6 +37,6 @@ defmodule Pinchflat.Media.MediaItem do
3737
|> cast(attrs, @allowed_fields)
3838
|> cast_assoc(:metadata, with: &MediaMetadata.changeset/2, required: false)
3939
|> validate_required(@required_fields)
40-
|> unique_constraint([:media_id, :channel_id])
40+
|> unique_constraint([:media_id, :source_id])
4141
end
4242
end

lib/pinchflat/media_client/backends/yt_dlp/channel.ex

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
defmodule Pinchflat.MediaClient.Backends.YtDlp.VideoCollection do
22
@moduledoc """
3-
Contains utilities for working with collections of videos (ie: channels, playlists).
3+
Contains utilities for working with collections of
4+
videos (aka: a source [ie: channels, playlists]).
5+
"""
6+
7+
alias Pinchflat.MediaClient.SourceDetails
8+
9+
@doc """
10+
Returns a list of strings representing the video ids in the collection.
11+
12+
Returns {:ok, [binary()]} | {:error, any, ...}.
13+
"""
14+
def get_video_ids(url, command_opts \\ []) do
15+
runner = Application.get_env(:pinchflat, :yt_dlp_runner)
16+
opts = command_opts ++ [:simulate, :skip_download]
17+
18+
case runner.run(url, opts, "%(id)s") do
19+
{:ok, output} -> {:ok, String.split(output, "\n", trim: true)}
20+
res -> res
21+
end
22+
end
23+
24+
@doc """
25+
Gets a source's ID and name from its URL.
426
5-
Meant to be included in other modules but can be used on its own. Channels and playlists
6-
will have many of their own methods, but also share a lot of methods. This module is for
7-
those shared methods.
27+
yt-dlp does not _really_ have source-specific functions, so
28+
instead we're fetching just the first video (using playlist_end: 1)
29+
and parsing the source ID and name from _its_ metadata
30+
31+
Returns {:ok, %SourceDetails{}} | {:error, any, ...}.
832
"""
33+
def get_source_details(source_url) do
34+
opts = [:skip_download, playlist_end: 1]
935

10-
defmacro __using__(_) do
11-
quote do
12-
@doc """
13-
Returns a list of strings representing the video ids in the collection.
14-
15-
Returns {:ok, [binary()]} | {:error, any, ...}.
16-
"""
17-
def get_video_ids(url, command_opts \\ []) do
18-
runner = Application.get_env(:pinchflat, :yt_dlp_runner)
19-
opts = command_opts ++ [:simulate, :skip_download]
20-
21-
case runner.run(url, opts, "%(id)s") do
22-
{:ok, output} -> {:ok, String.split(output, "\n", trim: true)}
23-
res -> res
24-
end
25-
end
36+
with {:ok, output} <- backend_runner().run(source_url, opts, "%(.{channel,channel_id})j"),
37+
{:ok, parsed_json} <- Phoenix.json_library().decode(output) do
38+
{:ok, SourceDetails.new(parsed_json["channel_id"], parsed_json["channel"])}
39+
else
40+
err -> err
2641
end
2742
end
43+
44+
defp backend_runner do
45+
Application.get_env(:pinchflat, :yt_dlp_runner)
46+
end
2847
end

lib/pinchflat/media_client/channel_details.ex

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
defmodule Pinchflat.MediaClient.SourceDetails do
2+
@moduledoc """
3+
This is the integration layer for actually working with sources.
4+
5+
Technically hardcodes the yt-dlp backend for now, but should leave
6+
it open-ish for future expansion (just in case).
7+
"""
8+
@enforce_keys [:id, :name]
9+
defstruct [:id, :name]
10+
11+
alias Pinchflat.MediaClient.Backends.YtDlp.VideoCollection, as: YtDlpSource
12+
13+
@doc false
14+
def new(id, name) do
15+
%__MODULE__{id: id, name: name}
16+
end
17+
18+
@doc """
19+
Gets a source's ID and name from its URL, using the given backend.
20+
21+
Returns {:ok, map()} | {:error, any, ...}.
22+
"""
23+
def get_source_details(source_url, backend \\ :yt_dlp) do
24+
source_module(backend).get_source_details(source_url)
25+
end
26+
27+
@doc """
28+
Returns a list of video IDs for the given source URL, using the given backend.
29+
30+
Returns {:ok, list(binary())} | {:error, any, ...}.
31+
"""
32+
def get_video_ids(source_url, backend \\ :yt_dlp) do
33+
source_module(backend).get_video_ids(source_url)
34+
end
35+
36+
defp source_module(backend) do
37+
case backend do
38+
:yt_dlp -> YtDlpSource
39+
end
40+
end
41+
end

lib/pinchflat/media_client/video_downloader.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ defmodule Pinchflat.MediaClient.VideoDownloader do
2525
Returns {:ok, %MediaItem{}} | {:error, any, ...any}
2626
"""
2727
def download_for_media_item(%MediaItem{} = media_item, backend \\ :yt_dlp) do
28-
item_with_preloads = Repo.preload(media_item, [:metadata, channel: :media_profile])
29-
media_profile = item_with_preloads.channel.media_profile
28+
item_with_preloads = Repo.preload(media_item, [:metadata, source: :media_profile])
29+
media_profile = item_with_preloads.source.media_profile
3030

3131
case download_for_media_profile(media_item.media_id, media_profile, backend) do
3232
{:ok, parsed_json} ->

0 commit comments

Comments
 (0)