* Caching and refactor * Fix and tweak some parsing * Missing imports * refactor
243 lines
7.6 KiB
Plaintext
243 lines
7.6 KiB
Plaintext
namespace Innertube
|
|
|
|
function CreatePlayerRequest(videoId as string, ctt as string) as object
|
|
deviceInfo = CreateObject("roDeviceInfo")
|
|
|
|
payload = {
|
|
"videoId": videoId
|
|
"context": Innertube.CreateContext(Innertube.ClientType.IOS, deviceInfo, ctt)
|
|
"playbackContext": {
|
|
"contentPlaybackContext": {
|
|
"vis": 0
|
|
"splay": false
|
|
"referer": "https://www.youtube.com/watch?v=" + videoId
|
|
"currentUrl": "/watch?v=" + videoId
|
|
"autonavState": "STATE_ON"
|
|
"autoCaptionsDefaultOn": false
|
|
"html5Preference": "HTML5_PREF_WANTS"
|
|
"lactMilliseconds": "-1"
|
|
}
|
|
}
|
|
"attestationRequest": {
|
|
"omitBotguardData": true
|
|
}
|
|
"racyCheckOk": true
|
|
"contentCheckOk": true
|
|
}
|
|
|
|
request = HttpClient.PostJson("https://www.youtube.com/youtubei/v1/player?prettyPrint=false&alt=json", payload)
|
|
headers = {
|
|
"accept": "*/*"
|
|
"accept-language": "*"
|
|
}
|
|
headers.Append(Innertube.CreateHeaders(Innertube.ClientType.IOS))
|
|
request.Headers(headers)
|
|
|
|
return request
|
|
end function
|
|
|
|
function ParseInnertubePlayerResponse(payload as object) as object
|
|
if not IsAssociativeArray(payload)
|
|
return {
|
|
error: "Invalid payload"
|
|
}
|
|
end if
|
|
|
|
playabilityStatus = payload["playabilityStatus"]
|
|
if not IsAssociativeArray(playabilityStatus)
|
|
return {
|
|
error: "Invalid playability status"
|
|
}
|
|
end if
|
|
|
|
if playabilityStatus["status"] <> "OK"
|
|
errorMessage = ""
|
|
if IsString(playabilityStatus["reason"])
|
|
errorMessage = playabilityStatus["reason"]
|
|
end if
|
|
|
|
subreason = ParseText(ObjectUtils.Dig(playabilityStatus, ["errorScreen", "playerErrorMessageRenderer", "subreason"]))
|
|
|
|
if subreason <> ""
|
|
errorMessage += `\n` + subreason
|
|
end if
|
|
|
|
if errorMessage = ""
|
|
errorMessage = "Video not available"
|
|
end if
|
|
|
|
return {
|
|
error: errorMessage
|
|
}
|
|
end if
|
|
|
|
videoDetails = payload["videoDetails"]
|
|
if not IsAssociativeArray(videoDetails)
|
|
return {
|
|
error: "Invalid video details"
|
|
}
|
|
end if
|
|
|
|
streamingData = payload["streamingData"]
|
|
if not IsAssociativeArray(streamingData)
|
|
return {
|
|
error: "Invalid streaming data"
|
|
}
|
|
end if
|
|
|
|
lengthSeconds = videoDetails["lengthSeconds"].ToInt()
|
|
videoInfo = {
|
|
"type": "video"
|
|
"title": videoDetails["title"]
|
|
"videoId": videoDetails["videoId"]
|
|
"videoThumbnails": videoDetails["thumbnail"]["thumbnails"]
|
|
"storyboards": ParseStoryboards(payload, lengthSeconds)
|
|
"viewCount": videoDetails["viewCount"].ToInt()
|
|
"author": videoDetails["author"]
|
|
"authorId": videoDetails["channelId"]
|
|
"lengthSeconds": lengthSeconds
|
|
"liveNow": ValidBool(videoDetails["isLive"])
|
|
"hlsUrl": streamingData["hlsManifestUrl"]
|
|
"adaptiveFormats": []
|
|
"formatStreams": []
|
|
"captions": ParseCaptions(payload)
|
|
"recommendedVideos": []
|
|
}
|
|
|
|
return videoInfo
|
|
end function
|
|
|
|
function ParseStoryboards(payload as object, lengthSeconds as integer) as object
|
|
storyboard = ObjectUtils.Dig(payload, ["storyboards", "playerLiveStoryboardSpecRenderer", "spec"])
|
|
if IsString(storyboard)
|
|
storyboard = storyboard.Split("#")
|
|
if storyboard.Count() <> 5
|
|
return []
|
|
end if
|
|
return [{
|
|
"templateUrl": storyboard[0]
|
|
"width": storyboard[1].ToInt()
|
|
"height": storyboard[2].ToInt()
|
|
"count": -1
|
|
"interval": 5000
|
|
"storyboardHeight": storyboard[3].ToInt()
|
|
"storyboardWidth": storyboard[4].ToInt()
|
|
}]
|
|
end if
|
|
|
|
storyboards = ObjectUtils.Dig(payload, ["storyboards", "playerStoryboardSpecRenderer", "spec"])
|
|
if not IsString(storyboards)
|
|
return []
|
|
end if
|
|
|
|
storyboardsData = storyboards.Split("|")
|
|
if storyboardsData.Count() < 2
|
|
return []
|
|
end if
|
|
|
|
baseUrl = storyboardsData.Shift()
|
|
storyboards = []
|
|
|
|
index = 0
|
|
for each sb in storyboardsData
|
|
sbData = sb.Split("#")
|
|
if sbData.Count() <> 8
|
|
index += 1
|
|
continue for
|
|
end if
|
|
|
|
width = sbData[0].ToInt()
|
|
height = sbData[1].ToInt()
|
|
_count = sbData[2].ToInt()
|
|
columns = sbData[3].ToInt()
|
|
rows = sbData[4].ToInt()
|
|
interval = sbData[5].ToInt()
|
|
name = sbData[6]
|
|
sigh = sbData[7]
|
|
|
|
url = baseUrl
|
|
url = url.Replace("$L", `${index}`)
|
|
url = url.Replace("$N", name)
|
|
|
|
queryComponents = UrlUtils.ParseQueryComponents(url)
|
|
queryComponents["sigh"] = sigh.DecodeUriComponent()
|
|
url = UrlUtils.SetQueryParams(url, queryComponents)
|
|
|
|
thumbnailsPerImage = columns * rows
|
|
imagesCount = _count \ thumbnailsPerImage
|
|
if _count mod thumbnailsPerImage > 0
|
|
imagesCount += 1
|
|
end if
|
|
|
|
if interval = 0 and _count > 0
|
|
interval = Cint(Cdbl(lengthSeconds) * 1000.0 / Cdbl(_count))
|
|
end if
|
|
|
|
storyboards.Push({
|
|
"templateUrl": url
|
|
"width": width
|
|
"height": height
|
|
"count": _count
|
|
"interval": interval
|
|
"storyboardWidth": columns
|
|
"storyboardHeight": rows
|
|
"storyboardCount": imagesCount
|
|
})
|
|
index += 1
|
|
end for
|
|
|
|
return storyboards
|
|
end function
|
|
|
|
function ParseCaptions(payload as object) as object
|
|
tracks = ObjectUtils.Dig(payload, ["captions", "playerCaptionsTracklistRenderer", "captionTracks"])
|
|
if not IsArray(tracks)
|
|
return []
|
|
end if
|
|
|
|
captions = []
|
|
for each track in tracks
|
|
if not IsAssociativeArray(track)
|
|
continue for
|
|
end if
|
|
|
|
baseUrl = track["baseUrl"]
|
|
if not IsString(baseUrl)
|
|
continue for
|
|
end if
|
|
|
|
queryComponents = UrlUtils.ParseQueryComponents(baseUrl)
|
|
queryComponents["fmt"] = "vtt"
|
|
baseUrl = UrlUtils.SetQueryParams(baseUrl, queryComponents)
|
|
|
|
languageCode = track["languageCode"]
|
|
if not IsString(languageCode)
|
|
continue for
|
|
end if
|
|
|
|
label = ""
|
|
name = track["name"]
|
|
if IsAssociativeArray(name)
|
|
runs = name["runs"]
|
|
if IsArray(runs)
|
|
for each rn in runs
|
|
text = rn["text"]
|
|
if IsString(text)
|
|
label += text
|
|
end if
|
|
end for
|
|
end if
|
|
end if
|
|
|
|
captions.Push({
|
|
"label": label
|
|
"language_code": languageCode
|
|
"url": baseUrl
|
|
})
|
|
end for
|
|
|
|
return captions
|
|
end function
|
|
|
|
end namespace
|