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