Improve video queue caching
This commit is contained in:
1 parent
87e50e713f
commit
71f9820ed6
4 files changed
+75
-47
No files matched your search
@@ -43,14 +43,11 @@ namespace Invidious
|
||||
m.node = node
|
||||
end function
|
||||
|
||||
function DefaultQueryParams() as object
|
||||
if m._defaultQueryParams = invalid
|
||||
m._defaultQueryParams = {
|
||||
"region": m.node.applicationInfo@.GetUserCountryCode()
|
||||
}
|
||||
function GetUserRegion() as string
|
||||
if m._userRegion = invalid
|
||||
m._userRegion = m.node.applicationInfo@.GetUserCountryCode()
|
||||
end if
|
||||
|
||||
return m._defaultQueryParams
|
||||
return m._userRegion
|
||||
end function
|
||||
|
||||
function GetAuthenticationHeaders(token as string) as object
|
||||
@@ -89,7 +86,7 @@ namespace Invidious
|
||||
url = `${instance}${Invidious.SEARCH_SUGGEST_ENDPOINT}`
|
||||
|
||||
request = HttpClient.Get(url)
|
||||
request.QueryParams(m.DefaultQueryParams())
|
||||
request.QueryParam("region", m.GetUserRegion())
|
||||
request.QueryParam("q", q)
|
||||
request.CacheSeconds(60 * 60 * 24)
|
||||
|
||||
@@ -99,33 +96,59 @@ namespace Invidious
|
||||
return response.Json()
|
||||
end function
|
||||
|
||||
function GetVideoMetadata(videoId as string, cancellation = invalid as object) as object
|
||||
instance = m.GetInstance()
|
||||
url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`
|
||||
|
||||
request = HttpClient.Get(url)
|
||||
request.QueryParams(m.DefaultQueryParams())
|
||||
function GetVideoMetadata(videoId as string, options = invalid as object) as object
|
||||
region = true
|
||||
' 5 hours cache. Videos should expire in 6 hours, but since we don't
|
||||
' have the exact expiration time, we'll use 5 hours. Invidious currently
|
||||
' Does a fresh fetch if the cache is older than 10 minutes.
|
||||
' https://github.com/iv-org/invidious/blob/069e91d2a609b1df94bdfbb693335969b2a281d8/src/invidious/videos.cr#L349
|
||||
request.CacheSeconds(60 * 60 * 5)
|
||||
cacheSeconds = 18000 ' 5 hours
|
||||
cancellation = invalid
|
||||
tryCount = 2
|
||||
|
||||
if options <> invalid
|
||||
if options.DoesExist("region")
|
||||
region = options.region
|
||||
end if
|
||||
if options.DoesExist("cacheSeconds")
|
||||
cacheSeconds = options.cacheSeconds
|
||||
end if
|
||||
if options.DoesExist("cancellation")
|
||||
cancellation = options.cancellation
|
||||
end if
|
||||
if options.DoesExist("tryCount")
|
||||
tryCount = options.tryCount
|
||||
end if
|
||||
end if
|
||||
|
||||
instance = m.GetInstance()
|
||||
url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`
|
||||
|
||||
request = HttpClient.Get(url)
|
||||
|
||||
if region = true
|
||||
request.QueryParam("region", m.GetUserRegion())
|
||||
end if
|
||||
|
||||
request.CacheSeconds(cacheSeconds)
|
||||
request.Cancellation(cancellation)
|
||||
|
||||
request.TryCount(2)
|
||||
request.RetryCallback(function(context as object) as boolean
|
||||
response = context.response
|
||||
' In some cases, video info requests return "HTTP/0.9 when not allowed" errors.
|
||||
' Retrying should usually fix the issue.
|
||||
if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL
|
||||
errorMessage = response.ErrorMessage()
|
||||
if errorMessage.InStr("HTTP/0.9") <> -1
|
||||
return true
|
||||
if tryCount > 1
|
||||
request.TryCount(tryCount)
|
||||
request.RetryCallback(function(context as object) as boolean
|
||||
response = context.response
|
||||
' In some cases, video info requests return "HTTP/0.9 when not allowed" errors.
|
||||
' Retrying should usually fix the issue.
|
||||
if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL
|
||||
errorMessage = response.ErrorMessage()
|
||||
if errorMessage.InStr("HTTP/0.9") <> -1
|
||||
return true
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
|
||||
return false
|
||||
end function)
|
||||
return false
|
||||
end function)
|
||||
end if
|
||||
|
||||
return request.Await()
|
||||
end function
|
||||
@@ -142,7 +165,6 @@ namespace Invidious
|
||||
request.Headers(m.GetAuthenticationHeaders(authToken.token))
|
||||
end if
|
||||
|
||||
request.QueryParams(m.DefaultQueryParams())
|
||||
request.QueryParam("index", index)
|
||||
request.Cancellation(cancellation)
|
||||
|
||||
@@ -154,7 +176,6 @@ namespace Invidious
|
||||
url = `${instance}${Invidious.CHANNELS_ENDPOINT}/${authorId}`
|
||||
|
||||
request = HttpClient.Get(url)
|
||||
request.QueryParams(m.DefaultQueryParams())
|
||||
request.Cancellation(cancellation)
|
||||
request.CacheSeconds(60 * 60 * 24)' 24 hours
|
||||
request.TryCount(3)
|
||||
|
||||
@@ -17,7 +17,7 @@ function VideoContentTask(input as object) as object
|
||||
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
|
||||
response = service.GetVideoMetadata(contentNode.videoId, m.top.cancellation)
|
||||
response = service.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation })
|
||||
|
||||
if m.top.cancel
|
||||
return invalid
|
||||
|
||||
@@ -13,11 +13,22 @@ function VideoQueueViewContentTask(input as object) as object
|
||||
|
||||
index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1)
|
||||
|
||||
requestOptions = {
|
||||
' Not providing a region should lead to a better DB hit rate
|
||||
' When the region is provided, the video will be fetched fresh every time
|
||||
' and will not be saved to the database.
|
||||
region: false
|
||||
cacheSeconds: 8640000 ' 100 days
|
||||
cancellation: cancellation
|
||||
tryCount: 1
|
||||
}
|
||||
|
||||
for i = index to videoNodes.Count() - 1
|
||||
videoNode = videoNodes[i]
|
||||
LoadVideoDetail(videoNode, service, instance, cancellation)
|
||||
' Sleep for a bit to avoid creating too much traffic
|
||||
sleep(500)
|
||||
if not LoadVideoDetail(videoNode, service, instance, requestOptions)
|
||||
' Sleep for a bit to avoid creating too much traffic
|
||||
sleep(500)
|
||||
end if
|
||||
|
||||
if m.top.cancel = true
|
||||
return invalid
|
||||
@@ -26,9 +37,10 @@ function VideoQueueViewContentTask(input as object) as object
|
||||
|
||||
for i = index - 1 to 0 step -1
|
||||
videoNode = videoNodes[i]
|
||||
LoadVideoDetail(videoNode, service, instance, cancellation)
|
||||
' Sleep for a bit to avoid creating too much traffic
|
||||
sleep(1000)
|
||||
if not LoadVideoDetail(videoNode, service, instance, requestOptions)
|
||||
' Sleep for a bit to avoid creating too much traffic
|
||||
sleep(1000)
|
||||
end if
|
||||
|
||||
if m.top.cancel = true
|
||||
return invalid
|
||||
@@ -38,17 +50,10 @@ function VideoQueueViewContentTask(input as object) as object
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function LoadVideoDetail(videoNode as object, service as object, instance as string, cancellation as object) as void
|
||||
' Returns true if the video was loaded from cache.
|
||||
function LoadVideoDetail(videoNode as object, service as object, instance as string, requestOptions as object) as boolean
|
||||
videoId = videoNode.videoId
|
||||
url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`
|
||||
|
||||
request = HttpClient.Get(url)
|
||||
request.QueryParams(service.DefaultQueryParams())
|
||||
' A large cache interval, since we don't care about the video detail accuracy
|
||||
request.CacheSeconds(8640000)
|
||||
request.Cancellation(cancellation)
|
||||
|
||||
response = request.Await()
|
||||
response = service.GetVideoMetadata(videoId, requestOptions)
|
||||
metadata = response.Json()
|
||||
|
||||
if not response.IsSuccess() or metadata = invalid
|
||||
@@ -58,8 +63,9 @@ function LoadVideoDetail(videoNode as object, service as object, instance as str
|
||||
' We set the title to make sure we don't try to load it again
|
||||
videoNode.title = "Video not available"
|
||||
end if
|
||||
return
|
||||
return false
|
||||
end if
|
||||
|
||||
InvidiousContent.ToVideoContentNode(videoNode, metadata, instance)
|
||||
return response.IsCached()
|
||||
end function
|
||||
Reference in new issue
Block a user