@@ -100,6 +100,10 @@ defmodule Pinchflat.MediaSource do
100
100
Note that this fetches source details as long as the `original_url` is present.
101
101
This means that it'll go for it even if a changeset is otherwise invalid. This
102
102
is pretty easy to change, but for MVP I'm not concerned.
103
+
104
+ IDEA: Maybe I could discern `collection_type` based on the original URL?
105
+ It also seems like it's a channel when the returned yt-dlp channel_id is the
106
+ same as the playlist_id - maybe could use that?
103
107
"""
104
108
def change_source_from_url ( % Source { } = source , attrs ) do
105
109
case change_source ( source , attrs ) do
@@ -115,14 +119,8 @@ defmodule Pinchflat.MediaSource do
115
119
% Ecto.Changeset { changes: changes } = changeset
116
120
117
121
case SourceDetails . get_source_details ( changes . original_url ) do
118
- { :ok , % SourceDetails { } = source_details } ->
119
- change_source (
120
- source ,
121
- Map . merge ( changes , % {
122
- name: source_details . name ,
123
- collection_id: source_details . id
124
- } )
125
- )
122
+ { :ok , source_details } ->
123
+ add_source_details_by_collection_type ( source , changeset , source_details )
126
124
127
125
{ :error , runner_error , _status_code } ->
128
126
Ecto.Changeset . add_error (
@@ -134,15 +132,35 @@ defmodule Pinchflat.MediaSource do
134
132
end
135
133
end
136
134
137
- defp commit_and_start_indexing ( changeset ) do
138
- case Repo . insert_or_update ( changeset ) do
139
- { :ok , % Source { } = source } ->
140
- maybe_run_indexing_task ( changeset , source )
135
+ defp add_source_details_by_collection_type ( source , changeset , source_details ) do
136
+ % Ecto.Changeset { changes: changes } = changeset
137
+ collection_type = source . collection_type || changes [ :collection_type ]
138
+
139
+ collection_changes =
140
+ case collection_type do
141
+ :channel ->
142
+ % {
143
+ collection_id: source_details . channel_id ,
144
+ collection_name: source_details . channel_name
145
+ }
146
+
147
+ :playlist ->
148
+ % {
149
+ collection_id: source_details . playlist_id ,
150
+ collection_name: source_details . playlist_name
151
+ }
152
+
153
+ _ ->
154
+ % { }
155
+ end
141
156
142
- { :ok , source }
157
+ change_source ( source , Map . merge ( changes , collection_changes ) )
158
+ end
143
159
144
- err ->
145
- err
160
+ defp commit_and_start_indexing ( changeset ) do
161
+ case Repo . insert_or_update ( changeset ) do
162
+ { :ok , % Source { } = source } -> maybe_run_indexing_task ( changeset , source )
163
+ err -> err
146
164
end
147
165
end
148
166
@@ -159,5 +177,7 @@ defmodule Pinchflat.MediaSource do
159
177
SourceTasks . kickoff_indexing_task ( source )
160
178
end
161
179
end
180
+
181
+ { :ok , source }
162
182
end
163
183
end
0 commit comments