Put cookie auth in correct spot
This commit is contained in:
parent
78d33db169
commit
2c370c722c
5 changed files with 59 additions and 50 deletions
|
|
@ -55,7 +55,13 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
|
|||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||
filepath = Path.join(base_dir, "cookies.txt")
|
||||
|
||||
FilesystemHelpers.write_p!(filepath, "")
|
||||
if File.exists?(filepath) do
|
||||
Logger.info("Cookies file exists")
|
||||
else
|
||||
Logger.info("Cookies does not exist - creating it")
|
||||
|
||||
FilesystemHelpers.write_p!(filepath, "")
|
||||
end
|
||||
end
|
||||
|
||||
defp apply_default_settings do
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do
|
|||
|
||||
built_options =
|
||||
default_options() ++
|
||||
cookie_options() ++
|
||||
subtitle_options(media_profile) ++
|
||||
thumbnail_options(media_item_with_preloads) ++
|
||||
metadata_options(media_profile) ++
|
||||
|
|
@ -44,19 +43,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do
|
|||
[:no_progress, :windows_filenames]
|
||||
end
|
||||
|
||||
defp cookie_options do
|
||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||
cookie_file = Path.join(base_dir, "cookies.txt")
|
||||
|
||||
case File.read(cookie_file) do
|
||||
{:ok, cookie_data} ->
|
||||
if String.trim(cookie_data) != "", do: [cookies: cookie_file], else: []
|
||||
|
||||
{:error, _} ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp subtitle_options(media_profile) do
|
||||
mapped_struct = Map.from_struct(media_profile)
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,8 @@ defmodule Pinchflat.YtDlp.CommandRunner do
|
|||
# Also, can't use RAM file since yt-dlp needs a concrete filepath.
|
||||
output_filepath = Keyword.get(addl_opts, :output_filepath, FSUtils.generate_metadata_tmpfile(:json))
|
||||
print_to_file_opts = [{:print_to_file, output_template}, output_filepath]
|
||||
formatted_command_opts = [url] ++ parse_options(command_opts ++ print_to_file_opts)
|
||||
cookie_opts = build_cookie_options()
|
||||
formatted_command_opts = [url] ++ parse_options(command_opts ++ print_to_file_opts ++ cookie_opts)
|
||||
|
||||
Logger.info("[yt-dlp] called with: #{Enum.join(formatted_command_opts, " ")}")
|
||||
|
||||
|
|
@ -47,6 +48,19 @@ defmodule Pinchflat.YtDlp.CommandRunner do
|
|||
end
|
||||
end
|
||||
|
||||
defp build_cookie_options do
|
||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||
cookie_file = Path.join(base_dir, "cookies.txt")
|
||||
|
||||
case File.read(cookie_file) do
|
||||
{:ok, cookie_data} ->
|
||||
if String.trim(cookie_data) != "", do: [cookies: cookie_file], else: []
|
||||
|
||||
{:error, _} ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
# We want to satisfy the following behaviours:
|
||||
#
|
||||
# 1. If the key is an atom, convert it to a string and convert it to kebab case (for convenience)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilderTest do
|
|||
import Pinchflat.ProfilesFixtures
|
||||
|
||||
alias Pinchflat.Profiles
|
||||
alias Pinchflat.Filesystem.FilesystemHelpers
|
||||
alias Pinchflat.Downloading.DownloadOptionBuilder
|
||||
|
||||
setup do
|
||||
|
|
@ -42,39 +41,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilderTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "build/1 when testing cookie options" do
|
||||
setup do
|
||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||
fpath = Path.join(base_dir, "cookies.txt")
|
||||
|
||||
{:ok, fpath: fpath}
|
||||
end
|
||||
|
||||
test "includes cookie options when cookies.txt exists", %{media_item: media_item, fpath: fpath} do
|
||||
FilesystemHelpers.write_p!(fpath, "cookie data")
|
||||
|
||||
assert {:ok, res} = DownloadOptionBuilder.build(media_item)
|
||||
|
||||
assert {:cookies, fpath} in res
|
||||
end
|
||||
|
||||
test "doesn't include cookie options when cookies.txt blank", %{media_item: media_item, fpath: fpath} do
|
||||
FilesystemHelpers.write_p!(fpath, " \n \n ")
|
||||
|
||||
assert {:ok, res} = DownloadOptionBuilder.build(media_item)
|
||||
|
||||
refute {:cookies, fpath} in res
|
||||
end
|
||||
|
||||
test "doesn't include cookie options when cookies.txt doesn't exist", %{media_item: media_item, fpath: fpath} do
|
||||
File.rm(fpath)
|
||||
|
||||
assert {:ok, res} = DownloadOptionBuilder.build(media_item)
|
||||
|
||||
refute {:cookies, fpath} in res
|
||||
end
|
||||
end
|
||||
|
||||
describe "build/1 when testing subtitle options" do
|
||||
test "includes :write_subs option when specified", %{media_item: media_item} do
|
||||
media_item = update_media_profile_attribute(media_item, %{download_subs: true})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
defmodule Pinchflat.YtDlp.CommandRunnerTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias Pinchflat.Filesystem.FilesystemHelpers
|
||||
|
||||
alias Pinchflat.YtDlp.CommandRunner, as: Runner
|
||||
|
||||
@original_executable Application.compile_env(:pinchflat, :yt_dlp_executable)
|
||||
|
|
@ -65,6 +67,41 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "run/4 when testing cookie options" do
|
||||
setup do
|
||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||
cookie_file = Path.join(base_dir, "cookies.txt")
|
||||
|
||||
{:ok, cookie_file: cookie_file}
|
||||
end
|
||||
|
||||
test "includes cookie options when cookies.txt exists", %{cookie_file: cookie_file} do
|
||||
FilesystemHelpers.write_p!(cookie_file, "cookie data")
|
||||
|
||||
assert {:ok, output} = Runner.run(@media_url, [], "")
|
||||
|
||||
assert String.contains?(output, "--cookies #{cookie_file}")
|
||||
end
|
||||
|
||||
test "doesn't include cookie options when cookies.txt blank", %{cookie_file: cookie_file} do
|
||||
FilesystemHelpers.write_p!(cookie_file, " \n \n ")
|
||||
|
||||
assert {:ok, output} = Runner.run(@media_url, [], "")
|
||||
|
||||
refute String.contains?(output, "--cookies")
|
||||
refute String.contains?(output, cookie_file)
|
||||
end
|
||||
|
||||
test "doesn't include cookie options when cookies.txt doesn't exist", %{cookie_file: cookie_file} do
|
||||
File.rm(cookie_file)
|
||||
|
||||
assert {:ok, output} = Runner.run(@media_url, [], "")
|
||||
|
||||
refute String.contains?(output, "--cookies")
|
||||
refute String.contains?(output, cookie_file)
|
||||
end
|
||||
end
|
||||
|
||||
defp wrap_executable(new_executable, fun) do
|
||||
Application.put_env(:pinchflat, :yt_dlp_executable, new_executable)
|
||||
fun.()
|
||||
|
|
|
|||
Loading…
Reference in a new issue