Skip to content

Commit f5cb7ba

Browse files
authored
Merge pull request #2833 from matthewmcgarvey/get-channel-cleanup
Channel helpers cleanup
2 parents eba311b + e92b377 commit f5cb7ba

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

src/invidious/channels/channels.cr

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ class ChannelRedirect < Exception
114114
end
115115
end
116116

117-
def get_batch_channels(channels, refresh = false, pull_all_videos = true, max_threads = 10)
117+
def get_batch_channels(channels)
118118
finished_channel = Channel(String | Nil).new
119+
max_threads = 10
119120

120121
spawn do
121122
active_threads = 0
@@ -130,7 +131,7 @@ def get_batch_channels(channels, refresh = false, pull_all_videos = true, max_th
130131
active_threads += 1
131132
spawn do
132133
begin
133-
get_channel(ucid, refresh, pull_all_videos)
134+
get_channel(ucid)
134135
finished_channel.send(ucid)
135136
rescue ex
136137
finished_channel.send(nil)
@@ -151,23 +152,20 @@ def get_batch_channels(channels, refresh = false, pull_all_videos = true, max_th
151152
return final
152153
end
153154

154-
def get_channel(id, refresh = true, pull_all_videos = true)
155-
if channel = Invidious::Database::Channels.select(id)
156-
if refresh && Time.utc - channel.updated > 10.minutes
157-
channel = fetch_channel(id, pull_all_videos: pull_all_videos)
158-
Invidious::Database::Channels.insert(channel, update_on_conflict: true)
159-
end
160-
else
161-
channel = fetch_channel(id, pull_all_videos: pull_all_videos)
162-
Invidious::Database::Channels.insert(channel)
155+
def get_channel(id) : InvidiousChannel
156+
channel = Invidious::Database::Channels.select(id)
157+
158+
if channel.nil? || (Time.utc - channel.updated) > 2.days
159+
channel = fetch_channel(id, pull_all_videos: false)
160+
Invidious::Database::Channels.insert(channel, update_on_conflict: true)
163161
end
164162

165163
return channel
166164
end
167165

168-
def fetch_channel(ucid, pull_all_videos = true, locale = nil)
166+
def fetch_channel(ucid, pull_all_videos : Bool)
169167
LOGGER.debug("fetch_channel: #{ucid}")
170-
LOGGER.trace("fetch_channel: #{ucid} : pull_all_videos = #{pull_all_videos}, locale = #{locale}")
168+
LOGGER.trace("fetch_channel: #{ucid} : pull_all_videos = #{pull_all_videos}")
171169

172170
LOGGER.trace("fetch_channel: #{ucid} : Downloading RSS feed")
173171
rss = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{ucid}").body

src/invidious/jobs/refresh_channels_job.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Invidious::Jobs::RefreshChannelsJob < Invidious::Jobs::BaseJob
3030
spawn do
3131
begin
3232
LOGGER.trace("RefreshChannelsJob: #{id} fiber : Fetching channel")
33-
channel = fetch_channel(id, CONFIG.full_refresh)
33+
channel = fetch_channel(id, pull_all_videos: CONFIG.full_refresh)
3434

3535
lim_fibers = max_fibers
3636

src/invidious/routes/api/v1/authenticated.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module Invidious::Routes::API::V1::Authenticated
9292
ucid = env.params.url["ucid"]
9393

9494
if !user.subscriptions.includes? ucid
95-
get_channel(ucid, false, false)
95+
get_channel(ucid)
9696
Invidious::Database::Users.subscribe_channel(user, ucid)
9797
end
9898

src/invidious/routes/preferences.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ module Invidious::Routes::PreferencesRoute
327327
user.subscriptions += body["subscriptions"].as_a.map(&.as_s)
328328
user.subscriptions.uniq!
329329

330-
user.subscriptions = get_batch_channels(user.subscriptions, false, false)
330+
user.subscriptions = get_batch_channels(user.subscriptions)
331331

332332
Invidious::Database::Users.update_subscriptions(user)
333333
end
@@ -409,7 +409,7 @@ module Invidious::Routes::PreferencesRoute
409409
end
410410

411411
user.subscriptions.uniq!
412-
user.subscriptions = get_batch_channels(user.subscriptions, false, false)
412+
user.subscriptions = get_batch_channels(user.subscriptions)
413413

414414
Invidious::Database::Users.update_subscriptions(user)
415415
when "import_freetube"
@@ -418,7 +418,7 @@ module Invidious::Routes::PreferencesRoute
418418
end
419419
user.subscriptions.uniq!
420420

421-
user.subscriptions = get_batch_channels(user.subscriptions, false, false)
421+
user.subscriptions = get_batch_channels(user.subscriptions)
422422

423423
Invidious::Database::Users.update_subscriptions(user)
424424
when "import_newpipe_subscriptions"
@@ -437,7 +437,7 @@ module Invidious::Routes::PreferencesRoute
437437
end
438438
user.subscriptions.uniq!
439439

440-
user.subscriptions = get_batch_channels(user.subscriptions, false, false)
440+
user.subscriptions = get_batch_channels(user.subscriptions)
441441

442442
Invidious::Database::Users.update_subscriptions(user)
443443
when "import_newpipe"
@@ -456,7 +456,7 @@ module Invidious::Routes::PreferencesRoute
456456
user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String).map(&.lchop("https://www.youtube.com/channel/"))
457457
user.subscriptions.uniq!
458458

459-
user.subscriptions = get_batch_channels(user.subscriptions, false, false)
459+
user.subscriptions = get_batch_channels(user.subscriptions)
460460

461461
Invidious::Database::Users.update_subscriptions(user)
462462

src/invidious/routes/subscriptions.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module Invidious::Routes::Subscriptions
5151
case action
5252
when "action_create_subscription_to_channel"
5353
if !user.subscriptions.includes? channel_id
54-
get_channel(channel_id, false, false)
54+
get_channel(channel_id)
5555
Invidious::Database::Users.subscribe_channel(user, channel_id)
5656
end
5757
when "action_remove_subscriptions"

src/invidious/users.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def fetch_user(sid, headers)
7474
end
7575
end
7676

77-
channels = get_batch_channels(channels, false, false)
77+
channels = get_batch_channels(channels)
7878

7979
email = feed.xpath_node(%q(//a[@class="yt-masthead-picker-header yt-masthead-picker-active-account"]))
8080
if email

0 commit comments

Comments
 (0)