Added option for yt-dlp config file usage
This commit is contained in:
parent
b4438c1ba5
commit
b7565a0ec2
4 changed files with 78 additions and 22 deletions
|
|
@ -32,7 +32,7 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
|
||||||
@impl true
|
@impl true
|
||||||
def init(state) do
|
def init(state) do
|
||||||
reset_executing_jobs()
|
reset_executing_jobs()
|
||||||
create_blank_cookie_file()
|
create_blank_yt_dlp_files()
|
||||||
apply_default_settings()
|
apply_default_settings()
|
||||||
|
|
||||||
{:ok, state}
|
{:ok, state}
|
||||||
|
|
@ -50,15 +50,19 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
|
||||||
Logger.info("Reset #{count} executing jobs")
|
Logger.info("Reset #{count} executing jobs")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp create_blank_cookie_file do
|
defp create_blank_yt_dlp_files do
|
||||||
|
files = ["cookies.txt", "yt-dlp-config.txt"]
|
||||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||||
filepath = Path.join(base_dir, "cookies.txt")
|
|
||||||
|
|
||||||
if !File.exists?(filepath) do
|
Enum.each(files, fn file ->
|
||||||
Logger.info("yt-dlp cookie file does not exist - creating it")
|
filepath = Path.join(base_dir, file)
|
||||||
|
|
||||||
FilesystemUtils.write_p!(filepath, "")
|
if !File.exists?(filepath) do
|
||||||
end
|
Logger.info("Creating blank file: #{filepath}")
|
||||||
|
|
||||||
|
FilesystemUtils.write_p!(filepath, "")
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_default_settings do
|
defp apply_default_settings do
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,14 @@ defmodule Pinchflat.YtDlp.CommandRunner do
|
||||||
def run(url, command_opts, output_template, addl_opts \\ []) do
|
def run(url, command_opts, output_template, addl_opts \\ []) do
|
||||||
# This approach lets us mock the command for testing
|
# This approach lets us mock the command for testing
|
||||||
command = backend_executable()
|
command = backend_executable()
|
||||||
# These must stay in exactly this order, hence why I'm giving it its own variable.
|
|
||||||
# Also, can't use RAM file since yt-dlp needs a concrete filepath.
|
|
||||||
output_filepath = generate_output_filepath(addl_opts)
|
output_filepath = generate_output_filepath(addl_opts)
|
||||||
print_to_file_opts = [{:print_to_file, output_template}, output_filepath]
|
print_to_file_opts = [{:print_to_file, output_template}, output_filepath]
|
||||||
cookie_opts = build_cookie_options()
|
external_file_opts = build_external_file_options()
|
||||||
formatted_command_opts = [url] ++ CliUtils.parse_options(command_opts ++ print_to_file_opts ++ cookie_opts)
|
# These must stay in exactly this order, hence why I'm giving it its own variable.
|
||||||
|
all_opts = command_opts ++ print_to_file_opts ++ external_file_opts
|
||||||
|
|
||||||
|
formatted_command_opts = [url] ++ CliUtils.parse_options(all_opts)
|
||||||
Logger.info("[yt-dlp] called with: #{Enum.join(formatted_command_opts, " ")}")
|
Logger.info("[yt-dlp] called with: #{Enum.join(formatted_command_opts, " ")}")
|
||||||
|
|
||||||
case System.cmd(command, formatted_command_opts, stderr_to_stdout: true) do
|
case System.cmd(command, formatted_command_opts, stderr_to_stdout: true) do
|
||||||
|
|
@ -73,17 +74,29 @@ defmodule Pinchflat.YtDlp.CommandRunner do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp build_cookie_options do
|
defp build_external_file_options do
|
||||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||||
cookie_file = Path.join(base_dir, "cookies.txt")
|
|
||||||
|
|
||||||
case File.read(cookie_file) do
|
filename_options_map = %{
|
||||||
{:ok, cookie_data} ->
|
cookies: "cookies.txt",
|
||||||
if String.trim(cookie_data) != "", do: [cookies: cookie_file], else: []
|
config_locations: "yt-dlp-config.txt"
|
||||||
|
}
|
||||||
|
|
||||||
{:error, _} ->
|
Enum.reduce(filename_options_map, [], fn {opt_name, filename}, acc ->
|
||||||
[]
|
filepath = Path.join(base_dir, filename)
|
||||||
end
|
|
||||||
|
case File.read(filepath) do
|
||||||
|
{:ok, file_data} ->
|
||||||
|
if String.trim(file_data) != "" do
|
||||||
|
[{opt_name, filepath} | acc]
|
||||||
|
else
|
||||||
|
acc
|
||||||
|
end
|
||||||
|
|
||||||
|
{:error, _} ->
|
||||||
|
acc
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp backend_executable do
|
defp backend_executable do
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ defmodule Pinchflat.Boot.PreJobStartupTasksTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "create_blank_cookie_file" do
|
describe "create_blank_yt_dlp_files" do
|
||||||
test "creates a blank cookie file" do
|
test "creates a blank cookie file" 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")
|
||||||
|
|
@ -39,6 +39,18 @@ defmodule Pinchflat.Boot.PreJobStartupTasksTest do
|
||||||
|
|
||||||
assert File.exists?(filepath)
|
assert File.exists?(filepath)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "creates a blank yt-dlp config file" do
|
||||||
|
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||||
|
filepath = Path.join(base_dir, "yt-dlp-config.txt")
|
||||||
|
File.rm(filepath)
|
||||||
|
|
||||||
|
refute File.exists?(filepath)
|
||||||
|
|
||||||
|
PreJobStartupTasks.init(%{})
|
||||||
|
|
||||||
|
assert File.exists?(filepath)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "apply_default_settings" do
|
describe "apply_default_settings" do
|
||||||
|
|
|
||||||
|
|
@ -42,12 +42,13 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "run/4 when testing cookie options" do
|
describe "run/4 when testing external file options" do
|
||||||
setup do
|
setup do
|
||||||
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
base_dir = Application.get_env(:pinchflat, :extras_directory)
|
||||||
cookie_file = Path.join(base_dir, "cookies.txt")
|
cookie_file = Path.join(base_dir, "cookies.txt")
|
||||||
|
yt_dlp_file = Path.join(base_dir, "yt-dlp-config.txt")
|
||||||
|
|
||||||
{:ok, cookie_file: cookie_file}
|
{:ok, cookie_file: cookie_file, yt_dlp_file: yt_dlp_file}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "includes cookie options when cookies.txt exists", %{cookie_file: cookie_file} do
|
test "includes cookie options when cookies.txt exists", %{cookie_file: cookie_file} do
|
||||||
|
|
@ -75,6 +76,32 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
|
||||||
refute String.contains?(output, "--cookies")
|
refute String.contains?(output, "--cookies")
|
||||||
refute String.contains?(output, cookie_file)
|
refute String.contains?(output, cookie_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "includes yt-dlp options when config file exists", %{yt_dlp_file: yt_dlp_file} do
|
||||||
|
FilesystemUtils.write_p!(yt_dlp_file, "config data")
|
||||||
|
|
||||||
|
assert {:ok, output} = Runner.run(@media_url, [], "")
|
||||||
|
|
||||||
|
assert String.contains?(output, "--config-locations #{yt_dlp_file}")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "doesn't include yt-dlp options when config file blank", %{yt_dlp_file: yt_dlp_file} do
|
||||||
|
FilesystemUtils.write_p!(yt_dlp_file, " \n \n ")
|
||||||
|
|
||||||
|
assert {:ok, output} = Runner.run(@media_url, [], "")
|
||||||
|
|
||||||
|
refute String.contains?(output, "--config-locations")
|
||||||
|
refute String.contains?(output, yt_dlp_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ddoesn't include yt-dlp options when config file doesn't exist", %{yt_dlp_file: yt_dlp_file} do
|
||||||
|
File.rm(yt_dlp_file)
|
||||||
|
|
||||||
|
assert {:ok, output} = Runner.run(@media_url, [], "")
|
||||||
|
|
||||||
|
refute String.contains?(output, "--config-locations")
|
||||||
|
refute String.contains?(output, yt_dlp_file)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "version/0" do
|
describe "version/0" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue