[Enhancement] Add option for a source to only use cookies when needed (#640)
* Updated model with new attribute * Update app logic to use new cookie logic * lots of tests * Updated UI and renamed attribute * Updated tests
This commit is contained in:
parent
59f8aa69cd
commit
ac895944a8
17 changed files with 247 additions and 65 deletions
|
|
@ -9,6 +9,7 @@ defmodule Pinchflat.Downloading.MediaDownloader do
|
|||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Sources
|
||||
alias Pinchflat.Media.MediaItem
|
||||
alias Pinchflat.Utils.StringUtils
|
||||
alias Pinchflat.Metadata.NfoBuilder
|
||||
|
|
@ -151,7 +152,8 @@ defmodule Pinchflat.Downloading.MediaDownloader do
|
|||
|
||||
defp download_with_options(url, item_with_preloads, output_filepath, override_opts) do
|
||||
{:ok, options} = DownloadOptionBuilder.build(item_with_preloads, override_opts)
|
||||
use_cookies = item_with_preloads.source.use_cookies
|
||||
|
||||
use_cookies = Sources.use_cookies?(item_with_preloads.source, :downloading)
|
||||
runner_opts = [output_filepath: output_filepath, use_cookies: use_cookies]
|
||||
|
||||
case YtDlpMedia.get_downloadable_status(url, use_cookies: use_cookies) do
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do
|
|||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Tasks
|
||||
alias Pinchflat.Sources
|
||||
alias Pinchflat.Sources.Source
|
||||
alias Pinchflat.FastIndexing.YoutubeRss
|
||||
alias Pinchflat.FastIndexing.YoutubeApi
|
||||
|
|
@ -88,12 +89,16 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do
|
|||
|
||||
defp create_media_item_from_media_id(source, media_id) do
|
||||
url = "https://www.youtube.com/watch?v=#{media_id}"
|
||||
# This is set to :metadata instead of :indexing since this happens _after_ the
|
||||
# actual indexing process. In reality, slow indexing is the only thing that
|
||||
# should be using :indexing.
|
||||
should_use_cookies = Sources.use_cookies?(source, :metadata)
|
||||
|
||||
command_opts =
|
||||
[output: DownloadOptionBuilder.build_output_path_for(source)] ++
|
||||
DownloadOptionBuilder.build_quality_options_for(source)
|
||||
|
||||
case YtDlpMedia.get_media_attributes(url, command_opts, use_cookies: source.use_cookies) do
|
||||
case YtDlpMedia.get_media_attributes(url, command_opts, use_cookies: should_use_cookies) do
|
||||
{:ok, media_attrs} ->
|
||||
Media.create_media_item_from_backend_attrs(source, media_attrs)
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do
|
|||
needed
|
||||
"""
|
||||
|
||||
alias Pinchflat.Sources
|
||||
alias Pinchflat.Utils.FilesystemUtils
|
||||
|
||||
alias Pinchflat.YtDlp.Media, as: YtDlpMedia
|
||||
|
|
@ -66,7 +67,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do
|
|||
yt_dlp_filepath = generate_filepath_for(media_item_with_preloads, "thumbnail.%(ext)s")
|
||||
real_filepath = generate_filepath_for(media_item_with_preloads, "thumbnail.jpg")
|
||||
command_opts = [output: yt_dlp_filepath]
|
||||
addl_opts = [use_cookies: media_item_with_preloads.source.use_cookies]
|
||||
addl_opts = [use_cookies: Sources.use_cookies?(media_item_with_preloads.source, :metadata)]
|
||||
|
||||
case YtDlpMedia.download_thumbnail(media_item_with_preloads.original_url, command_opts, addl_opts) do
|
||||
{:ok, _} -> real_filepath
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorker do
|
|||
defp determine_series_directory(source) do
|
||||
output_path = DownloadOptionBuilder.build_output_path_for(source)
|
||||
runner_opts = [output: output_path]
|
||||
addl_opts = [use_cookies: source.use_cookies]
|
||||
addl_opts = [use_cookies: Sources.use_cookies?(source, :metadata)]
|
||||
{:ok, %{filepath: filepath}} = MediaCollection.get_source_details(source.original_url, runner_opts, addl_opts)
|
||||
|
||||
case MetadataFileHelpers.series_directory_from_media_filepath(filepath) do
|
||||
|
|
@ -113,6 +113,7 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorker do
|
|||
defp fetch_metadata_for_source(source) do
|
||||
tmp_output_path = "#{tmp_directory()}/#{StringUtils.random_string(16)}/source_image.%(ext)S"
|
||||
base_opts = [convert_thumbnails: "jpg", output: tmp_output_path]
|
||||
should_use_cookies = Sources.use_cookies?(source, :metadata)
|
||||
|
||||
opts =
|
||||
if source.collection_type == :channel do
|
||||
|
|
@ -121,7 +122,7 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorker do
|
|||
base_opts ++ [:write_thumbnail, playlist_items: 1]
|
||||
end
|
||||
|
||||
MediaCollection.get_source_metadata(source.original_url, opts, use_cookies: source.use_cookies)
|
||||
MediaCollection.get_source_metadata(source.original_url, opts, use_cookies: should_use_cookies)
|
||||
end
|
||||
|
||||
defp tmp_directory do
|
||||
|
|
|
|||
|
|
@ -132,13 +132,14 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do
|
|||
{:ok, pid} = FileFollowerServer.start_link()
|
||||
|
||||
handler = fn filepath -> setup_file_follower_watcher(pid, filepath, source) end
|
||||
should_use_cookies = Sources.use_cookies?(source, :indexing)
|
||||
|
||||
command_opts =
|
||||
[output: DownloadOptionBuilder.build_output_path_for(source)] ++
|
||||
DownloadOptionBuilder.build_quality_options_for(source) ++
|
||||
build_download_archive_options(source, was_forced)
|
||||
|
||||
runner_opts = [file_listener_handler: handler, use_cookies: source.use_cookies]
|
||||
runner_opts = [file_listener_handler: handler, use_cookies: should_use_cookies]
|
||||
result = MediaCollection.get_media_attributes_for_collection(source.original_url, command_opts, runner_opts)
|
||||
|
||||
FileFollowerServer.stop(pid)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ defmodule Pinchflat.Sources.Source do
|
|||
series_directory
|
||||
index_frequency_minutes
|
||||
fast_index
|
||||
use_cookies
|
||||
cookie_behaviour
|
||||
download_media
|
||||
last_indexed_at
|
||||
original_url
|
||||
|
|
@ -78,7 +78,7 @@ defmodule Pinchflat.Sources.Source do
|
|||
field :collection_type, Ecto.Enum, values: [:channel, :playlist]
|
||||
field :index_frequency_minutes, :integer, default: 60 * 24
|
||||
field :fast_index, :boolean, default: false
|
||||
field :use_cookies, :boolean, default: false
|
||||
field :cookie_behaviour, Ecto.Enum, values: [:disabled, :when_needed, :all_operations], default: :disabled
|
||||
field :download_media, :boolean, default: true
|
||||
field :last_indexed_at, :utc_datetime
|
||||
# Only download media items that were published after this date
|
||||
|
|
|
|||
|
|
@ -32,6 +32,19 @@ defmodule Pinchflat.Sources do
|
|||
source.output_path_template_override || media_profile.output_path_template
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a boolean indicating whether or not cookies should be used for a given operation.
|
||||
|
||||
Returns boolean()
|
||||
"""
|
||||
def use_cookies?(source, operation) when operation in [:indexing, :downloading, :metadata] do
|
||||
case source.cookie_behaviour do
|
||||
:disabled -> false
|
||||
:all_operations -> true
|
||||
:when_needed -> operation == :indexing
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the list of sources. Returns [%Source{}, ...]
|
||||
"""
|
||||
|
|
@ -181,9 +194,9 @@ defmodule Pinchflat.Sources do
|
|||
|
||||
defp add_source_details_to_changeset(source, changeset) do
|
||||
original_url = changeset.changes.original_url
|
||||
use_cookies = Ecto.Changeset.get_field(changeset, :use_cookies)
|
||||
should_use_cookies = Ecto.Changeset.get_field(changeset, :cookie_behaviour) == :all_operations
|
||||
# Skipping sleep interval since this is UI blocking and we want to keep this as fast as possible
|
||||
addl_opts = [use_cookies: use_cookies, skip_sleep_interval: true]
|
||||
addl_opts = [use_cookies: should_use_cookies, skip_sleep_interval: true]
|
||||
|
||||
case MediaCollection.get_source_details(original_url, [], addl_opts) do
|
||||
{:ok, source_details} ->
|
||||
|
|
|
|||
|
|
@ -27,6 +27,14 @@ defmodule PinchflatWeb.Sources.SourceHTML do
|
|||
]
|
||||
end
|
||||
|
||||
def friendly_cookie_behaviours do
|
||||
[
|
||||
{"Disabled", :disabled},
|
||||
{"When Needed", :when_needed},
|
||||
{"All Operations", :all_operations}
|
||||
]
|
||||
end
|
||||
|
||||
def cutoff_date_presets do
|
||||
[
|
||||
{"7 days", compute_date_offset(7)},
|
||||
|
|
|
|||
|
|
@ -87,10 +87,11 @@
|
|||
/>
|
||||
|
||||
<.input
|
||||
field={f[:use_cookies]}
|
||||
type="toggle"
|
||||
label="Use Cookies for Downloading"
|
||||
help="Uses your YouTube cookies for this source (if configured). Used for downloading private playlists and videos. See docs for important details"
|
||||
field={f[:cookie_behaviour]}
|
||||
options={friendly_cookie_behaviours()}
|
||||
type="select"
|
||||
label="Cookie Behaviour"
|
||||
help="Uses your YouTube cookies for this source (if configured). 'When Needed' tries to minimize cookie usage except for certain indexing and downloading tasks. See docs"
|
||||
/>
|
||||
|
||||
<section x-show="advancedMode">
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 497 KiB After Width: | Height: | Size: 497 KiB |
|
|
@ -0,0 +1,18 @@
|
|||
defmodule Pinchflat.Repo.Migrations.AddCookieBehaviourToSources do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table(:sources) do
|
||||
add :cookie_behaviour, :string, null: false, default: "disabled"
|
||||
end
|
||||
|
||||
execute(
|
||||
"UPDATE sources SET cookie_behaviour = 'all_operations' WHERE use_cookies = TRUE",
|
||||
"UPDATE sources SET use_cookies = TRUE WHERE cookie_behaviour = 'all_operations'"
|
||||
)
|
||||
|
||||
alter table(:sources) do
|
||||
remove :use_cookies, :boolean, null: false, default: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -157,15 +157,16 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
|
|||
{:ok, ""}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: true})
|
||||
source = source_fixture(%{cookie_behaviour: :all_operations})
|
||||
media_item = media_item_fixture(%{source_id: source.id})
|
||||
|
||||
assert {:ok, _} = MediaDownloader.download_for_media_item(media_item)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source does not use cookies" do
|
||||
test "does not set use_cookies if the source uses cookies when needed" do
|
||||
expect(YtDlpRunnerMock, :run, 3, fn
|
||||
_url, :get_downloadable_status, _opts, _ot, _addl ->
|
||||
_url, :get_downloadable_status, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
{:ok, "{}"}
|
||||
|
||||
_url, :download, _opts, _ot, addl ->
|
||||
|
|
@ -176,7 +177,27 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
|
|||
{:ok, ""}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: false})
|
||||
source = source_fixture(%{cookie_behaviour: :when_needed})
|
||||
media_item = media_item_fixture(%{source_id: source.id})
|
||||
|
||||
assert {:ok, _} = MediaDownloader.download_for_media_item(media_item)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source does not use cookies" do
|
||||
expect(YtDlpRunnerMock, :run, 3, fn
|
||||
_url, :get_downloadable_status, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
{:ok, "{}"}
|
||||
|
||||
_url, :download, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
{:ok, render_metadata(:media_metadata)}
|
||||
|
||||
_url, :download_thumbnail, _opts, _ot, _addl ->
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{cookie_behaviour: :disabled})
|
||||
media_item = media_item_fixture(%{source_id: source.id})
|
||||
|
||||
assert {:ok, _} = MediaDownloader.download_for_media_item(media_item)
|
||||
|
|
|
|||
|
|
@ -104,34 +104,6 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
|||
FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "sets use_cookies if the source uses cookies" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
|
||||
assert {:use_cookies, true} in addl
|
||||
|
||||
{:ok, media_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: true})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source does not use cookies" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
|
||||
{:ok, media_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: false})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not enqueue a download job if the media item does not match the format rules" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
|
|
@ -180,6 +152,50 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "index_and_kickoff_downloads/1 when testing cookies" do
|
||||
test "sets use_cookies if the source uses cookies" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
|
||||
assert {:use_cookies, true} in addl
|
||||
|
||||
{:ok, media_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{cookie_behaviour: :all_operations})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source uses cookies when needed" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
|
||||
{:ok, media_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{cookie_behaviour: :when_needed})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source does not use cookies" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
|
||||
{:ok, media_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{cookie_behaviour: :disabled})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
end
|
||||
|
||||
describe "index_and_kickoff_downloads/1 when testing backends" do
|
||||
test "uses the YouTube API if it is enabled", %{source: source} do
|
||||
expect(HTTPClientMock, :get, fn url, _headers ->
|
||||
|
|
|
|||
|
|
@ -88,13 +88,35 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
|
|||
Helpers.download_and_store_thumbnail_for(media_item)
|
||||
end
|
||||
|
||||
test "returns nil if yt-dlp fails", %{media_item: media_item} do
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:error, "error"} end)
|
||||
|
||||
filepath = Helpers.download_and_store_thumbnail_for(media_item)
|
||||
|
||||
assert filepath == nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "download_and_store_thumbnail_for/2 when testing cookie usage" do
|
||||
test "sets use_cookies if the source uses cookies" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, addl ->
|
||||
assert {:use_cookies, true} in addl
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: true})
|
||||
source = source_fixture(%{cookie_behaviour: :all_operations})
|
||||
media_item = Repo.preload(media_item_fixture(%{source_id: source.id}), :source)
|
||||
|
||||
Helpers.download_and_store_thumbnail_for(media_item)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source uses cookies when needed" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{cookie_behaviour: :when_needed})
|
||||
media_item = Repo.preload(media_item_fixture(%{source_id: source.id}), :source)
|
||||
|
||||
Helpers.download_and_store_thumbnail_for(media_item)
|
||||
|
|
@ -106,19 +128,11 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
|
|||
{:ok, ""}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: false})
|
||||
source = source_fixture(%{cookie_behaviour: :disabled})
|
||||
media_item = Repo.preload(media_item_fixture(%{source_id: source.id}), :source)
|
||||
|
||||
Helpers.download_and_store_thumbnail_for(media_item)
|
||||
end
|
||||
|
||||
test "returns nil if yt-dlp fails", %{media_item: media_item} do
|
||||
stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:error, "error"} end)
|
||||
|
||||
filepath = Helpers.download_and_store_thumbnail_for(media_item)
|
||||
|
||||
assert filepath == nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "parse_upload_date/1" do
|
||||
|
|
|
|||
|
|
@ -254,7 +254,23 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
|
|||
end)
|
||||
|
||||
profile = media_profile_fixture(%{download_source_images: true})
|
||||
source = source_fixture(media_profile_id: profile.id, use_cookies: true)
|
||||
source = source_fixture(media_profile_id: profile.id, cookie_behaviour: :all_operations)
|
||||
|
||||
perform_job(SourceMetadataStorageWorker, %{id: source.id})
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source uses cookies when needed" do
|
||||
expect(YtDlpRunnerMock, :run, 2, fn
|
||||
_url, :get_source_details, _opts, _ot, _addl ->
|
||||
{:ok, source_details_return_fixture()}
|
||||
|
||||
_url, :get_source_metadata, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
{:ok, render_metadata(:channel_source_metadata)}
|
||||
end)
|
||||
|
||||
profile = media_profile_fixture(%{download_source_images: true})
|
||||
source = source_fixture(media_profile_id: profile.id, cookie_behaviour: :when_needed)
|
||||
|
||||
perform_job(SourceMetadataStorageWorker, %{id: source.id})
|
||||
end
|
||||
|
|
@ -270,7 +286,7 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
|
|||
end)
|
||||
|
||||
profile = media_profile_fixture(%{download_source_images: true})
|
||||
source = source_fixture(media_profile_id: profile.id, use_cookies: false)
|
||||
source = source_fixture(media_profile_id: profile.id, cookie_behaviour: :disabled)
|
||||
|
||||
perform_job(SourceMetadataStorageWorker, %{id: source.id})
|
||||
end
|
||||
|
|
@ -323,7 +339,21 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
|
|||
{:ok, "{}"}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{series_directory: nil, use_cookies: true})
|
||||
source = source_fixture(%{series_directory: nil, cookie_behaviour: :all_operations})
|
||||
perform_job(SourceMetadataStorageWorker, %{id: source.id})
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source uses cookies when needed" do
|
||||
expect(YtDlpRunnerMock, :run, 2, fn
|
||||
_url, :get_source_details, _opts, _ot, addl ->
|
||||
assert {:use_cookies, false} in addl
|
||||
{:ok, source_details_return_fixture()}
|
||||
|
||||
_url, :get_source_metadata, _opts, _ot, _addl ->
|
||||
{:ok, "{}"}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{series_directory: nil, cookie_behaviour: :when_needed})
|
||||
perform_job(SourceMetadataStorageWorker, %{id: source.id})
|
||||
end
|
||||
|
||||
|
|
@ -337,7 +367,7 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
|
|||
{:ok, "{}"}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{series_directory: nil, use_cookies: false})
|
||||
source = source_fixture(%{series_directory: nil, cookie_behaviour: :disabled})
|
||||
perform_job(SourceMetadataStorageWorker, %{id: source.id})
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -302,14 +302,27 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
|
|||
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source)
|
||||
end
|
||||
end
|
||||
|
||||
describe "index_and_enqueue_download_for_media_items/2 when testing cookies" do
|
||||
test "sets use_cookies if the source uses cookies" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
|
||||
assert {:use_cookies, true} in addl_opts
|
||||
{:ok, source_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: true})
|
||||
source = source_fixture(%{cookie_behaviour: :all_operations})
|
||||
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source)
|
||||
end
|
||||
|
||||
test "sets use_cookies if the source uses cookies when needed" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
|
||||
assert {:use_cookies, true} in addl_opts
|
||||
{:ok, source_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{cookie_behaviour: :when_needed})
|
||||
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source)
|
||||
end
|
||||
|
|
@ -320,7 +333,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
|
|||
{:ok, source_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
source = source_fixture(%{use_cookies: false})
|
||||
source = source_fixture(%{cookie_behaviour: :disabled})
|
||||
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -60,6 +60,28 @@ defmodule Pinchflat.SourcesTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "use_cookies?/2" do
|
||||
test "returns true if the source has been set to use cookies" do
|
||||
source = source_fixture(%{cookie_behaviour: :all_operations})
|
||||
assert Sources.use_cookies?(source, :downloading)
|
||||
end
|
||||
|
||||
test "returns false if the source has not been set to use cookies" do
|
||||
source = source_fixture(%{cookie_behaviour: :disabled})
|
||||
refute Sources.use_cookies?(source, :downloading)
|
||||
end
|
||||
|
||||
test "returns true if the action is indexing and the source is set to :when_needed" do
|
||||
source = source_fixture(%{cookie_behaviour: :when_needed})
|
||||
assert Sources.use_cookies?(source, :indexing)
|
||||
end
|
||||
|
||||
test "returns false if the action is downloading and the source is set to :when_needed" do
|
||||
source = source_fixture(%{cookie_behaviour: :when_needed})
|
||||
refute Sources.use_cookies?(source, :downloading)
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_sources/0" do
|
||||
test "it returns all sources" do
|
||||
source = source_fixture()
|
||||
|
|
@ -393,13 +415,13 @@ defmodule Pinchflat.SourcesTest do
|
|||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
use_cookies: true
|
||||
cookie_behaviour: :all_operations
|
||||
}
|
||||
|
||||
assert {:ok, %Source{}} = Sources.create_source(valid_attrs)
|
||||
end
|
||||
|
||||
test "does not set use_cookies to false if the source has not been set to use cookies" do
|
||||
test "does not set use_cookies if the source uses cookies when needed" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, addl ->
|
||||
refute Keyword.get(addl, :use_cookies)
|
||||
|
||||
|
|
@ -409,7 +431,23 @@ defmodule Pinchflat.SourcesTest do
|
|||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
use_cookies: false
|
||||
cookie_behaviour: :when_needed
|
||||
}
|
||||
|
||||
assert {:ok, %Source{}} = Sources.create_source(valid_attrs)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source has not been set to use cookies" do
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, addl ->
|
||||
refute Keyword.get(addl, :use_cookies)
|
||||
|
||||
{:ok, playlist_return()}
|
||||
end)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
cookie_behaviour: :disabled
|
||||
}
|
||||
|
||||
assert {:ok, %Source{}} = Sources.create_source(valid_attrs)
|
||||
|
|
|
|||
Loading…
Reference in a new issue