From ceb1feb3502367ec41c8ae7e28a0cfbd7a2e2170 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 25 Nov 2021 23:16:50 +0100 Subject: [PATCH] likes/dislikes: better fallback management '.to_i64?' instead of '.to_i64' returns nil rather than raising an exception when it's done on an empty string. In some rare cases, rating can be equal to 5. In this case, the value of player_response[videoDetails][averageRating] is an Int and not a Float. --- src/invidious/videos.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 1f9a6bc9..d04c6ecb 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -903,7 +903,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_ if likes_button likes_txt = (likes_button["defaultText"]? || likes_button["toggledText"]?) .try &.dig?("accessibility", "accessibilityData", "label") - likes = likes_txt.as_s.gsub(/\D/, "").to_i64 if likes_txt + likes = likes_txt.as_s.gsub(/\D/, "").to_i64? if likes_txt LOGGER.trace("extract_video_info: Found \"likes\" button. Button text is \"#{likes_txt}\"") LOGGER.debug("extract_video_info: Likes count is #{likes}") if likes @@ -916,7 +916,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_ if dislikes_button dislikes_txt = (dislikes_button["defaultText"] || dislikes_button["toggledText"]?) .try &.dig?("accessibility", "accessibilityData", "label") - dislikes = dislikes_txt.as_s.gsub(/\D/, "").to_i64 if dislikes_txt + dislikes = dislikes_txt.as_s.gsub(/\D/, "").to_i64? if dislikes_txt LOGGER.trace("extract_video_info: Found \"dislikes\" button. Button text is \"#{dislikes_txt}\"") LOGGER.debug("extract_video_info: Dislikes count is #{dislikes}") if dislikes @@ -924,7 +924,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_ end if likes && likes != 0_i64 && (!dislikes || dislikes == 0_i64) - if rating = player_response.dig?("videoDetails", "averageRating").try &.as_f + if rating = player_response.dig?("videoDetails", "averageRating").try { |x| x.as_i64? || x.as_f? } dislikes = (likes * ((5 - rating)/(rating - 1))).round.to_i64 LOGGER.debug("extract_video_info: Dislikes count (using fallback method) is #{dislikes}") end