|
| 1 | +defmodule Pinchflat.Profiles.Options.YtDlp.IndexOptionBuilder do |
| 2 | + @moduledoc """ |
| 3 | + Builds the options for yt-dlp to index a media source based on the given media profile. |
| 4 | + """ |
| 5 | + |
| 6 | + alias Pinchflat.Profiles.MediaProfile |
| 7 | + |
| 8 | + @doc """ |
| 9 | + Builds the options for yt-dlp to index a media source based on the given media profile. |
| 10 | + """ |
| 11 | + def build(%MediaProfile{} = media_profile) do |
| 12 | + built_options = release_type_options(media_profile) |
| 13 | + |
| 14 | + {:ok, built_options} |
| 15 | + end |
| 16 | + |
| 17 | + defp release_type_options(media_profile) do |
| 18 | + mapped_struct = Map.from_struct(media_profile) |
| 19 | + |
| 20 | + # Appending multiple match filters treats them as an OR condition, |
| 21 | + # so we have to be careful around combining `only` and `exclude` options. |
| 22 | + # eg: only shorts + exclude livestreams = "any video that is a short OR is not a livestream" |
| 23 | + # which will return all shorts AND normal videos. |
| 24 | + Enum.reduce(mapped_struct, [], fn attr, acc -> |
| 25 | + case {attr, media_profile} do |
| 26 | + {{:shorts_behaviour, :only}, _} -> |
| 27 | + acc ++ [match_filter: "original_url*=/shorts/"] |
| 28 | + |
| 29 | + {{:livestream_behaviour, :only}, _} -> |
| 30 | + acc ++ [match_filter: "was_live"] |
| 31 | + |
| 32 | + # Since match_filter is an OR (see above), `exclude`s must be ignored entirely if the |
| 33 | + # other type is set to `only`. There is also special behaviour if they're both excludes, |
| 34 | + # hence why these check against `:include` alone. |
| 35 | + {{:shorts_behaviour, :exclude}, %{livestream_behaviour: :include}} -> |
| 36 | + acc ++ [match_filter: "original_url!*=/shorts/"] |
| 37 | + |
| 38 | + {{:livestream_behaviour, :exclude}, %{shorts_behaviour: :include}} -> |
| 39 | + acc ++ [match_filter: "!was_live"] |
| 40 | + |
| 41 | + # Again, since it's an OR, there's a special syntax if they're both excluded |
| 42 | + # to make it an AND. Note that I'm not checking for the other permutation of |
| 43 | + # both excluding since this MUST get hit so adding the other version would double up. |
| 44 | + {{:livestream_behaviour, :exclude}, %{shorts_behaviour: :exclude}} -> |
| 45 | + acc ++ [match_filter: "!was_live & original_url!*=/shorts/"] |
| 46 | + |
| 47 | + _ -> |
| 48 | + acc |
| 49 | + end |
| 50 | + end) |
| 51 | + end |
| 52 | +end |
0 commit comments