Innertube continuations (#522)

* Search continuations

* add another type of playlists

* Continuation for search and channels working

* Playlist continuation

* Fix pagination - what a fucking mess I've created

* web app fix

* Changelog
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2024-12-25 14:07:48 -05:00
1 parent f6e23ad7d9
commit e23da56b37
15 files changed
+293 -96

No files matched your search

+28 -12
View File
@@ -28,7 +28,8 @@ export class InvidiousApi {
return await this.cachedFetch(url, 60 * 60 * 24);
}
public async search(query: string, filters: any, page: number = 1) {
// need to change this
public async search(query: string, filters: any, page: number, continuation: string | undefined) {
// TODO:P2 use a FeedSource similar to brightscript version
let url = `${this.instance}/api/v1/search?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`;
for (let filter in filters) {
@@ -44,9 +45,18 @@ export class InvidiousApi {
url += `&${filter}=${filters[filter].join(',')}`;
}
}
url += `&page=${page}`;
if (continuation) {
url += `&continuation=${encodeURIComponent(continuation)}`;
} else {
url += `&page=${page}`;
}
const response = await fetch(url);
return await response.json();
let json = await response.json();
if (Array.isArray(json)) {
json = { items: json };
}
return json;
}
public async getVideoMetadata(videoId: string) {
@@ -89,19 +99,20 @@ export class InvidiousApi {
feedSourceState.paginationType = endpoint.paginationType
if (feedSourceState.paginationType === "Pages") {
if (!Number.isInteger(feedSourceState.page)) {
feedSourceState.page = 0;
}
feedSourceState.page += 1;
feedSourceState.queryParams.page = feedSourceState.page;
} else if (feedSourceState.paginationType === "Continuation") {
const continuation = feedSourceState.continuation;
const continuation = feedSourceState.continuation;
if (feedSourceState.paginationType === "Continuation" || continuation) {
if (continuation) {
feedSourceState.queryParams.continuation = continuation;
} else {
delete feedSourceState.queryParams.continuation;
}
delete feedSourceState.queryParams.page;
} else if (feedSourceState.paginationType === "Pages") {
if (!Number.isInteger(feedSourceState.page)) {
feedSourceState.page = 0;
}
feedSourceState.page += 1;
feedSourceState.queryParams.page = feedSourceState.page;
}
}
@@ -188,12 +199,17 @@ export class InvidiousApi {
}
private async DefaultHandler(feedSource, responseJson) {
return { items: responseJson };
// if is array
if (Array.isArray(responseJson)) {
responseJson = { items: responseJson };
}
return responseJson;
}
private async PlaylistHandler(feedSource, responseJson) {
return {
items: responseJson.videos,
continuation: responseJson.continuation
};
}
@@ -21,6 +21,7 @@
let searchBoxText = "";
let suggestions: { suggestions: any[] } = { suggestions: [] };
let page = 1;
let searchContinuation = "";
let videos = [];
let searchHistory = [];
let isLoading = false;
@@ -85,11 +86,12 @@
async function suggestionClicked(query) {
searchBoxText = query;
page = 1;
searchContinuation = "";
videos = [];
await searchVideos();
await searchVideos("");
}
async function searchVideos() {
async function searchVideos(continuation: string | undefined) {
suggestions = { suggestions: [] };
if (searchBoxText.length === 0) {
return;
@@ -97,12 +99,14 @@
try {
isLoading = true;
const newVideos = await invidiousApi.search(
const response = await invidiousApi.search(
searchBoxText,
searchFilters,
page
page,
continuation
);
videos = [...videos, ...newVideos];
videos = [...videos, ...response.items];
searchContinuation = response.continuation || "";
} finally {
isLoading = false;
}
@@ -122,7 +126,7 @@
e.preventDefault();
page = 1;
videos = [];
await searchVideos();
await searchVideos("");
}}
>
<div class="join w-full border border-neutral rounded-full">
@@ -148,7 +152,7 @@
on:click={async () => {
page = 1;
videos = [];
await searchVideos();
await searchVideos("");
}}
>
<div class="h-6">
@@ -206,7 +210,7 @@
class="btn w-1/2"
on:click={async () => {
page++;
await searchVideos();
await searchVideos(searchContinuation);
}}
>
{$tr("Load more")}
@@ -151,6 +151,7 @@
}
invidiousApi.markFeedSourcePagination(feedSource);
let hadContinuation = !!feedSource.state.continuation;
try {
const result = await invidiousApi.makeRequest(feedSource);
@@ -163,14 +164,14 @@
continue;
}
const hasContinuation = !!result.continuation;
if (hasContinuation) {
feedSource.state.continuation = result.continuation;
}
let hasContinuation = !!result.continuation;
feedSource.state.continuation = result.continuation || "";
if (result.items.length > 0) {
const paginationType = feedSource.state.paginationType;
if (paginationType === "Continuation" && hasContinuation) {
if (hadContinuation && !hasContinuation) {
feedSource.state.loadState = FeedLoadState.Loaded;
} else if (paginationType === "Continuation" && hasContinuation) {
feedSource.state.loadState = FeedLoadState.LoadedPage;
} else if (paginationType === "Pages") {
feedSource.state.loadState = FeedLoadState.LoadedPage;