pinchflat/lib/pinchflat/fast_indexing/youtube_rss.ex
Daniel Da Cunha d51ba8bbfd Fix CI failures for YouTube API key feature
Add missing test_api_key/1 callback to YoutubeRss module (RSS doesn't
use API keys, so it returns :ok) and fix formatting in YoutubeApi.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-18 22:07:01 +08:00

73 lines
2 KiB
Elixir

defmodule Pinchflat.FastIndexing.YoutubeRss do
@moduledoc """
Methods for interacting with YouTube RSS feeds for fast indexing
"""
require Logger
alias Pinchflat.Sources.Source
alias Pinchflat.FastIndexing.YoutubeBehaviour
@behaviour YoutubeBehaviour
@doc """
Determines if the YouTube RSS feed is enabled for fast indexing. Used to satisfy
the `YoutubeBehaviour` behaviour.
Returns true
"""
@impl YoutubeBehaviour
def enabled?(), do: true
@doc """
RSS feeds don't use API keys, so this always returns :ok.
Used to satisfy the `YoutubeBehaviour` behaviour.
Returns :ok
"""
@impl YoutubeBehaviour
def test_api_key(_api_key), do: :ok
@doc """
Fetches the recent media IDs from a YouTube RSS feed for a given source.
Returns {:ok, [binary()]} | {:error, binary()}
"""
@impl YoutubeBehaviour
def get_recent_media_ids(%Source{} = source) do
Logger.debug("Fetching recent media IDs from YouTube RSS feed for source: #{source.collection_id}")
case http_client().get(rss_url_for_source(source)) do
{:ok, response} ->
response = to_string(response)
media_id_regex = ~r/<yt:videoId>(.*?)<\/yt:videoId>/
# Don't get on me about using regex to search XML.
# The content is known, well-formed, and simple.
media_ids =
media_id_regex
|> Regex.scan(response)
|> Enum.map(fn [_, id] -> String.trim(id) end)
|> Enum.filter(&(String.length(&1) > 0))
|> Enum.uniq()
Logger.debug("Media ids fetched from RSS: #{inspect(media_ids)}")
{:ok, media_ids}
{:error, _reason} ->
{:error, "Failed to fetch RSS feed"}
end
end
defp rss_url_for_source(source) do
case source.collection_type do
:channel -> "https://www.youtube.com/feeds/videos.xml?channel_id=#{source.collection_id}"
:playlist -> "https://www.youtube.com/feeds/videos.xml?playlist_id=#{source.collection_id}"
end
end
defp http_client do
Application.get_env(:pinchflat, :http_client, Pinchflat.HTTP.HTTPClient)
end
end