feat: added torrents and similar for every movie
This commit is contained in:
1 parent
ada725072f
commit
8bff4d6d80
16 files changed
+91
-117
No files matched your search
+5
-9
@@ -1,6 +1,8 @@
|
||||
import { Filters } from '@/app/(site)/useFilters';
|
||||
import client from '@/network';
|
||||
|
||||
import { ORDER } from '../api/torrents/parsers/pirate-bay';
|
||||
|
||||
const API = {
|
||||
genres(): Promise<IDName[]> {
|
||||
return client.get('/genres');
|
||||
@@ -31,17 +33,11 @@ const API = {
|
||||
});
|
||||
},
|
||||
torrents: {
|
||||
tracker({ query }: { query: string }): Promise<Torrent[]> {
|
||||
return client.get('/torrents/tracker', {
|
||||
params: {
|
||||
query
|
||||
}
|
||||
});
|
||||
},
|
||||
pirateBay({ query }: { query: string }): Promise<Torrent[]> {
|
||||
pirateBay({ query, order = ORDER.seeders }: { query: string; order?: ORDER }): Promise<Torrent[]> {
|
||||
return client.get('/torrents/pirate-bay', {
|
||||
params: {
|
||||
query
|
||||
query,
|
||||
order
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,10 +14,11 @@ const API = {
|
||||
}
|
||||
});
|
||||
},
|
||||
person({ id }: { id: string }): Promise<Person> {
|
||||
return client.get('/person', {
|
||||
similar({ page, movieId }: { page: string; movieId: string }): Promise<MovieDBResponse> {
|
||||
return client.get('/discover/similar', {
|
||||
params: {
|
||||
id
|
||||
page,
|
||||
movieId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,24 +2,32 @@
|
||||
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
|
||||
import usePerson from '@/app/(site)/top/usePerson';
|
||||
import List from '@/components/Movie/List';
|
||||
|
||||
import useTop from './useTop';
|
||||
|
||||
export default function Container() {
|
||||
const starring = useSearchParams().get('starring');
|
||||
const { data: person } = usePerson({ id: starring });
|
||||
const { data: top, hasNextPage, fetchNextPage } = useTop({ starring });
|
||||
const params = useSearchParams();
|
||||
|
||||
const starring = params.get('starring');
|
||||
const similar = params.get('similar');
|
||||
const title = params.get('title');
|
||||
|
||||
const { data: top, hasNextPage, fetchNextPage, isFetched, isFetching } = useTop({ starring, similar });
|
||||
|
||||
return (
|
||||
<>
|
||||
{Boolean(person) && (
|
||||
{Boolean(title && (starring || similar)) && (
|
||||
<span className='inline-flex w-full h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground mb-2'>
|
||||
Starring: {person?.name}
|
||||
{starring ? 'Starring' : 'Similar'}: {decodeURIComponent(title || '')}
|
||||
</span>
|
||||
)}
|
||||
<List pages={top?.pages || []} fetchNextPage={fetchNextPage} hasNextPage={hasNextPage} />
|
||||
{isFetched && !top?.pages?.[0]?.results?.length && (
|
||||
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
|
||||
Nothing was found
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import API from './api';
|
||||
|
||||
const usePerson = ({ id }: { id: string | null }) => {
|
||||
return useQuery({ enabled: Boolean(id), queryKey: ['person', id], queryFn: () => API.person({ id: id! }) });
|
||||
};
|
||||
|
||||
export default usePerson;
|
||||
@@ -1,31 +1,20 @@
|
||||
'use client';
|
||||
|
||||
import { useInfiniteQuery } from '@tanstack/react-query';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
|
||||
import API from './api';
|
||||
|
||||
const useTop = ({ starring }: { starring: string | null }) => {
|
||||
const page = useSearchParams().get('page');
|
||||
|
||||
const useTop = ({ starring, similar }: { starring: string | null; similar: string | null }) => {
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['top', starring],
|
||||
queryFn: ({ pageParam: page }) => API.top({ page, starring }),
|
||||
queryKey: ['top', starring, similar],
|
||||
queryFn: ({ pageParam: page }) => (similar ? API.similar({ page, movieId: similar }) : API.top({ page, starring })),
|
||||
suspense: true,
|
||||
getNextPageParam({ page, total_pages }) {
|
||||
if (page === total_pages) return undefined;
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
const url = new URL(location.href);
|
||||
|
||||
url.searchParams.set('page', page.toString());
|
||||
|
||||
window.history.pushState(null, '', url.href);
|
||||
}
|
||||
|
||||
return `${page + 1}`;
|
||||
},
|
||||
defaultPageParam: page || '1'
|
||||
defaultPageParam: '1'
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,29 +1,15 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { ORDER } from '../api/torrents/parsers/pirate-bay';
|
||||
|
||||
import API from './api';
|
||||
|
||||
const useTorrents = ({ query }: { query: string }) => {
|
||||
const { data: trackersTorrents, ...tracker } = useQuery({
|
||||
const useTorrents = ({ query, order }: { query: string; order: ORDER }) => {
|
||||
return useQuery({
|
||||
enabled: Boolean(query),
|
||||
queryKey: ['tracker', query],
|
||||
queryFn: () => API.torrents.tracker({ query })
|
||||
queryKey: ['pirate-bay', query, order],
|
||||
queryFn: () => API.torrents.pirateBay({ query, order })
|
||||
});
|
||||
|
||||
const { data: piratesTorrents, ...pirateBay } = useQuery({
|
||||
enabled: Boolean(query),
|
||||
queryKey: ['pirate-bay', query],
|
||||
queryFn: () => API.torrents.pirateBay({ query })
|
||||
});
|
||||
|
||||
return {
|
||||
torrents: useMemo(
|
||||
() => [...(trackersTorrents || []), ...(piratesTorrents || [])],
|
||||
[trackersTorrents, piratesTorrents]
|
||||
),
|
||||
isLoading: tracker.isLoading || pirateBay.isLoading,
|
||||
isFetched: tracker.isFetched && pirateBay.isFetched
|
||||
};
|
||||
};
|
||||
|
||||
export default useTorrents;
|
||||
@@ -4,9 +4,11 @@ import server from '@/network/server';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const params = Object.fromEntries(new URL(request.url).searchParams.entries());
|
||||
const response = await server.get(`/person/${params.id}`);
|
||||
const response = await server.get(`/movie/${params.movieId}/similar`, {
|
||||
params
|
||||
});
|
||||
|
||||
return NextResponse.json(response);
|
||||
}
|
||||
|
||||
export const revalidate = 3600 * 24;
|
||||
export const revalidate = 0;
|
||||
@@ -1,7 +1,7 @@
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { parse } from 'node-html-parser';
|
||||
|
||||
enum ORDER {
|
||||
export enum ORDER {
|
||||
size = 5,
|
||||
seeders = 7
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import pirateBay from '../parsers/pirate-bay';
|
||||
import pirateBay, { ORDER } from '../parsers/pirate-bay';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const params = Object.fromEntries(new URL(request.url).searchParams.entries());
|
||||
|
||||
try {
|
||||
const torrents = await pirateBay.search({ query: params.query });
|
||||
const torrents = await pirateBay.search({
|
||||
query: params.query,
|
||||
order: parseInt(params.order || `${ORDER.seeders}`)
|
||||
});
|
||||
return NextResponse.json(torrents);
|
||||
} catch (error) {
|
||||
return NextResponse.json([]);
|
||||
|
||||
Reference in new issue
Block a user