From d9bfb3d3056d1c9f628c0c7d1b363ebf63977e87 Mon Sep 17 00:00:00 2001 From: diogo Date: Fri, 16 Jul 2021 23:32:48 +0200 Subject: [PATCH] playlist starts at the offset --- src/invidious/playlists.cr | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index f56cc2ea..6c745945 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -437,17 +437,38 @@ def get_playlist_videos(db, playlist, offset, locale = nil, continuation = nil) db.query_all("SELECT * FROM playlist_videos WHERE plid = $1 ORDER BY array_position($2, index) LIMIT 100 OFFSET $3", playlist.id, playlist.index, offset, as: PlaylistVideo) else - if offset >= 100 - # Normalize offset to match youtube's behavior (100 videos chunck per request) - offset = (offset / 100).to_i64 * 100_i64 + videos = [] of PlaylistVideo - ctoken = produce_playlist_continuation(playlist.id, offset) - initial_data = YoutubeAPI.browse(ctoken) - else - initial_data = YoutubeAPI.browse("VL" + playlist.id, params: "") + until videos.size >= 50 || videos.size == playlist.video_count + if offset >= 100 + # Normalize offset to match youtube's behavior (100 videos chunck per request) + normalized_offset = (offset / 100).to_i64 * 100_i64 + ctoken = produce_playlist_continuation(playlist.id, normalized_offset) + initial_data = request_youtube_api_browse(ctoken) + else + initial_data = request_youtube_api_browse("VL" + playlist.id, params: "") + end + + videos += extract_playlist_videos(initial_data) + + if continuation + until videos[0].id == continuation + videos.shift + end + elsif + until videos[0].index == offset + videos.shift + end + end + + if offset == 0 + offset = videos[0].index + end + + offset += 50 end - return extract_playlist_videos(initial_data) + return videos end end