From 3c98601f35270fd3cf06b138de17b2c5e6919521 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 8 Nov 2018 20:08:03 -0600 Subject: [PATCH 1/4] Add job for pulling popular videos --- src/invidious.cr | 8 +++++++- src/invidious/jobs.cr | 15 +++++++++++++++ src/invidious/users.cr | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/invidious.cr b/src/invidious.cr index cbba17c1..e1b337b5 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -require "crypto/bcrypt/password" require "detect_language" require "digest/md5" require "kemal" @@ -112,6 +111,13 @@ spawn do end end +popular_videos = [] of ChannelVideo +spawn do + pull_popular_videos(PG_DB) do |videos| + popular_videos = videos + end +end + decrypt_function = [] of {name: String, value: Int32} spawn do update_decrypt_function do |function| diff --git a/src/invidious/jobs.cr b/src/invidious/jobs.cr index 6f5481c9..afd0ad31 100644 --- a/src/invidious/jobs.cr +++ b/src/invidious/jobs.cr @@ -180,6 +180,21 @@ def pull_top_videos(config, db) end end +def pull_popular_videos(db) + loop do + subscriptions = PG_DB.query_all("SELECT channel FROM \ + (SELECT UNNEST(subscriptions) AS channel FROM users) AS d \ + GROUP BY channel ORDER BY COUNT(channel) DESC LIMIT 40", as: String) + + videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM \ + channel_videos WHERE ucid IN (#{arg_array(subscriptions)}) \ + ORDER BY ucid, published DESC", subscriptions, as: ChannelVideo).sort_by { |video| video.published }.reverse + + yield videos + Fiber.yield + end +end + def update_decrypt_function loop do begin diff --git a/src/invidious/users.cr b/src/invidious/users.cr index b7cb3fbd..28879d23 100644 --- a/src/invidious/users.cr +++ b/src/invidious/users.cr @@ -1,3 +1,5 @@ +require "crypto/bcrypt/password" + class User module PreferencesConverter def self.from_rs(rs) From 18bb397c7d10bb782f2a3c7f99390974e6479aa3 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 25 Nov 2018 18:13:11 -0600 Subject: [PATCH 2/4] Add '/api/v1/popular' --- src/invidious.cr | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/invidious.cr b/src/invidious.cr index e1b337b5..cd1da476 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -2606,6 +2606,40 @@ get "/api/v1/trending" do |env| videos end +get "/api/v1/popular" do |env| + videos = JSON.build do |json| + json.array do + popular_videos.each do |video| + json.object do + json.field "title", video.title + json.field "videoId", video.id + json.field "videoThumbnails" do + generate_thumbnails(json, video.id) + end + + json.field "lengthSeconds", video.info["length_seconds"].to_i + json.field "viewCount", video.views + + json.field "author", video.author + json.field "authorId", video.ucid + json.field "authorUrl", "/channel/#{video.ucid}" + json.field "published", video.published.to_unix + json.field "publishedText", "#{recode_date(video.published)} ago" + + description = video.description.gsub("
", "\n") + description = description.gsub("
", "\n") + description = XML.parse_html(description) + json.field "description", description.content + json.field "descriptionHtml", video.description + end + end + end + end + + env.response.content_type = "application/json" + videos +end + get "/api/v1/top" do |env| videos = JSON.build do |json| json.array do From 32e4ad078435baa4f8a3c552ca3ad516f247e831 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 25 Nov 2018 18:13:56 -0600 Subject: [PATCH 3/4] Update default config --- config/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.yml b/config/config.yml index 54a25417..62e80704 100644 --- a/config/config.yml +++ b/config/config.yml @@ -1,7 +1,7 @@ -crawl_threads: 1 +video_threads: 0 +crawl_threads: 0 channel_threads: 1 feed_threads: 1 -video_threads: 1 db: user: kemal password: kemal From 9ce02e579d8f70862788f7af57007647bd4970c1 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 25 Nov 2018 18:16:56 -0600 Subject: [PATCH 4/4] Update '/api/v1/popular' --- src/invidious.cr | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index cd1da476..5fa57ea4 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -2617,20 +2617,13 @@ get "/api/v1/popular" do |env| generate_thumbnails(json, video.id) end - json.field "lengthSeconds", video.info["length_seconds"].to_i - json.field "viewCount", video.views + json.field "lengthSeconds", video.length_seconds json.field "author", video.author json.field "authorId", video.ucid json.field "authorUrl", "/channel/#{video.ucid}" json.field "published", video.published.to_unix json.field "publishedText", "#{recode_date(video.published)} ago" - - description = video.description.gsub("
", "\n") - description = description.gsub("
", "\n") - description = XML.parse_html(description) - json.field "description", description.content - json.field "descriptionHtml", video.description end end end