Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Innertube#resolveURL(url) #268

Merged
merged 3 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const yt = await Innertube.create({
* [.getPlaylist(id)](#getplaylist)
* [.getStreamingData(video_id, options)](#getstreamingdata)
* [.download(video_id, options?)](#download)
* [.resolveURL(url)](#resolveurl)
* [.call(endpoint, args?)](#call)

</p>
Expand Down Expand Up @@ -563,6 +564,16 @@ Downloads a given video.

See [`./examples/download`](https://github.com/LuanRT/YouTube.js/blob/main/examples/download) for examples.

<a name="resolveurl"></a>
### resolveURL(url)
Resolves a given url.

**Returns**: `Promise.<NavigationEndpoint>`

| Param | Type | Description |
| --- | --- | --- |
| url | `string` | Url to resolve |

<a name="call"></a>
### call(endpoint, args?)
Utility to call navigation endpoints.
Expand Down
9 changes: 9 additions & 0 deletions src/Innertube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ class Innertube {
return info.download(options);
}

/**
* Resolves the given URL.
* @param url - The URL.
*/
async resolveURL(url: string): Promise<NavigationEndpoint> {
const response = await this.actions.execute('/navigation/resolve_url', { url, parse: true });
return response.endpoint as NavigationEndpoint;
}

/**
* Utility method to call an endpoint without having to use {@link Actions}.
* @param endpoint -The endpoint to call.
Expand Down
1 change: 1 addition & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export default class Parser {
hls_manifest_url: data.streamingData?.hlsManifestUrl as string || null
} : undefined,
current_video_endpoint: data.currentVideoEndpoint ? new NavigationEndpoint(data.currentVideoEndpoint) : null,
endpoint: data.endpoint ? new NavigationEndpoint(data.endpoint) : null,
captions: Parser.parseItem<PlayerCaptionsTracklist>(data.captions),
video_details: data.videoDetails ? new VideoDetails(data.videoDetails) : undefined,
annotations: Parser.parseArray<PlayerAnnotationsExpanded>(data.annotations),
Expand Down
2 changes: 1 addition & 1 deletion test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const VIDEOS = [

export const CHANNELS = [
{
ID: 'UC_x5XG1OV2P6uZZ5FSM9Ttw',
ID: 'UCXuqSBlHAE6Xw-yeJA0Tunw',
NAME: 'Linus Tech Tips'
}
];
5 changes: 5 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ describe('YouTube.js Tests', () => {
});

describe('General', () => {
it('should resolve a URL', async () => {
const url = await yt.resolveURL('https://www.youtube.com/@linustechtips');
expect(url.payload.browseId).toBe(CHANNELS[0].ID);
});

it('should retrieve playlist', async () => {
const playlist = await yt.getPlaylist('PLLw0AzOz95FU7w2juhPECP9NyGhbZmz_t');
expect(playlist.items.length).toBeLessThanOrEqual(100);
Expand Down