Fix full channel refresh

This commit is contained in:
Omar Roth 2018-09-16 20:32:39 -05:00
parent b6adeb80e6
commit f38aac851e

View File

@ -69,61 +69,33 @@ def fetch_channel(ucid, client, db, pull_all_videos = true)
updated = $4, ucid = $5, author = $6", video_array) updated = $4, ucid = $5, author = $6", video_array)
end end
else else
videos = [] of ChannelVideo
page = 1 page = 1
ids = [] of String
loop do loop do
url = produce_channel_videos_url(ucid, page) videos = extract_playlist(ucid, page)
response = client.get(url) videos = videos.map { |video| ChannelVideo.new(video.id, video.title, video.published, Time.now, video.ucid, video.author) }
count = videos.size
json = JSON.parse(response.body) videos.each do |video|
content_html = json["content_html"].as_s ids << video.id
if content_html.empty? db.exec("UPDATE users SET notifications = notifications || $1 \
# If we don't get anything, move on WHERE updated < $2 AND $3 = ANY(subscriptions) AND $1 <> ALL(notifications)", video.id, video.published, ucid)
break
end
document = XML.parse_html(content_html)
document.xpath_nodes(%q(//li[contains(@class, "feed-item-container")])).each do |item| video_array = video.to_a
anchor = item.xpath_node(%q(.//h3[contains(@class,"yt-lockup-title")]/a)) args = arg_array(video_array)
if !anchor db.exec("INSERT INTO channel_videos VALUES (#{args}) ON CONFLICT (id) DO NOTHING", video_array)
raise "could not find anchor"
end
title = anchor.content.strip
video_id = anchor["href"].lchop("/watch?v=")
published = item.xpath_node(%q(.//div[@class="yt-lockup-meta"]/ul/li[1]))
if !published
# This happens on Youtube red videos, here we just skip them
next
end
published = published.content
published = decode_date(published)
videos << ChannelVideo.new(video_id, title, published, Time.now, ucid, author)
end end
if document.xpath_nodes(%q(//li[contains(@class, "channels-content-item")])).size < 30 if count < 100
break break
end end
page += 1 page += 1
end end
video_ids = [] of String
videos.each do |video|
db.exec("UPDATE users SET notifications = notifications || $1 \
WHERE updated < $2 AND $3 = ANY(subscriptions) AND $1 <> ALL(notifications)", video.id, video.published, ucid)
video_ids << video.id
video_array = video.to_a
args = arg_array(video_array)
db.exec("INSERT INTO channel_videos VALUES (#{args}) ON CONFLICT (id) DO NOTHING", video_array)
end
# When a video is deleted from a channel, we find and remove it here # When a video is deleted from a channel, we find and remove it here
db.exec("DELETE FROM channel_videos * WHERE NOT id = ANY ('{#{video_ids.map { |a| %("#{a}") }.join(",")}}') AND ucid = $1", ucid) db.exec("DELETE FROM channel_videos * WHERE NOT id = ANY ('{#{ids.map { |id| %("#{id}") }.join(",")}}') AND ucid = $1", ucid)
end end
channel = InvidiousChannel.new(ucid, author, Time.now) channel = InvidiousChannel.new(ucid, author, Time.now)