Improved yt search
This commit is contained in:
@@ -304,4 +304,6 @@ export interface SynciousSaveProgressModel {
|
||||
time: number;
|
||||
}
|
||||
|
||||
export type SearchResults = (Channel | Video | Playlist | HashTag)[];
|
||||
export type SearchResults = (Channel | Video | Playlist | HashTag)[] & {
|
||||
getContinuation?: () => Promise<SearchResults>;
|
||||
};
|
||||
|
||||
@@ -3,23 +3,9 @@ import { convertToSeconds } from '$lib/time';
|
||||
import { getInnertube } from '.';
|
||||
import { searchSetDefaults } from '../misc';
|
||||
import type { Channel, SearchOptions, SearchResults, Thumbnail, Video } from '../model';
|
||||
import { YTNodes, type Types } from 'youtubei.js';
|
||||
|
||||
export async function getSearchYTjs(
|
||||
search: string,
|
||||
options: SearchOptions
|
||||
): Promise<SearchResults> {
|
||||
const innertube = await getInnertube();
|
||||
|
||||
searchSetDefaults(options);
|
||||
|
||||
const innerResults = await innertube.search(search, {
|
||||
sort_by: options.sort_by,
|
||||
duration: options.duration,
|
||||
features: [options.features] as Types.Feature[],
|
||||
upload_date: options.date
|
||||
});
|
||||
import { YTNodes, type Types, YT } from 'youtubei.js';
|
||||
|
||||
function invidiousSchema(innerResults: YT.Search): SearchResults {
|
||||
const searchResults: SearchResults = [];
|
||||
|
||||
innerResults.results.forEach((result) => {
|
||||
@@ -66,3 +52,33 @@ export async function getSearchYTjs(
|
||||
|
||||
return searchResults;
|
||||
}
|
||||
|
||||
function setGetContinuation(innerResults: YT.Search) {
|
||||
return async () => {
|
||||
const result = await innerResults.getContinuation();
|
||||
const searchResults = invidiousSchema(result);
|
||||
searchResults.getContinuation = setGetContinuation(result);
|
||||
return searchResults;
|
||||
};
|
||||
}
|
||||
|
||||
export async function getSearchYTjs(
|
||||
search: string,
|
||||
options: SearchOptions
|
||||
): Promise<SearchResults> {
|
||||
const innertube = await getInnertube();
|
||||
|
||||
searchSetDefaults(options);
|
||||
|
||||
const innerResults = await innertube.search(search, {
|
||||
sort_by: options.sort_by,
|
||||
duration: options.duration,
|
||||
features: [options.features] as Types.Feature[],
|
||||
upload_date: options.date
|
||||
});
|
||||
|
||||
const searchResults = invidiousSchema(innerResults);
|
||||
searchResults.getContinuation = setGetContinuation(innerResults);
|
||||
|
||||
return searchResults;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import type {
|
||||
Playlist,
|
||||
PlaylistPage,
|
||||
PlaylistPageVideo,
|
||||
SearchResults,
|
||||
Video,
|
||||
VideoBase,
|
||||
VideoPlay
|
||||
@@ -334,7 +335,7 @@ export const feedCacheStore: Writable<{
|
||||
[key: string]: (VideoBase | Video | PlaylistPageVideo)[];
|
||||
}> = writable({});
|
||||
export const searchCacheStore: Writable<{
|
||||
[searchTypeAndQuery: string]: (Channel | Video | Playlist | HashTag)[];
|
||||
[searchTypeAndQuery: string]: SearchResults;
|
||||
}> = writable({});
|
||||
export const feedLastItemId: Writable<string | undefined> = writable(undefined);
|
||||
export const playlistCacheStore: Writable<{
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import { _ } from '$lib/i18n';
|
||||
import InfiniteLoading, { type InfiniteEvent } from 'svelte-infinite-loading';
|
||||
import ItemsList from '$lib/components/ItemsList.svelte';
|
||||
import type { SearchOptions } from '$lib/api/model.js';
|
||||
import type { SearchOptions, SearchResults } from '$lib/api/model.js';
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
@@ -69,18 +69,28 @@
|
||||
}
|
||||
|
||||
async function loadMore(event: InfiniteEvent) {
|
||||
currentPage++;
|
||||
searchOptions = {
|
||||
...searchOptions,
|
||||
page: currentPage.toString()
|
||||
};
|
||||
const newSearch = await getSearch(data.slug, searchOptions);
|
||||
let newSearch: SearchResults;
|
||||
|
||||
const searchCacheItem = $searchCacheStore[data.searchStoreId];
|
||||
|
||||
if (searchCacheItem.getContinuation) {
|
||||
newSearch = await searchCacheItem.getContinuation();
|
||||
searchCacheItem.getContinuation = newSearch.getContinuation;
|
||||
} else {
|
||||
currentPage++;
|
||||
searchOptions = {
|
||||
...searchOptions,
|
||||
page: currentPage.toString()
|
||||
};
|
||||
|
||||
newSearch = await getSearch(data.slug, searchOptions);
|
||||
}
|
||||
|
||||
if (newSearch.length === 0) {
|
||||
event.detail.complete();
|
||||
} else {
|
||||
searchCacheStore.set({
|
||||
[data.searchStoreId]: [...($searchCacheStore[data.searchStoreId] ?? []), ...newSearch]
|
||||
[data.searchStoreId]: [...(searchCacheItem ?? []), ...newSearch]
|
||||
});
|
||||
event.detail.loaded();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user