Merge pull request #9 from ztpn/uam

Support for new nsbx api
This commit is contained in:
TPN 2024-07-06 19:49:01 +01:00 committed by GitHub
commit 3341c6d75c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 33 additions and 26 deletions

View file

@ -39,6 +39,7 @@ export function makeFetcher(fetcher: Fetcher): UseableFetcher {
baseUrl: ops?.baseUrl ?? '',
readHeaders: ops?.readHeaders ?? [],
body: ops?.body,
credentials: ops?.credentials,
});
};
const output: UseableFetcher = async (url, ops) => (await newFetcher(url, ops)).body;

View file

@ -7,6 +7,7 @@ export type FetchOps = {
headers: Record<string, string>;
method: string;
body: any;
credentials?: 'include' | 'same-origin' | 'omit';
};
export type FetchHeaders = {

View file

@ -28,6 +28,7 @@ export function makeStandardFetcher(f: FetchLike): Fetcher {
...ops.headers,
},
body: seralizedBody.body,
credentials: ops.credentials,
});
let body: any;

View file

@ -7,6 +7,7 @@ export type FetcherOptions = {
method?: 'HEAD' | 'GET' | 'POST';
readHeaders?: string[];
body?: Record<string, any> | string | FormData | URLSearchParams;
credentials?: 'include' | 'same-origin' | 'omit';
};
// Version of the options that always has the defaults set
@ -18,6 +19,7 @@ export type DefaultedFetcherOptions = {
query: Record<string, string>;
readHeaders: string[];
method: 'HEAD' | 'GET' | 'POST';
credentials?: 'include' | 'same-origin' | 'omit';
};
export type FetcherResponse<T = any> = {

View file

@ -1,5 +1,4 @@
import { EmbedOutput, makeEmbed } from '@/providers/base';
import { baseUrl } from '@/providers/sources/nsbx';
import { NotFoundError } from '@/utils/errors';
const providers = [
@ -20,21 +19,30 @@ function embed(provider: { id: string; rank: number }) {
rank: provider.rank,
disabled: false,
async scrape(ctx) {
const search = await ctx.proxiedFetcher.full(
`${baseUrl}/search?query=${encodeURIComponent(ctx.url)}&provider=${provider.id}`,
);
const [query, baseUrl] = ctx.url.split('.');
if (search.statusCode === 429) {
throw new Error('Rate limited');
} else if (search.statusCode !== 200) {
throw new NotFoundError('Failed to search');
}
const search = await ctx.fetcher.full('/search', {
query: {
query: encodeURIComponent(query),
provider: provider.id,
},
credentials: 'include',
baseUrl,
});
if (search.statusCode === 429) throw new Error('Rate limited');
if (search.statusCode !== 200) throw new NotFoundError('Failed to search');
ctx.progress(50);
const result = await ctx.proxiedFetcher(
`${baseUrl}/provider?resourceId=${encodeURIComponent(search.body.url)}&provider=${provider.id}`,
);
const result = await ctx.fetcher('/provider', {
query: {
resourceId: encodeURIComponent(search.body.url),
provider: provider.id,
},
credentials: 'include',
baseUrl,
});
ctx.progress(100);

View file

@ -3,8 +3,6 @@ import { SourcererOutput, makeSourcerer } from '@/providers/base';
import { MovieScrapeContext, ShowScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
export const baseUrl = 'https://api.nsbx.ru';
async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promise<SourcererOutput> {
const query = {
title: ctx.media.title,
@ -12,25 +10,21 @@ async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promis
tmdbId: ctx.media.tmdbId,
imdbId: ctx.media.imdbId,
type: ctx.media.type,
season: '',
episode: '',
...(ctx.media.type === 'show' && {
season: ctx.media.season.number.toString(),
episode: ctx.media.episode.number.toString(),
}),
};
if (ctx.media.type === 'show') {
query.season = ctx.media.season.number.toString();
query.episode = ctx.media.episode.number.toString();
}
const res: { providers: string[]; endpoint: string } = await ctx.fetcher('https://api.nsbx.ru/status');
const res = await ctx.proxiedFetcher(`${baseUrl}/status`);
if (res.providers?.length === 0) {
throw new NotFoundError('No providers available');
}
if (res.providers?.length === 0) throw new NotFoundError('No providers available');
if (!res.endpoint) throw new Error('No endpoint returned');
const embeds = res.providers.map((provider: string) => {
return {
embedId: provider,
url: JSON.stringify(query),
url: `${JSON.stringify(query)}.${res.endpoint}`,
};
});