mirror of
https://github.com/iv-org/invidious.git
synced 2026-09-26 11:35:27 -05:00
fix: fetch channel videos through Innertube
This commit is contained in:
parent
c88230067b
commit
b5fcc9bdbc
@ -19,6 +19,11 @@ record AboutChannel,
|
|||||||
verified : Bool,
|
verified : Bool,
|
||||||
is_age_gated : Bool
|
is_age_gated : Bool
|
||||||
|
|
||||||
|
private DELETED_CHANNEL_ERROR_MESSAGES = {
|
||||||
|
"This channel does not exist.",
|
||||||
|
"This account has been terminated for a violation of YouTube's Terms of Service.",
|
||||||
|
}
|
||||||
|
|
||||||
def get_about_info(ucid) : AboutChannel
|
def get_about_info(ucid) : AboutChannel
|
||||||
begin
|
begin
|
||||||
# Fetch channel information from channel home page
|
# Fetch channel information from channel home page
|
||||||
@ -29,7 +34,7 @@ def get_about_info(ucid) : AboutChannel
|
|||||||
|
|
||||||
if initdata.dig?("alerts", 0, "alertRenderer", "type") == "ERROR"
|
if initdata.dig?("alerts", 0, "alertRenderer", "type") == "ERROR"
|
||||||
error_message = initdata["alerts"][0]["alertRenderer"]["text"]["simpleText"].as_s
|
error_message = initdata["alerts"][0]["alertRenderer"]["text"]["simpleText"].as_s
|
||||||
if error_message == "This channel does not exist."
|
if DELETED_CHANNEL_ERROR_MESSAGES.includes?(error_message)
|
||||||
raise NotFoundException.new(error_message)
|
raise NotFoundException.new(error_message)
|
||||||
else
|
else
|
||||||
raise InfoException.new(error_message)
|
raise InfoException.new(error_message)
|
||||||
|
|||||||
@ -159,36 +159,13 @@ def fetch_channel(ucid, pull_all_videos : Bool)
|
|||||||
LOGGER.debug("fetch_channel: #{ucid}")
|
LOGGER.debug("fetch_channel: #{ucid}")
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : pull_all_videos = #{pull_all_videos}")
|
LOGGER.trace("fetch_channel: #{ucid} : pull_all_videos = #{pull_all_videos}")
|
||||||
|
|
||||||
namespaces = {
|
channel_info = get_about_info(ucid)
|
||||||
"yt" => "http://www.youtube.com/xml/schemas/2015",
|
|
||||||
"media" => "http://search.yahoo.com/mrss/",
|
|
||||||
"default" => "http://www.w3.org/2005/Atom",
|
|
||||||
}
|
|
||||||
|
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : Downloading RSS feed")
|
LOGGER.trace("fetch_channel: #{ucid} : author = #{channel_info.author}, auto_generated = #{channel_info.auto_generated}")
|
||||||
rss = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{ucid}").body
|
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : Parsing RSS feed")
|
|
||||||
rss = XML.parse(rss)
|
|
||||||
|
|
||||||
author = rss.xpath_node("//default:feed/default:title", namespaces)
|
|
||||||
if !author
|
|
||||||
raise InfoException.new("Deleted or invalid channel")
|
|
||||||
end
|
|
||||||
|
|
||||||
author = author.content
|
|
||||||
|
|
||||||
# Auto-generated channels
|
|
||||||
# https://support.google.com/youtube/answer/2579942
|
|
||||||
if author.ends_with?(" - Topic") ||
|
|
||||||
{"Popular on YouTube", "Music", "Sports", "Gaming"}.includes? author
|
|
||||||
auto_generated = true
|
|
||||||
end
|
|
||||||
|
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : author = #{author}, auto_generated = #{auto_generated}")
|
|
||||||
|
|
||||||
channel = InvidiousChannel.new({
|
channel = InvidiousChannel.new({
|
||||||
id: ucid,
|
id: ucid,
|
||||||
author: author,
|
author: channel_info.author,
|
||||||
updated: Time.utc,
|
updated: Time.utc,
|
||||||
deleted: false,
|
deleted: false,
|
||||||
subscribed: nil,
|
subscribed: nil,
|
||||||
@ -197,61 +174,32 @@ def fetch_channel(ucid, pull_all_videos : Bool)
|
|||||||
LOGGER.trace("fetch_channel: #{ucid} : Downloading channel videos page")
|
LOGGER.trace("fetch_channel: #{ucid} : Downloading channel videos page")
|
||||||
videos, continuation = IV::Channel::Tabs.get_videos(channel)
|
videos, continuation = IV::Channel::Tabs.get_videos(channel)
|
||||||
|
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : Extracting videos from channel RSS feed")
|
LOGGER.trace("fetch_channel: #{ucid} : Extracting videos from channel")
|
||||||
rss.xpath_nodes("//default:feed/default:entry", namespaces).each do |entry|
|
videos.select(SearchVideo).each do |video|
|
||||||
video_id = entry.xpath_node("yt:videoId", namespaces).not_nil!.content
|
channel_video = ChannelVideo.new({
|
||||||
title = entry.xpath_node("default:title", namespaces).not_nil!.content
|
id: video.id,
|
||||||
|
title: video.title,
|
||||||
published = Time.parse_rfc3339(
|
published: video.published,
|
||||||
entry.xpath_node("default:published", namespaces).not_nil!.content
|
updated: Time.utc,
|
||||||
)
|
ucid: video.ucid,
|
||||||
updated = Time.parse_rfc3339(
|
author: video.author,
|
||||||
entry.xpath_node("default:updated", namespaces).not_nil!.content
|
length_seconds: video.length_seconds,
|
||||||
)
|
live_now: video.badges.live_now?,
|
||||||
|
premiere_timestamp: video.premiere_timestamp,
|
||||||
author = entry.xpath_node("default:author/default:name", namespaces).not_nil!.content
|
views: video.views,
|
||||||
ucid = entry.xpath_node("yt:channelId", namespaces).not_nil!.content
|
|
||||||
|
|
||||||
views = entry
|
|
||||||
.xpath_node("media:group/media:community/media:statistics", namespaces)
|
|
||||||
.try &.["views"]?.try &.to_i64? || 0_i64
|
|
||||||
|
|
||||||
channel_video = videos
|
|
||||||
.select(SearchVideo)
|
|
||||||
.select(&.id.== video_id)[0]?
|
|
||||||
|
|
||||||
length_seconds = channel_video.try &.length_seconds
|
|
||||||
length_seconds ||= 0
|
|
||||||
|
|
||||||
live_now = channel_video.try &.badges.live_now?
|
|
||||||
live_now ||= false
|
|
||||||
|
|
||||||
premiere_timestamp = channel_video.try &.premiere_timestamp
|
|
||||||
|
|
||||||
video = ChannelVideo.new({
|
|
||||||
id: video_id,
|
|
||||||
title: title,
|
|
||||||
published: published,
|
|
||||||
updated: updated,
|
|
||||||
ucid: ucid,
|
|
||||||
author: author,
|
|
||||||
length_seconds: length_seconds,
|
|
||||||
live_now: live_now,
|
|
||||||
premiere_timestamp: premiere_timestamp,
|
|
||||||
views: views,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Updating or inserting video")
|
LOGGER.trace("fetch_channel: #{ucid} : video #{video.id} : Updating or inserting video")
|
||||||
|
|
||||||
# We don't include the 'premiere_timestamp' here because channel pages don't include them,
|
# We don't include the 'premiere_timestamp' here because channel pages don't include them,
|
||||||
# meaning the above timestamp is always null
|
# meaning the above timestamp is always null
|
||||||
was_insert = Invidious::Database::ChannelVideos.insert(video)
|
was_insert = Invidious::Database::ChannelVideos.insert(channel_video, preserve_timestamps_on_conflict: true)
|
||||||
|
|
||||||
if was_insert
|
if was_insert
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Inserted, updating subscriptions")
|
LOGGER.trace("fetch_channel: #{ucid} : video #{video.id} : Inserted, updating subscriptions")
|
||||||
NOTIFICATION_CHANNEL.send(VideoNotification.from_video(video))
|
NOTIFICATION_CHANNEL.send(VideoNotification.from_video(channel_video))
|
||||||
else
|
else
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Updated")
|
LOGGER.trace("fetch_channel: #{ucid} : video #{video.id} : Updated")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -279,7 +227,7 @@ def fetch_channel(ucid, pull_all_videos : Bool)
|
|||||||
# We are notified of Red videos elsewhere (PubSub), which includes a correct published date,
|
# We are notified of Red videos elsewhere (PubSub), which includes a correct published date,
|
||||||
# so since they don't provide a published date here we can safely ignore them.
|
# so since they don't provide a published date here we can safely ignore them.
|
||||||
if Time.utc - video.published > 1.minute
|
if Time.utc - video.published > 1.minute
|
||||||
was_insert = Invidious::Database::ChannelVideos.insert(video)
|
was_insert = Invidious::Database::ChannelVideos.insert(video, preserve_timestamps_on_conflict: true)
|
||||||
if was_insert
|
if was_insert
|
||||||
NOTIFICATION_CHANNEL.send(VideoNotification.from_video(video))
|
NOTIFICATION_CHANNEL.send(VideoNotification.from_video(video))
|
||||||
end
|
end
|
||||||
|
|||||||
@ -98,18 +98,30 @@ module Invidious::Database::ChannelVideos
|
|||||||
# -------------------
|
# -------------------
|
||||||
|
|
||||||
# This function returns the status of the query (i.e: success?)
|
# This function returns the status of the query (i.e: success?)
|
||||||
def insert(video : ChannelVideo, with_premiere_timestamp : Bool = false) : Bool
|
def insert(
|
||||||
|
video : ChannelVideo,
|
||||||
|
with_premiere_timestamp : Bool = false,
|
||||||
|
preserve_timestamps_on_conflict : Bool = false,
|
||||||
|
) : Bool
|
||||||
if with_premiere_timestamp
|
if with_premiere_timestamp
|
||||||
last_items = "premiere_timestamp = $9, views = $10"
|
last_items = "premiere_timestamp = $9, views = $10"
|
||||||
else
|
else
|
||||||
last_items = "views = $10"
|
last_items = "views = $10"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if preserve_timestamps_on_conflict
|
||||||
|
published_on_conflict = "published = COALESCE(channel_videos.published, EXCLUDED.published)"
|
||||||
|
updated_on_conflict = "updated = CASE WHEN channel_videos.title IS DISTINCT FROM EXCLUDED.title OR channel_videos.ucid IS DISTINCT FROM EXCLUDED.ucid OR channel_videos.author IS DISTINCT FROM EXCLUDED.author OR channel_videos.length_seconds IS DISTINCT FROM EXCLUDED.length_seconds OR channel_videos.live_now IS DISTINCT FROM EXCLUDED.live_now THEN EXCLUDED.updated ELSE channel_videos.updated END"
|
||||||
|
else
|
||||||
|
published_on_conflict = "published = $3"
|
||||||
|
updated_on_conflict = "updated = $4"
|
||||||
|
end
|
||||||
|
|
||||||
request = <<-SQL
|
request = <<-SQL
|
||||||
INSERT INTO channel_videos
|
INSERT INTO channel_videos
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
ON CONFLICT (id) DO UPDATE
|
ON CONFLICT (id) DO UPDATE
|
||||||
SET title = $2, published = $3, updated = $4, ucid = $5,
|
SET title = $2, #{published_on_conflict}, #{updated_on_conflict}, ucid = $5,
|
||||||
author = $6, length_seconds = $7, live_now = $8, #{last_items}
|
author = $6, length_seconds = $7, live_now = $8, #{last_items}
|
||||||
RETURNING (xmax=0) AS was_insert
|
RETURNING (xmax=0) AS was_insert
|
||||||
SQL
|
SQL
|
||||||
|
|||||||
@ -43,7 +43,7 @@ class Invidious::Jobs::RefreshChannelsJob < Invidious::Jobs::BaseJob
|
|||||||
end
|
end
|
||||||
rescue ex
|
rescue ex
|
||||||
LOGGER.error("RefreshChannelsJob: #{id} : #{ex.message}")
|
LOGGER.error("RefreshChannelsJob: #{id} : #{ex.message}")
|
||||||
if ex.message == "Deleted or invalid channel"
|
if ex.is_a?(NotFoundException)
|
||||||
Invidious::Database::Channels.update_mark_deleted(id)
|
Invidious::Database::Channels.update_mark_deleted(id)
|
||||||
else
|
else
|
||||||
lim_fibers = 1
|
lim_fibers = 1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user