Attempt to improve visitor data fetch error handling
This commit is contained in:
1 parent
9d23efe7e6
commit
5e34ce8067
5 files changed
+96
-64
No files matched your search
@@ -4,6 +4,7 @@ import "pkg:/components/Services/Innertube/Constants.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
function Init()
|
||||
m.top.observeField("requestVisitorData", FuncName(OnRequestVisitorData))
|
||||
end function
|
||||
|
||||
function OnNodeReady()
|
||||
@@ -11,14 +12,14 @@ function OnNodeReady()
|
||||
end function
|
||||
|
||||
function OnRequestVisitorData() as void
|
||||
if m.innertubeSessionDataJob <> invalid
|
||||
if m.innertubeSessionDataJob <> invalid and not m.innertubeSessionDataJob.done
|
||||
return
|
||||
end if
|
||||
|
||||
OnForceRequestVisitorData()
|
||||
RequestVisitorData()
|
||||
end function
|
||||
|
||||
function OnForceRequestVisitorData() as void
|
||||
function RequestVisitorData() as void
|
||||
if m.innertubeSessionDataJob <> invalid
|
||||
m.innertubeSessionDataJob.cancel = true
|
||||
m.innertubeSessionDataJob = invalid
|
||||
@@ -40,4 +41,8 @@ end function
|
||||
function OnInnertubeSessionDataJobDone(event as object) as void
|
||||
JobSystem.UnobserveCallback(event.getRoSGNode())
|
||||
m.innertubeSessionDataJob = invalid
|
||||
if StringUtils.IsNullOrEmpty(m.top.visitorData)
|
||||
' Trigger observers in case the job threw an error and didn't set the visitorData
|
||||
m.top.visitorData = ""
|
||||
end if
|
||||
end function
|
||||
@@ -1,9 +1,8 @@
|
||||
<component name="Innertube" extends="Node" includes="AutoBind">
|
||||
<interface>
|
||||
<field id="timestamp" type="int" />
|
||||
<field id="visitorData" type="string" />
|
||||
<field id="requestVisitorData" type="boolean" alwaysNotify="true" onChange="OnRequestVisitorData" />
|
||||
<field id="forceRequestVisitorData" type="boolean" alwaysNotify="true" onChange="OnForceRequestVisitorData" />
|
||||
<field id="visitorData" type="string" alwaysNotify="true" />
|
||||
<field id="requestVisitorData" type="boolean" alwaysNotify="true" />
|
||||
<field id="jobQueue" type="node" />
|
||||
</interface>
|
||||
</component>
|
||||
@@ -1,26 +1,28 @@
|
||||
import "pkg:/components/Services/Innertube/SessionData.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
@job("InnertubeSessionDataJob")
|
||||
function ExecuteJob() as void
|
||||
input = JobGetInput()
|
||||
innertubeNode = JobGetInput().innertube
|
||||
|
||||
innertubeNode = input.innertube
|
||||
|
||||
cancellation = JobGetCancellation()
|
||||
sessionData = Innertube.GetSessionData(cancellation)
|
||||
if sessionData.error <> invalid
|
||||
JobError(sessionData.error)
|
||||
return
|
||||
cached = Innertube.ReadSessionDataFromRegistry()
|
||||
if cached <> invalid and not StringUtils.IsNullOrEmpty(cached.visitorData)
|
||||
if IsInt(cached.timestamp)
|
||||
innertubeNode.timestamp = cached.timestamp
|
||||
end if
|
||||
innertubeNode.visitorData = cached.visitorData
|
||||
if Innertube.IsSessionDataFresh(cached)
|
||||
return
|
||||
end if
|
||||
end if
|
||||
|
||||
innertubeNode.queueFields(true)
|
||||
if IsInt(sessionData.timestamp)
|
||||
innertubeNode.timestamp = sessionData.timestamp
|
||||
fetched = Innertube.RequestSessionData(JobGetCancellation())
|
||||
if fetched <> invalid
|
||||
Innertube.SaveSessionDataToRegistry(fetched)
|
||||
innertubeNode.timestamp = fetched.timestamp
|
||||
innertubeNode.visitorData = fetched.visitorData
|
||||
else if StringUtils.IsNullOrEmpty(innertubeNode.visitorData)
|
||||
' Refresh failed and we never had an id — wake a cold waiter to fail. Never blank a valid id.
|
||||
innertubeNode.visitorData = ""
|
||||
end if
|
||||
|
||||
if not StringUtils.IsNullOrEmpty(sessionData.visitorData)
|
||||
innertubeNode.visitorData = sessionData.visitorData
|
||||
end if
|
||||
|
||||
innertubeNode.queueFields(false)
|
||||
end function
|
||||
@@ -11,18 +11,12 @@ namespace Innertube
|
||||
const SESSION_DATA_VERSION = 1
|
||||
const INNERTUBE_VISITOR_DATA_LIFESPAN_SECONDS = 60 * 60 * 24 * 14 ' 14 days
|
||||
|
||||
function GetSessionData(cancellation = invalid as object) as object
|
||||
sessionData = ReadSessionDataFromRegistry()
|
||||
if sessionData <> invalid
|
||||
return sessionData
|
||||
function IsSessionDataFresh(sessionData as object) as boolean
|
||||
if sessionData = invalid or not IsInt(sessionData.timestamp)
|
||||
return false
|
||||
end if
|
||||
|
||||
sessionData = RequestSessionData(cancellation)
|
||||
if sessionData.error <> invalid
|
||||
return sessionData
|
||||
end if
|
||||
SaveSessionDataToRegistry(sessionData)
|
||||
return sessionData
|
||||
nowSeconds = TimeUtils.Now().AsSeconds()
|
||||
return nowSeconds - sessionData.timestamp <= INNERTUBE_VISITOR_DATA_LIFESPAN_SECONDS
|
||||
end function
|
||||
|
||||
function RequestSessionData(cancellation = invalid as object) as object
|
||||
@@ -35,12 +29,12 @@ namespace Innertube
|
||||
})
|
||||
request.Cancellation(cancellation)
|
||||
request.TryCount(2)
|
||||
request.NoCache()
|
||||
|
||||
response = request.Await()
|
||||
if not response.IsSuccess()
|
||||
return {
|
||||
"error": "Failed to fetch session data: " + response.ErrorMessage()
|
||||
}
|
||||
LogError("Failed to fetch session data: " + response.ErrorMessage())
|
||||
return invalid
|
||||
end if
|
||||
|
||||
text = response.Text()
|
||||
@@ -48,16 +42,14 @@ namespace Innertube
|
||||
|
||||
json = StringUtils.ParseJsonSafe(text)
|
||||
if json = invalid
|
||||
return {
|
||||
"error": "Failed to parse session data: " + text
|
||||
}
|
||||
LogError("Failed to parse session data: " + text)
|
||||
return invalid
|
||||
end if
|
||||
|
||||
visitorData = ObjectUtils.Dig(json, [0, 2, 0, 0, 13])
|
||||
if StringUtils.IsNullOrEmpty(visitorData)
|
||||
return {
|
||||
"error": "Failed to find visitorData in session data: " + text
|
||||
}
|
||||
LogError("Failed to find visitorData in session data: " + text)
|
||||
return invalid
|
||||
end if
|
||||
|
||||
return {
|
||||
@@ -84,13 +76,7 @@ namespace Innertube
|
||||
return invalid
|
||||
end if
|
||||
|
||||
timestamp = sessionData.timestamp
|
||||
nowSeconds = TimeUtils.Now().AsSeconds()
|
||||
if nowSeconds - timestamp > INNERTUBE_VISITOR_DATA_LIFESPAN_SECONDS
|
||||
RegistryUtils.Delete(RegistryUtils.INNERTUBE_SESSION_DATA)
|
||||
return invalid
|
||||
end if
|
||||
|
||||
return sessionData
|
||||
end function
|
||||
|
||||
end namespace
|
||||
@@ -1,5 +1,7 @@
|
||||
import "pkg:/components/Services/Innertube/InnertubeService.bs"
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
import "pkg:/source/utils/CancellationUtils.bs"
|
||||
import "pkg:/source/utils/MessagePortUtils.bs"
|
||||
|
||||
@job("VideoContentJob")
|
||||
function ExecuteJob() as void
|
||||
@@ -32,19 +34,15 @@ function ExecuteJob() as void
|
||||
if not IsAssociativeArray(metadata)
|
||||
cancellation = JobGetCancellation()
|
||||
if backend = "playlet"
|
||||
visitorData = innertubeNode.visitorData
|
||||
if StringUtils.IsNullOrEmpty(visitorData)
|
||||
messagePort = CreateObject("roMessagePort")
|
||||
MessagePortUtils.ObserveFieldScopedExSafe(innertubeNode, "visitorData", messagePort)
|
||||
innertubeNode.requestVisitorData = true
|
||||
message = wait(20000, messagePort)
|
||||
innertubeNode.unobserveFieldScoped("visitorData")
|
||||
if message = invalid
|
||||
innertubeNode.forceRequestVisitorData = true
|
||||
JobError("Failed to get visitor data. Please try again later.")
|
||||
return
|
||||
end if
|
||||
visitorData = message.getData()
|
||||
|
||||
visitorData = GetVisitorData(innertubeNode, cancellation)
|
||||
if visitorData.cancel = true
|
||||
JobCancel()
|
||||
return
|
||||
end if
|
||||
if visitorData.error <> invalid
|
||||
JobError(visitorData.error)
|
||||
return
|
||||
end if
|
||||
|
||||
accessToken = invalid
|
||||
@@ -58,7 +56,7 @@ function ExecuteJob() as void
|
||||
metadata = InnertubeService.GetVideoMetadata(contentNode.videoId, {
|
||||
"cancellation": cancellation
|
||||
"credentialTransferToken": loungeServiceNode.credentialTransferToken
|
||||
"visitorData": visitorData
|
||||
"visitorData": visitorData.visitorData
|
||||
"accessToken": accessToken
|
||||
"onBehalfOfUser": onBehalfOfUser
|
||||
})
|
||||
@@ -154,6 +152,48 @@ function ExecuteJob() as void
|
||||
end if
|
||||
end function
|
||||
|
||||
function GetVisitorData(innertubeNode as object, cancellation as object) as object
|
||||
port = CreateObject("roMessagePort")
|
||||
|
||||
MessagePortUtils.ObserveFieldScopedExSafe(innertubeNode, "visitorData", port)
|
||||
CancellationUtils.ObserveCancellationScoped(cancellation, port)
|
||||
|
||||
visitorData = innertubeNode.visitorData
|
||||
if not StringUtils.IsNullOrEmpty(visitorData)
|
||||
innertubeNode.unobserveFieldScoped("visitorData")
|
||||
CancellationUtils.UnobserveCancellationScoped(cancellation)
|
||||
|
||||
return {
|
||||
visitorData: visitorData
|
||||
}
|
||||
end if
|
||||
|
||||
innertubeNode.requestVisitorData = true
|
||||
message = wait(30000, port)
|
||||
|
||||
innertubeNode.unobserveFieldScoped("visitorData")
|
||||
CancellationUtils.UnobserveCancellationScoped(cancellation)
|
||||
|
||||
if message <> invalid
|
||||
if CancellationUtils.IsCancellationEvent(cancellation, message)
|
||||
return {
|
||||
cancel: true
|
||||
}
|
||||
end if
|
||||
visitorData = message.getData()
|
||||
end if
|
||||
|
||||
if StringUtils.IsNullOrEmpty(visitorData)
|
||||
return {
|
||||
error: "Failed to get visitor data. Please try again later."
|
||||
}
|
||||
end if
|
||||
|
||||
return {
|
||||
visitorData: visitorData
|
||||
}
|
||||
end function
|
||||
|
||||
function CreateStreamUrls(metadata as object, service as Invidious.InvidiousService, preferences as object, backend as string, playletServerPort as integer) as object
|
||||
instance = service.GetInstance()
|
||||
proxyVideos = preferences["invidious.proxy_videos"]
|
||||
|
||||
Reference in new issue
Block a user