Skip to content

Commit

Permalink
Fix resolve url functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Dec 25, 2024
1 parent e23da56 commit 31a4669
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Channels sub count in web app
- Channel drag and drop in web app

### Removed

Expand Down
22 changes: 22 additions & 0 deletions playlet-lib/src/components/Services/Innertube/InnertubeService.bs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/CancellationUtils.bs"
import "pkg:/source/utils/ObjectUtils.bs"
import "pkg:/source/utils/TimeUtils.bs"
import "pkg:/components/Services/Innertube/ResolveUrl.bs"

namespace InnertubeService

Expand Down Expand Up @@ -239,6 +240,27 @@ namespace InnertubeService
return Innertube.ParsePlaylistPageResponse(json)
end function

function ResolveUrl(url as string, options = invalid as object) as object
cancellation = invalid

if options <> invalid
if options.DoesExist("cancellation")
cancellation = options.cancellation
end if
end if

request = Innertube.CreateResolveUrlRequest(url)
request.Cancellation(cancellation)

response = request.Await()
if not response.IsSuccess()
return invalid
end if

json = response.Json()
return json
end function

function AuthGetCode(cancellation = invalid as object) as object
result = Innertube.GetClientIdentity(cancellation)

Expand Down
20 changes: 20 additions & 0 deletions playlet-lib/src/components/Services/Innertube/ResolveUrl.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Innertube
function CreateResolveUrlRequest(url as string) as object
deviceInfo = CreateObject("roDeviceInfo")

payload = {
"context": CreateContext(ClientType.Web, deviceInfo, "")
"url": url
}

request = HttpClient.PostJson("https://www.youtube.com/youtubei/v1/navigation/resolve_url?prettyPrint=false&alt=json", payload)
headers = {
"accept": "*/*"
"accept-language": "*"
}
headers.Append(CreateHeaders(ClientType.Web))
request.Headers(headers)

return request
end function
end namespace
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,72 @@ namespace Http
return true
end function

@get("/playlet-invidious-backend/api/v1/resolveurl")
function ResolveUrl(context as object) as boolean
request = context.request
response = context.response

url = request.query.url
if StringUtils.IsNullOrEmpty(url)
response.Default(400, "Missing 'url' query parameter")
return true
end if

json = InnertubeService.ResolveUrl(url, response)
if json = invalid
response.Default(500, "Failed to resolve URL")
return true
end if

endpoint = json["endpoint"]
pageType = ValidString(ObjectUtils.Dig(endpoint, ["commandMetadata", "webCommandMetadata", "webPageType"]))
if pageType = "WEB_PAGE_TYPE_UNKNOWN" or pageType = ""
response.Default(400, "Unknown url")
return true
end if

subEndpoint = endpoint["watchEndpoint"]
if subEndpoint = invalid
subEndpoint = endpoint["browseEndpoint"]
if subEndpoint = invalid
subEndpoint = endpoint
end if
end if

params = ObjectUtils.Dig(subEndpoint, ["params"])

result = {
"pageType": pageType
}

browseId = ObjectUtils.Dig(subEndpoint, ["browseId"])
if not StringUtils.IsNullOrEmpty(browseId)
result["ucid"] = browseId
end if

videoId = ObjectUtils.Dig(subEndpoint, ["videoId"])
if not StringUtils.IsNullOrEmpty(videoId)
result["videoId"] = videoId
end if

playlistId = ObjectUtils.Dig(subEndpoint, ["playlistId"])
if not StringUtils.IsNullOrEmpty(playlistId)
result["playlistId"] = playlistId
end if

startTimeSeconds = ObjectUtils.Dig(subEndpoint, ["startTimeSeconds"])
if ValidInt(startTimeSeconds) > 0
result["startTimeSeconds"] = startTimeSeconds
end if

if IsString(params)
result["params"] = params
end if

response.Json(result)
return true
end function

@get("*")
function GetAll(context as object) as boolean
request = context.request
Expand Down

0 comments on commit 31a4669

Please sign in to comment.