From 68611482906f3b70e9c105fec5c57436c877a3b0 Mon Sep 17 00:00:00 2001 From: RadoslavL Date: Fri, 24 Nov 2023 11:24:56 +0200 Subject: [PATCH] Moved code around and fixed a problem --- src/invidious/jsonify/api_v1/video_json.cr | 2 +- src/invidious/videos/parser.cr | 21 ++++++++------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/invidious/jsonify/api_v1/video_json.cr b/src/invidious/jsonify/api_v1/video_json.cr index f5450f3f..622641ee 100644 --- a/src/invidious/jsonify/api_v1/video_json.cr +++ b/src/invidious/jsonify/api_v1/video_json.cr @@ -246,7 +246,7 @@ module Invidious::JSONify::APIv1 json.field "viewCountText", rv["short_view_count"]? json.field "viewCount", rv["view_count"]?.try &.empty? ? nil : rv["view_count"].to_i64 json.field "published", rv["published"]? - json.field "publishedTimeText", translate(locale, "`x` ago", rv["publishedText"].to_s) + json.field "publishedTimeText", translate(locale, "`x` ago", rv["publishedText"].to_s.gsub(" ago", "")) end end end diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 279daf37..9275b1bb 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -6,7 +6,7 @@ require "json" # # TODO: "compactRadioRenderer" (Mix) and # TODO: Use a proper struct/class instead of a hacky JSON object -def parse_related_video(related : JSON::Any, published : String? = nil) : Hash(String, JSON::Any)? +def parse_related_video(related : JSON::Any) : Hash(String, JSON::Any)? return nil if !related["videoId"]? # The compact renderer has video length in seconds, where the end @@ -36,11 +36,12 @@ def parse_related_video(related : JSON::Any, published : String? = nil) : Hash(S LOGGER.trace("parse_related_video: Found \"watchNextEndScreenRenderer\" container") - publishedText = related["publishedTimeText"]? - if !publishedText.nil? - publishedSimpleText = publishedText["simpleText"].to_s + if published_time_text = related["publishedTimeText"]? + decoded_time = decode_date(published_time_text["simpleText"].to_s) + published = decoded_time.to_unix.to_s + published_time_text = published_time_text["simpleText"].to_s else - publishedSimpleText = nil + published = nil end # TODO: when refactoring video types, make a struct for related videos @@ -55,7 +56,7 @@ def parse_related_video(related : JSON::Any, published : String? = nil) : Hash(S "short_view_count" => JSON::Any.new(short_view_count || "0"), "author_verified" => JSON::Any.new(author_verified), "published" => JSON::Any.new(published || ""), - "publishedText" => JSON::Any.new(publishedSimpleText || ""), + "publishedText" => JSON::Any.new(published_time_text || ""), } end @@ -244,13 +245,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any .dig?("secondaryResults", "secondaryResults", "results") secondary_results.try &.as_a.each do |element| if item = element["compactVideoRenderer"]? - if rv_published_time_text = item["publishedTimeText"]? - rv_decoded_time = decode_date(rv_published_time_text["simpleText"].to_s) - rv_published_timestamp = rv_decoded_time.to_unix.to_s - else - rv_published_timestamp = nil - end - related_video = parse_related_video(item, published: rv_published_timestamp) + related_video = parse_related_video(item) related << JSON::Any.new(related_video) if related_video end end