From e92b3779ad6ac530b4979dfeccb66e96d75d14c9 Mon Sep 17 00:00:00 2001 From: matthewmcgarvey Date: Mon, 24 Jan 2022 23:14:13 -0600 Subject: [PATCH] Add back in refreshing of channels every 2 days --- src/invidious/channels/channels.cr | 10 ++++++---- src/invidious/database/channels.cr | 9 ++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/invidious/channels/channels.cr b/src/invidious/channels/channels.cr index b9f762e7a..6905b6f8f 100644 --- a/src/invidious/channels/channels.cr +++ b/src/invidious/channels/channels.cr @@ -152,12 +152,14 @@ def get_batch_channels(channels) return final end -def get_channel(id) +def get_channel(id) : InvidiousChannel channel = Invidious::Database::Channels.select(id) - return channel if channel - channel = fetch_channel(id, pull_all_videos: false) - Invidious::Database::Channels.insert(channel) + if channel.nil? || (Time.utc - channel.updated) > 2.days + channel = fetch_channel(id, pull_all_videos: false) + Invidious::Database::Channels.insert(channel, update_on_conflict: true) + end + return channel end diff --git a/src/invidious/database/channels.cr b/src/invidious/database/channels.cr index e88b4989b..134cf59d2 100644 --- a/src/invidious/database/channels.cr +++ b/src/invidious/database/channels.cr @@ -10,7 +10,7 @@ module Invidious::Database::Channels # Insert / delete # ------------------- - def insert(channel : InvidiousChannel) + def insert(channel : InvidiousChannel, update_on_conflict : Bool = false) channel_array = channel.to_a request = <<-SQL @@ -18,6 +18,13 @@ module Invidious::Database::Channels VALUES (#{arg_array(channel_array)}) SQL + if update_on_conflict + request += <<-SQL + ON CONFLICT (id) DO UPDATE + SET author = $2, updated = $3 + SQL + end + PG_DB.exec(request, args: channel_array) end