Skip to content

Commit a82d21f

Browse files
Cleanup channel helpers code
1 parent 508f137 commit a82d21f

File tree

7 files changed

+20
-31
lines changed

7 files changed

+20
-31
lines changed

src/invidious/channels/channels.cr

Lines changed: 10 additions & 14 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,18 @@ 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)
163-
end
155+
def get_channel(id)
156+
channel = Invidious::Database::Channels.select(id)
157+
return channel if channel
164158

159+
channel = fetch_channel(id, pull_all_videos: false)
160+
Invidious::Database::Channels.insert(channel)
165161
return channel
166162
end
167163

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

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

src/invidious/database/channels.cr

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@ module Invidious::Database::Channels
1010
# Insert / delete
1111
# -------------------
1212

13-
def insert(channel : InvidiousChannel, update_on_conflict : Bool = false)
13+
def insert(channel : InvidiousChannel)
1414
channel_array = channel.to_a
1515

1616
request = <<-SQL
1717
INSERT INTO channels
1818
VALUES (#{arg_array(channel_array)})
1919
SQL
2020

21-
if update_on_conflict
22-
request += <<-SQL
23-
ON CONFLICT (id) DO UPDATE
24-
SET author = $2, updated = $3
25-
SQL
26-
end
27-
2821
PG_DB.exec(request, args: channel_array)
2922
end
3023

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)