Skip to content

Commit

Permalink
Handle channel URL resolve redirects in the local API (FreeTubeApp#4205)
Browse files Browse the repository at this point in the history
* Handle channel URL resolve redirects in the local API

* Refactor into a for-loop
  • Loading branch information
absidue authored and ab-shrek committed Nov 2, 2023
1 parent 2c9291c commit 70de805
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/renderer/helpers/api/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,24 @@ export async function getLocalChannelId(url) {
try {
const innertube = await createInnertube()

// resolveURL throws an error if the URL doesn't exist
const navigationEndpoint = await innertube.resolveURL(url)

if (navigationEndpoint.metadata.page_type === 'WEB_PAGE_TYPE_CHANNEL') {
return navigationEndpoint.payload.browseId
} else {
return null
// Resolve URL and allow 1 redirect, as YouTube should just do 1
// We want to avoid an endless loop
for (let i = 0; i < 2; i++) {
// resolveURL throws an error if the URL doesn't exist
const navigationEndpoint = await innertube.resolveURL(url)

if (navigationEndpoint.metadata.page_type === 'WEB_PAGE_TYPE_CHANNEL') {
return navigationEndpoint.payload.browseId
} else if (navigationEndpoint.metadata.page_type === 'WEB_PAGE_TYPE_UNKNOWN' && navigationEndpoint.payload.url?.startsWith('https://www.youtube.com/')) {
// handle redirects like https://www.youtube.com/@wanderbots, which resolves to https://www.youtube.com/Wanderbots, which we need to resolve again
url = navigationEndpoint.payload.url
} else {
return null
}
}
} catch {
return null
}
} catch { }

return null
}

/**
Expand Down

0 comments on commit 70de805

Please sign in to comment.