From 85dd3533bb4f9bc8e007d3b5de158f56db1445ce Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Tue, 3 Jan 2023 20:18:10 -0500 Subject: [PATCH 1/6] Fix for the ArithmeticOverflow Problem --- src/invidious/helpers/utils.cr | 2 +- src/invidious/yt_backend/extractors.cr | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index ed0cca38..59d8953a 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -162,7 +162,7 @@ def number_with_separator(number) end def short_text_to_number(short_text : String) : Int64 - matches = /(?\d+(\.\d+)?)\s?(?[mMkKbB])?/.match(short_text) + matches = /(?\d+(\.\d+)?)\s?(?[mMkKbB]|())?/.match(short_text) number = matches.try &.["number"].to_f || 0.0 case matches.try &.["suffix"].downcase diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr index edc722cf..326d2d62 100644 --- a/src/invidious/yt_backend/extractors.cr +++ b/src/invidious/yt_backend/extractors.cr @@ -169,7 +169,12 @@ private module Parsers # When public subscriber count is disabled, the subscriberCountText isn't sent by InnerTube. # Always simpleText # TODO change default value to nil + subscriber_count = item_contents.dig?("subscriberCountText", "simpleText") + if !subscriber_count || !subscriber_count.as_s.includes? " subscriber" + subscriber_count = item_contents.dig?("videoCountText", "simpleText") + end + subscriber_count = subscriber_count .try { |s| short_text_to_number(s.as_s.split(" ")[0]).to_i32 } || 0 # Auto-generated channels doesn't have videoCountText From 0d3610f63d726ac038861d3aede8d7339c552d74 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Wed, 4 Jan 2023 18:12:15 -0500 Subject: [PATCH 2/6] Change regex used in short_text_to_number --- src/invidious/helpers/utils.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 59d8953a..72fdb187 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -162,7 +162,7 @@ def number_with_separator(number) end def short_text_to_number(short_text : String) : Int64 - matches = /(?\d+(\.\d+)?)\s?(?[mMkKbB]|())?/.match(short_text) + matches = /(?\d+(\.\d+)?)\s?(?[mMkKbB]?)/.match(short_text) number = matches.try &.["number"].to_f || 0.0 case matches.try &.["suffix"].downcase From 8d08cfe30f550431015a7ecc8845b9c2968e27be Mon Sep 17 00:00:00 2001 From: DUO Labs Date: Thu, 5 Jan 2023 20:42:11 -0500 Subject: [PATCH 3/6] Add comments to src/invidious/yt_backend/extractors.cr Co-authored-by: Samantaz Fox --- src/invidious/yt_backend/extractors.cr | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr index 326d2d62..cd52c73b 100644 --- a/src/invidious/yt_backend/extractors.cr +++ b/src/invidious/yt_backend/extractors.cr @@ -171,6 +171,11 @@ private module Parsers # TODO change default value to nil subscriber_count = item_contents.dig?("subscriberCountText", "simpleText") + + # Since youtube added channel handles, `VideoCountText` holds the number of + # subscribers and `subscriberCountText` holds the handle, except when the + # channel doesn't have a handle (e.g: some topic music channels). + # See https://github.com/iv-org/invidious/issues/3394#issuecomment-1321261688 if !subscriber_count || !subscriber_count.as_s.includes? " subscriber" subscriber_count = item_contents.dig?("videoCountText", "simpleText") end From 8dcc98b3b9d8e189a4c92ab0cbed7e3635341b5d Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Mon, 16 Jan 2023 18:37:52 -0500 Subject: [PATCH 4/6] If videCountText lists the number of subscribers, then don't use it in get_video_count --- src/invidious/yt_backend/extractors.cr | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr index cd52c73b..d32f5646 100644 --- a/src/invidious/yt_backend/extractors.cr +++ b/src/invidious/yt_backend/extractors.cr @@ -652,8 +652,13 @@ module HelperExtractors # # Returns a 0 when it's unable to do so def self.get_video_count(container : JSON::Any) : Int32 + puts container if box = container["videoCountText"]? - return extract_text(box).try &.gsub(/\D/, "").to_i || 0 + if (extracted_text = extract_text(box)) && !extracted_text.includes? " subscriber" + return extracted_text.gsub(/\D/, "").to_i + else + return 0 + end elsif box = container["videoCount"]? return box.as_s.to_i else From 86333cd4344267f09ca34a179558dc71fb8b6fb4 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Mon, 16 Jan 2023 18:43:58 -0500 Subject: [PATCH 5/6] Formatting --- src/invidious/yt_backend/extractors.cr | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr index d32f5646..fbb02824 100644 --- a/src/invidious/yt_backend/extractors.cr +++ b/src/invidious/yt_backend/extractors.cr @@ -652,13 +652,13 @@ module HelperExtractors # # Returns a 0 when it's unable to do so def self.get_video_count(container : JSON::Any) : Int32 - puts container + puts container if box = container["videoCountText"]? - if (extracted_text = extract_text(box)) && !extracted_text.includes? " subscriber" - return extracted_text.gsub(/\D/, "").to_i - else - return 0 - end + if (extracted_text = extract_text(box)) && !extracted_text.includes? " subscriber" + return extracted_text.gsub(/\D/, "").to_i + else + return 0 + end elsif box = container["videoCount"]? return box.as_s.to_i else From ff66cec9209f464ffc269a7d189199a22b5486c0 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Mon, 16 Jan 2023 18:52:17 -0500 Subject: [PATCH 6/6] Remove debug print --- src/invidious/yt_backend/extractors.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr index fbb02824..1f7726fb 100644 --- a/src/invidious/yt_backend/extractors.cr +++ b/src/invidious/yt_backend/extractors.cr @@ -652,7 +652,6 @@ module HelperExtractors # # Returns a 0 when it's unable to do so def self.get_video_count(container : JSON::Any) : Int32 - puts container if box = container["videoCountText"]? if (extracted_text = extract_text(box)) && !extracted_text.includes? " subscriber" return extracted_text.gsub(/\D/, "").to_i