From b7a252b096f7bc29f105ea25454fe053cf4b4d4b Mon Sep 17 00:00:00 2001 From: RadoslavL Date: Mon, 9 Oct 2023 12:00:37 +0300 Subject: [PATCH] Removed need for more API calls by parsing the publishedTimeText string --- src/invidious/jsonify/api_v1/video_json.cr | 2 +- src/invidious/videos/parser.cr | 99 +++++++++++++--------- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/src/invidious/jsonify/api_v1/video_json.cr b/src/invidious/jsonify/api_v1/video_json.cr index 0deb3204..8a74343c 100644 --- a/src/invidious/jsonify/api_v1/video_json.cr +++ b/src/invidious/jsonify/api_v1/video_json.cr @@ -245,7 +245,7 @@ module Invidious::JSONify::APIv1 json.field "lengthSeconds", rv["length_seconds"]?.try &.to_i 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"]?.try { |t| Time.parse(t, "%Y-%m-%d", Time::Location::UTC).to_unix } || Time.utc + json.field "published", rv["published"]? end end end diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 607a418a..a1b9b988 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -51,47 +51,6 @@ def parse_related_video(related : JSON::Any, published) : Hash(String, JSON::Any } end -def fetch_published(video_id : String, proxy_region : String? = nil) - client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region) - player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config) - playability_status = player_response.dig?("playabilityStatus", "status").try &.as_s - if playability_status != "OK" - subreason = player_response.dig?("playabilityStatus", "errorScreen", "playerErrorMessageRenderer", "subreason") - reason = subreason.try &.[]?("simpleText").try &.as_s - reason ||= subreason.try &.[]("runs").as_a.map(&.[]("text")).join("") - reason ||= player_response.dig("playabilityStatus", "reason").as_s - - # Stop here if video is not a scheduled livestream or - # for LOGIN_REQUIRED when videoDetails element is not found because retrying won't help - if !{"LIVE_STREAM_OFFLINE", "LOGIN_REQUIRED"}.any?(playability_status) || - playability_status == "LOGIN_REQUIRED" && !player_response.dig?("videoDetails") - return { - "version" => JSON::Any.new(Video::SCHEMA_VERSION.to_i64), - "reason" => JSON::Any.new(reason), - } - end - elsif video_id != player_response.dig("videoDetails", "videoId") - # YouTube may return a different video player response than expected. - # See: https://github.com/TeamNewPipe/NewPipe/issues/8713 - # Line to be reverted if one day we solve the video not available issue. - return { - "version" => JSON::Any.new(Video::SCHEMA_VERSION.to_i64), - "reason" => JSON::Any.new("Can't load the video on this Invidious instance. YouTube is currently trying to block Invidious instances. Click here for more info about the issue."), - } - else - reason = nil - end - microformat1 = player_response.dig?("microformat", "playerMicroformatRenderer") - if !microformat1.nil? - published1 = microformat1["publishDate"] - else - published1 = JSON::Any.new("") - end - return { - "published" => published1, - } -end - def extract_video_info(video_id : String, proxy_region : String? = nil) # Init client config for the API client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region) @@ -277,7 +236,63 @@ 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"]? - published1 = fetch_published(item["videoId"].to_s, proxy_region)["published"] + time_string = item["publishedTimeText"]["simpleText"]? + if !time_string.nil? && time_string.to_s.ends_with?("hour ago") + time = Time.utc.to_unix - 3600 + end + if !time_string.nil? && time_string.to_s.ends_with?("hours ago") && !time_string.to_s.starts_with?("Streamed") + hours = time_string.to_s.rchop(" hours ago").to_i + time = Time.utc.to_unix - 3600*hours + end + if !time_string.nil? && time_string.to_s.ends_with?("hours ago") && time_string.to_s.starts_with?("Streamed") + hours = time_string.to_s.lchop("Streamed ").rchop(" hours ago").to_i + time = Time.utc.to_unix - 3600*hours + end + if !time_string.nil? && time_string.to_s.ends_with?("day ago") + time = Time.utc.to_unix - 86400 + end + if !time_string.nil? && time_string.to_s.ends_with?("days ago") && !time_string.to_s.starts_with?("Streamed") + days = time_string.to_s.rchop(" days ago").to_i + time = Time.utc.to_unix - 86400*days + end + if !time_string.nil? && time_string.to_s.ends_with?("days ago") && time_string.to_s.starts_with?("Streamed") + days = time_string.to_s.lchop("Streamed ").rchop(" days ago").to_i + time = Time.utc.to_unix - 86400*days + end + if !time_string.nil? && time_string.to_s.ends_with?("week ago") + time = Time.utc.to_unix - 604800 + end + if !time_string.nil? && time_string.to_s.ends_with?("weeks ago") && !time_string.to_s.starts_with?("Streamed") + weeks = time_string.to_s.rchop(" weeks ago").to_i + time = Time.utc.to_unix - 604800*weeks + end + if !time_string.nil? && time_string.to_s.ends_with?("weeks ago") && time_string.to_s.starts_with?("Streamed") + weeks = time_string.to_s.lchop("Streamed ").rchop(" weeks ago").to_i + time = Time.utc.to_unix - 604800*weeks + end + if !time_string.nil? && time_string.to_s.ends_with?("month ago") + time = Time.utc.to_unix - 2629743 + end + if !time_string.nil? && time_string.to_s.ends_with?("months ago") && !time_string.to_s.starts_with?("Streamed") + months = time_string.to_s.rchop(" months ago").to_i + time = Time.utc.to_unix - 2629743*months + end + if !time_string.nil? && time_string.to_s.ends_with?("months ago") && time_string.to_s.starts_with?("Streamed") + months = time_string.to_s.lchop("Streamed ").rchop(" months ago").to_i + time = Time.utc.to_unix - 2629743*months + end + if !time_string.nil? && time_string.to_s.ends_with?("year ago") + time = Time.utc.to_unix - 31556926 + end + if !time_string.nil? && time_string.to_s.ends_with?("years ago") && !time_string.to_s.starts_with?("Streamed") + years = time_string.to_s.rchop(" years ago").to_i + time = Time.utc.to_unix - 31556926*years + end + if !time_string.nil? && time_string.to_s.ends_with?("years ago") && time_string.to_s.starts_with?("Streamed") + years = time_string.to_s.lchop("Streamed ").rchop(" years ago").to_i + time = Time.utc.to_unix - 31556926*years + end + published1 = JSON::Any.new(time.to_s) related_video = parse_related_video(item, published1) related << JSON::Any.new(related_video) if related_video end