Put cookie auth in correct spot

This commit is contained in:
Kieran Eglin 2024-03-24 19:55:02 -07:00
parent 78d33db169
commit 2c370c722c
No known key found for this signature in database
GPG key ID: 193984967FCF432D
5 changed files with 59 additions and 50 deletions

View file

@ -55,8 +55,14 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
base_dir = Application.get_env(:pinchflat, :extras_directory) base_dir = Application.get_env(:pinchflat, :extras_directory)
filepath = Path.join(base_dir, "cookies.txt") filepath = Path.join(base_dir, "cookies.txt")
if File.exists?(filepath) do
Logger.info("Cookies file exists")
else
Logger.info("Cookies does not exist - creating it")
FilesystemHelpers.write_p!(filepath, "") FilesystemHelpers.write_p!(filepath, "")
end end
end
defp apply_default_settings do defp apply_default_settings do
Settings.fetch!(:onboarding, true) Settings.fetch!(:onboarding, true)

View file

@ -18,7 +18,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do
built_options = built_options =
default_options() ++ default_options() ++
cookie_options() ++
subtitle_options(media_profile) ++ subtitle_options(media_profile) ++
thumbnail_options(media_item_with_preloads) ++ thumbnail_options(media_item_with_preloads) ++
metadata_options(media_profile) ++ metadata_options(media_profile) ++
@ -44,19 +43,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do
[:no_progress, :windows_filenames] [:no_progress, :windows_filenames]
end 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 defp subtitle_options(media_profile) do
mapped_struct = Map.from_struct(media_profile) mapped_struct = Map.from_struct(media_profile)

View file

@ -31,7 +31,8 @@ defmodule Pinchflat.YtDlp.CommandRunner do
# Also, can't use RAM file since yt-dlp needs a concrete filepath. # 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)) output_filepath = Keyword.get(addl_opts, :output_filepath, FSUtils.generate_metadata_tmpfile(:json))
print_to_file_opts = [{:print_to_file, output_template}, output_filepath] 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, " ")}") Logger.info("[yt-dlp] called with: #{Enum.join(formatted_command_opts, " ")}")
@ -47,6 +48,19 @@ defmodule Pinchflat.YtDlp.CommandRunner do
end end
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: # 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) # 1. If the key is an atom, convert it to a string and convert it to kebab case (for convenience)

View file

@ -5,7 +5,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilderTest do
import Pinchflat.ProfilesFixtures import Pinchflat.ProfilesFixtures
alias Pinchflat.Profiles alias Pinchflat.Profiles
alias Pinchflat.Filesystem.FilesystemHelpers
alias Pinchflat.Downloading.DownloadOptionBuilder alias Pinchflat.Downloading.DownloadOptionBuilder
setup do setup do
@ -42,39 +41,6 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilderTest do
end end
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 describe "build/1 when testing subtitle options" do
test "includes :write_subs option when specified", %{media_item: media_item} do test "includes :write_subs option when specified", %{media_item: media_item} do
media_item = update_media_profile_attribute(media_item, %{download_subs: true}) media_item = update_media_profile_attribute(media_item, %{download_subs: true})

View file

@ -1,6 +1,8 @@
defmodule Pinchflat.YtDlp.CommandRunnerTest do defmodule Pinchflat.YtDlp.CommandRunnerTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
alias Pinchflat.Filesystem.FilesystemHelpers
alias Pinchflat.YtDlp.CommandRunner, as: Runner alias Pinchflat.YtDlp.CommandRunner, as: Runner
@original_executable Application.compile_env(:pinchflat, :yt_dlp_executable) @original_executable Application.compile_env(:pinchflat, :yt_dlp_executable)
@ -65,6 +67,41 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
end end
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 defp wrap_executable(new_executable, fun) do
Application.put_env(:pinchflat, :yt_dlp_executable, new_executable) Application.put_env(:pinchflat, :yt_dlp_executable, new_executable)
fun.() fun.()