* qrcode * add stuff * Some oauth stuff * remove some test code * Lint fix * Revert "add stuff" This reverts commit 8508471e4a0a2bfa06d4f04bfdece1f3c9191e1c. * Lint fix --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
199 lines
7.0 KiB
Plaintext
199 lines
7.0 KiB
Plaintext
import "pkg:/source/services/HttpClient.bs"
|
|
import "pkg:/source/utils/TimeUtils.bs"
|
|
|
|
namespace Innertube
|
|
|
|
function GetClientIdentity(cancellation = invalid as dynamic) as object
|
|
request = HttpClient.Get("https://www.youtube.com/tv")
|
|
request.Headers({
|
|
"User-Agent": "Mozilla/5.0 (ChromiumStylePlatform) Cobalt/Version"
|
|
"Referer": "https://www.youtube.com/tv"
|
|
"Accept-Language": "en-US"
|
|
})
|
|
request.Cancellation(cancellation)
|
|
|
|
response = request.Await()
|
|
if not response.IsSuccess()
|
|
throw `Failed to get client id: ${response.ErrorMessage()}`
|
|
end if
|
|
|
|
text = response.Text()
|
|
|
|
scriptRegex = /<script\s+id="base-js"\s+src="([^"]+)"[^>]*><\/script>/
|
|
|
|
match = scriptRegex.Match(text)
|
|
if match.Count() < 2
|
|
throw "Could not find base-js script"
|
|
end if
|
|
|
|
baseJsUrl = "https://www.youtube.com" + match[1]
|
|
|
|
request = HttpClient.Get(baseJsUrl)
|
|
request.Cancellation(cancellation)
|
|
response = request.Await()
|
|
|
|
if not response.IsSuccess()
|
|
throw `Failed to get base js: ${response.ErrorMessage()}`
|
|
end if
|
|
|
|
text = response.Text()
|
|
|
|
clientIdRegex = /clientId:"(?<client_id>[^"]+)",[^"]*?:"(?<client_secret>[^"]+)"/
|
|
|
|
match = clientIdRegex.Match(text)
|
|
if match.Count() < 3
|
|
throw "Could not find client id"
|
|
end if
|
|
|
|
return {
|
|
"clientId": match[1]
|
|
"clientSecret": match[2]
|
|
}
|
|
end function
|
|
|
|
function GetDeviceAndUserCode(clientId as string, cancellation = invalid as dynamic) as object
|
|
deviceInfo = CreateObject("roDeviceInfo")
|
|
|
|
payload = {
|
|
"client_id": clientId
|
|
"scope": "http://gdata.youtube.com https://www.googleapis.com/auth/youtube-paid-content"
|
|
"device_id": deviceInfo.GetRandomUUID()
|
|
"device_model": "ytlr::"
|
|
}
|
|
|
|
request = HttpClient.PostJson("https://www.youtube.com/o/oauth2/device/code", payload)
|
|
request.Cancellation(cancellation)
|
|
response = request.Await()
|
|
|
|
if not response.IsSuccess()
|
|
throw `Failed to get device code: ${response.ErrorMessage()}`
|
|
end if
|
|
|
|
responseData = ToCamelCase(response.Json())
|
|
|
|
if responseData.DoesExist("errorCode")
|
|
throw "Failed to get device code: " + ToString(responseData)
|
|
end if
|
|
|
|
if responseData.DoesExist("expiresIn")
|
|
responseData["expiresTimestamp"] = TimeUtils.Now().AsSeconds() + responseData["expiresIn"]
|
|
end if
|
|
|
|
return responseData
|
|
end function
|
|
|
|
function PollForAccessToken(clientIdentity as object, deviceAndUserCode as object, cancellation = invalid as dynamic) as object
|
|
payload = {
|
|
"client_id": clientIdentity.clientId
|
|
"client_secret": clientIdentity.clientSecret
|
|
"code": deviceAndUserCode.deviceCode
|
|
"grant_type": "http://oauth.net/grant_type/device/1.0"
|
|
}
|
|
|
|
intervalMs = deviceAndUserCode.interval * 1000
|
|
|
|
while true
|
|
request = HttpClient.PostJson("https://www.youtube.com/o/oauth2/token", payload)
|
|
request.Cancellation(cancellation)
|
|
response = request.Await()
|
|
|
|
if not response.IsSuccess()
|
|
errorMessage = response.ErrorMessage()
|
|
LogError(errorMessage)
|
|
return {
|
|
"error": errorMessage
|
|
}
|
|
end if
|
|
|
|
responseData = ToCamelCase(response.Json())
|
|
if not responseData.DoesExist("error")
|
|
if responseData.DoesExist("expiresIn")
|
|
responseData["expiresTimestamp"] = TimeUtils.Now().AsSeconds() + responseData["expiresIn"]
|
|
end if
|
|
responseData["clientId"] = clientIdentity["clientId"]
|
|
responseData["clientSecret"] = clientIdentity["clientSecret"]
|
|
return responseData
|
|
end if
|
|
|
|
if responseData.error = "authorization_pending"
|
|
LogInfo("Waiting for user to authorize device", responseData)
|
|
sleep(intervalMs)
|
|
if CancellationUtils.IsCancelled(cancellation)
|
|
return invalid
|
|
end if
|
|
continue while
|
|
else if responseData.error = "slow_down"
|
|
LogWarn("Polling too fast, slowing down", responseData)
|
|
sleep(intervalMs + 3000)
|
|
if CancellationUtils.IsCancelled(cancellation)
|
|
return invalid
|
|
end if
|
|
continue while
|
|
else if responseData.error = "expired_token"
|
|
throw "Failed to get access token: " + ToString(responseData)
|
|
else if responseData.error = "access_denied"
|
|
throw "Failed to get access token: " + ToString(responseData)
|
|
else
|
|
throw "Failed to get access token: " + ToString(responseData)
|
|
end if
|
|
end while
|
|
|
|
return invalid
|
|
end function
|
|
|
|
function RefreshAccessToken(accessToken as object, cancellation = invalid as dynamic) as object
|
|
payload = {
|
|
"client_id": accessToken.clientId
|
|
"client_secret": accessToken.clientSecret
|
|
"refresh_token": accessToken.refreshToken
|
|
"grant_type": "refresh_token"
|
|
}
|
|
|
|
request = HttpClient.PostJson("https://www.youtube.com/o/oauth2/token", payload)
|
|
request.Cancellation(cancellation)
|
|
response = request.Await()
|
|
|
|
if not response.IsSuccess()
|
|
throw `Failed to refresh token: ${response.ErrorMessage()}`
|
|
end if
|
|
|
|
responseData = ToCamelCase(response.Json())
|
|
if responseData.DoesExist("errorCode")
|
|
throw "Failed to refresh token: " + ToString(responseData)
|
|
end if
|
|
|
|
return responseData
|
|
end function
|
|
|
|
function RevokeAccessToken(accessToken as string, cancellation = invalid as dynamic) as boolean
|
|
request = HttpClient.Post("https://www.youtube.com/o/oauth2/revoke", "")
|
|
request.QueryParam("token", accessToken)
|
|
request.Cancellation(cancellation)
|
|
response = request.Await()
|
|
success = response.IsSuccess()
|
|
if success
|
|
LogError(`Failed to revoke token: ${response.ErrorMessage()}`)
|
|
end if
|
|
return success
|
|
end function
|
|
|
|
function ToCamelCase(obj as object) as object
|
|
if not IsAssociativeArray(obj)
|
|
return obj
|
|
end if
|
|
|
|
newObj = {}
|
|
for each key in obj
|
|
newKey = key
|
|
underScoreIndex = newKey.InStr("_")
|
|
while underScoreIndex <> -1 and underScoreIndex < newKey.Len() - 2
|
|
newKey = newKey.Left(underScoreIndex) + UCase(newKey.Mid(underScoreIndex + 1, 1)) + newKey.Mid(underScoreIndex + 2)
|
|
underScoreIndex = newKey.InStr("_")
|
|
end while
|
|
newObj[newKey] = obj[key]
|
|
end for
|
|
return newObj
|
|
end function
|
|
|
|
end namespace
|