@@ -68,18 +68,20 @@ def extract_video_info(video_id : String)
6868 playability_status = player_response.dig?(" playabilityStatus" , " status" ).try & .as_s
6969
7070 if playability_status != " OK"
71- subreason = player_response.dig?(" playabilityStatus" , " errorScreen" , " playerErrorMessageRenderer" , " subreason" )
72- reason = subreason.try & .[]?(" simpleText" ).try & .as_s
73- reason ||= subreason.try & .[](" runs" ).as_a.map(& .[](" text" )).join(" " )
74- reason ||= player_response.dig(" playabilityStatus" , " reason" ).as_s
75-
76- # Stop here if video is not a scheduled livestream or
77- # for LOGIN_REQUIRED when videoDetails element is not found because retrying won't help
78- if ! {" LIVE_STREAM_OFFLINE" , " LOGIN_REQUIRED" }.any?(playability_status) ||
79- playability_status == " LOGIN_REQUIRED" && ! player_response.dig?(" videoDetails" )
71+ reason = player_response.dig?(" playabilityStatus" , " reason" ).try & .as_s
72+ reason ||= player_response.dig(" playabilityStatus" , " errorScreen" , " playerErrorMessageRenderer" , " reason" , " simpleText" ).as_s
73+ subreason_main = player_response.dig?(" playabilityStatus" , " errorScreen" , " playerErrorMessageRenderer" , " subreason" )
74+ subreason = subreason_main.try & .[]?(" simpleText" ).try & .as_s
75+ subreason ||= subreason_main.try & .[](" runs" ).as_a.map(& .[](" text" )).join(" " )
76+
77+ # Stop if private video or video not found.
78+ # But for video unavailable, only stop if playability_status is ERROR because playability_status UNPLAYABLE
79+ # still gives all the necessary info for displaying the video page (title, description and more)
80+ if {" Private video" , " Video unavailable" }.any?(reason) && ! {" UNPLAYABLE" }.any?(playability_status)
8081 return {
81- " version" => JSON ::Any .new(Video ::SCHEMA_VERSION .to_i64),
82- " reason" => JSON ::Any .new(reason),
82+ " version" => JSON ::Any .new(Video ::SCHEMA_VERSION .to_i64),
83+ " reason" => JSON ::Any .new(reason),
84+ " subreason" => JSON ::Any .new(subreason),
8385 }
8486 end
8587 elsif video_id != player_response.dig(" videoDetails" , " videoId" )
@@ -99,11 +101,8 @@ def extract_video_info(video_id : String)
99101 reason = nil
100102 end
101103
102- # Don't fetch the next endpoint if the video is unavailable.
103- if {" OK" , " LIVE_STREAM_OFFLINE" , " LOGIN_REQUIRED" }.any?(playability_status)
104- next_response = YoutubeAPI .next({" videoId" : video_id, " params" : " " })
105- player_response = player_response.merge(next_response)
106- end
104+ next_response = YoutubeAPI .next({" videoId" : video_id, " params" : " " })
105+ player_response = player_response.merge(next_response)
107106
108107 params = parse_video_info(video_id, player_response)
109108 params[" reason" ] = JSON ::Any .new(reason) if reason
@@ -197,16 +196,20 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
197196 end
198197
199198 video_details = player_response.dig?(" videoDetails" )
199+ video_details ||= {} of String => JSON ::Any
200200 if ! (microformat = player_response.dig?(" microformat" , " playerMicroformatRenderer" ))
201201 microformat = {} of String => JSON ::Any
202202 end
203203
204- raise BrokenTubeException .new(" videoDetails" ) if ! video_details
205-
206204 # Basic video infos
207205
208206 title = video_details[" title" ]?.try & .as_s
209207
208+ title ||= extract_text(
209+ video_primary_renderer
210+ .try & .dig?(" title" )
211+ )
212+
210213 # We have to try to extract viewCount from videoPrimaryInfoRenderer first,
211214 # then from videoDetails, as the latter is "0" for livestreams (we want
212215 # to get the amount of viewers watching).
@@ -483,7 +486,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
483486 # Description
484487 " description" => JSON ::Any .new(description || " " ),
485488 " descriptionHtml" => JSON ::Any .new(description_html || " <p></p>" ),
486- " shortDescription" => JSON ::Any .new(short_description.try & .as_s || nil ),
489+ " shortDescription" => JSON ::Any .new(short_description.try & .as_s || " " ),
487490 # Video metadata
488491 " genre" => JSON ::Any .new(genre.try & .as_s || " " ),
489492 " genreUcid" => JSON ::Any .new(genre_ucid.try & .as_s?),
0 commit comments