Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
fix playlist continuations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzeng0 committed May 10, 2023
1 parent 7eee08f commit a2f0003
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
39 changes: 32 additions & 7 deletions proto/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,48 @@ const {
search_sort, search_filters, search, search_continuation, search_options
} = require('./build/youtube_pb');

function bin_b64(binary){
return Buffer.from(binary).toString('base64').replaceAll('+', '-').replaceAll('/', '_');
function binary_to_b64_no_pad(binary){
return Buffer.from(binary).toString('base64url');
}

function binary_to_b64_no_pad(binary){
var str = bin_b64(binary),
index = str.indexOf('=');
if(index != -1)
str = str.substring(0, index);
function bin_b64(binary){
var str = binary_to_b64_no_pad(binary);

while(str.length & 3)
str += '=';
return str;
}

function binary_to_b64url(binary){
return encodeURIComponent(bin_b64(binary));
}

function b64url_to_binary(input){
return Buffer.from(decodeURIComponent(input), 'base64');
}

module.exports = {
playlist_next_offset(continuation){
var p = playlist.deserializeBinary(b64url_to_binary(continuation));
var cont = p.getContinuation();

if(!cont)
return undefined;
var params = cont.getParams();

if(!params)
return undefined;
params = playlist_params.deserializeBinary(b64url_to_binary(params));

var offset = params.getOffset();

if(!offset)
return undefined;
var p_offset = playlist_offset.deserializeBinary(b64url_to_binary(offset.substring('PT:'.length)));

return p_offset.getOffset();
},

gen_playlist_continuation(id, offset){
var p_offset = new playlist_offset(), p_params = new playlist_params(),
p_cont = new playlist.playlist_continuation(), p = new playlist();
Expand Down
4 changes: 2 additions & 2 deletions src/api/Youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Request = require('../Request');
const SourceError = require('../SourceError');

const {Track, TrackImage, TrackResults, TrackPlaylist, TrackStream, TrackStreams} = require('../Track');
const {gen_playlist_continuation, gen_search_options} = require('../../proto/youtube');
const {gen_playlist_continuation, gen_search_options, playlist_next_offset} = require('../../proto/youtube');

function get_property(array, prop){
if(!(array instanceof Array))
Expand Down Expand Up @@ -174,7 +174,7 @@ class YoutubePlaylist extends TrackPlaylist{

for(var item of data){
if(item.continuationItemRenderer)
this.next_offset = offset + this.length;
this.next_offset = playlist_next_offset(item.continuationItemRenderer.continuationEndpoint.continuationCommand.token);
else if(item.playlistVideoRenderer)
this.push(new YoutubeTrack().from_playlist(item.playlistVideoRenderer));
}
Expand Down

0 comments on commit a2f0003

Please sign in to comment.