diff --git a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex index 61ba897..ffefa70 100644 --- a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex +++ b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex @@ -5,6 +5,8 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do Many of these methods are made to be kickoff or be consumed by workers. """ + use Pinchflat.Media.MediaQuery + require Logger alias Pinchflat.Repo @@ -14,6 +16,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do alias Pinchflat.Sources.Source alias Pinchflat.Media.MediaItem alias Pinchflat.YtDlp.MediaCollection + alias Pinchflat.Utils.FilesystemUtils alias Pinchflat.Downloading.DownloadingHelpers alias Pinchflat.SlowIndexing.FileFollowerServer alias Pinchflat.Downloading.DownloadOptionBuilder @@ -96,6 +99,22 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do result end + # TODO: test + def create_download_archive_file(%Source{} = source) do + tmpfile = FilesystemUtils.generate_metadata_tmpfile(:txt) + + archive_contents = + source + |> get_media_items_for_download_archive() + |> Enum.map(fn media_item -> "youtube #{media_item.media_id}" end) + |> Enum.join("\n") + + case File.write(tmpfile, archive_contents) do + :ok -> tmpfile + err -> err + end + end + # The file follower is a GenServer that watches a file for new lines and # processes them. This works well, but we have to be resilliant to partially-written # lines (ie: you should gracefully fail if you can't parse a line). @@ -166,4 +185,14 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do max(0, index_frequency_seconds - offset_seconds) end + + # TODO: document + defp get_media_items_for_download_archive(source) do + MediaQuery.new() + |> where(^MediaQuery.for_source(source)) + |> order_by(desc: :uploaded_at) + |> limit(20) + |> offset(20) + |> Repo.all() + end end diff --git a/lib/pinchflat/yt_dlp/media_collection.ex b/lib/pinchflat/yt_dlp/media_collection.ex index 3762db7..aa4abb3 100644 --- a/lib/pinchflat/yt_dlp/media_collection.ex +++ b/lib/pinchflat/yt_dlp/media_collection.ex @@ -23,7 +23,6 @@ defmodule Pinchflat.YtDlp.MediaCollection do Returns {:ok, [map()]} | {:error, any, ...}. """ def get_media_attributes_for_collection(url, command_opts \\ [], addl_opts \\ []) do - runner = Application.get_env(:pinchflat, :yt_dlp_runner) # `ignore_no_formats_error` is necessary because yt-dlp will error out if # the first video has not released yet (ie: is a premier). We don't care about # available formats since we're just getting the media details @@ -39,7 +38,7 @@ defmodule Pinchflat.YtDlp.MediaCollection do file_listener_handler.(output_filepath) end - case runner.run(url, action, all_command_opts, output_template, runner_opts) do + case backend_runner().run(url, action, all_command_opts, output_template, runner_opts) do {:ok, output} -> parsed_lines = output