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
|
||||
Reference in new issue
Block a user