From f6fd0775d93c86d07a42998353064b90002bf5b3 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Sat, 20 Jan 2024 20:34:29 -0800 Subject: [PATCH] [WIP] Added basic video download method --- config/config.exs | 4 ++- config/dev.exs | 3 ++ config/test.exs | 3 +- .../downloader/backends/yt_dlp/video.ex | 34 +++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 lib/pinchflat/downloader/backends/yt_dlp/video.ex diff --git a/config/config.exs b/config/config.exs index fd3d9f1..92bd240 100644 --- a/config/config.exs +++ b/config/config.exs @@ -12,7 +12,9 @@ config :pinchflat, generators: [timestamp_type: :utc_datetime], # Specifying backend data here makes mocking and local testing SUPER easy yt_dlp_executable: System.find_executable("yt-dlp"), - yt_dlp_runner: Pinchflat.Downloader.Backends.YtDlp.CommandRunner + yt_dlp_runner: Pinchflat.Downloader.Backends.YtDlp.CommandRunner, + # TODO: figure this out + media_directory: :not_implemented # Configures the endpoint config :pinchflat, PinchflatWeb.Endpoint, diff --git a/config/dev.exs b/config/dev.exs index a3bc3e4..847ef3c 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,5 +1,8 @@ import Config +config :pinchflat, + media_directory: Path.join([System.tmp_dir!(), "yt-dlp"]) + # Configure your database config :pinchflat, Pinchflat.Repo, username: System.get_env("POSTGRES_USER"), diff --git a/config/test.exs b/config/test.exs index 20d03ff..1ca2159 100644 --- a/config/test.exs +++ b/config/test.exs @@ -2,7 +2,8 @@ import Config config :pinchflat, # Specifying backend data here makes mocking and local testing SUPER easy - yt_dlp_executable: Path.join([File.cwd!(), "/test/support/scripts/yt-dlp-mocks/repeater.sh"]) + yt_dlp_executable: Path.join([File.cwd!(), "/test/support/scripts/yt-dlp-mocks/repeater.sh"]), + media_directory: Path.join([System.tmp_dir!(), "yt-dlp"]) # Configure your database # diff --git a/lib/pinchflat/downloader/backends/yt_dlp/video.ex b/lib/pinchflat/downloader/backends/yt_dlp/video.ex new file mode 100644 index 0000000..fe6895d --- /dev/null +++ b/lib/pinchflat/downloader/backends/yt_dlp/video.ex @@ -0,0 +1,34 @@ +defmodule Pinchflat.Downloader.Backends.YtDlp.Video do + @moduledoc """ + Contains utilities for working with singular videos + """ + + @doc """ + Downloads a single video (and possible metadata) to the tmp directory. + Videos are downloaded in the following format: + `tmp/yt-dlp//.` + + The video will be moved to its final destination... elsewhere + # TODO: update these docs when I figure out a module to move videos + # NOTE: maybe instead of moving it to the tempdir, I can just download it + to the final destination by using the `output` option. The + parser could be updated to generate a value for the output option. + This way, advanced users can just the yt-dlp output syntax and + newer users can use the easier liquid-like syntax. + """ + def download(url, command_opts \\ []) do + default_output_path = Path.join([base_directory(), "%(id)s", "%(id)s.%(ext)s"]) + default_opts = [output: default_output_path] + opts = Keyword.merge(default_opts, command_opts) + + backend_runner().run(url, opts) + end + + defp base_directory do + Application.get_env(:pinchflat, :media_directory) + end + + defp backend_runner do + Application.get_env(:pinchflat, :yt_dlp_runner) + end +end