From 8bff4d6d80f4558de4578dc55354fc5614c5003a Mon Sep 17 00:00:00 2001 From: ner Date: Fri, 18 Aug 2023 12:36:10 +0300 Subject: [PATCH] feat: added torrents and similar for every movie --- app/(site)/api.ts | 14 ++---- app/(site)/top/api.ts | 7 +-- app/(site)/top/container.tsx | 20 +++++--- app/(site)/top/usePerson.ts | 9 ---- app/(site)/top/useTop.ts | 19 ++----- app/(site)/useTorrents.ts | 26 +++------- app/api/{person => discover/similar}/route.ts | 6 ++- app/api/torrents/parsers/pirate-bay.ts | 2 +- app/api/torrents/pirate-bay/route.ts | 7 ++- components/Movie/List.tsx | 2 +- components/Movie/Preview.tsx | 28 +++++++++-- components/Movie/components/Credits/index.tsx | 2 +- components/Movie/components/Poster/index.tsx | 5 +- .../Movie/components/Torrents/index.tsx | 49 ++++++------------- components/Search/index.tsx | 7 --- types.d.ts | 5 +- 16 files changed, 91 insertions(+), 117 deletions(-) delete mode 100644 app/(site)/top/usePerson.ts rename app/api/{person => discover/similar}/route.ts (68%) delete mode 100644 components/Search/index.tsx diff --git a/app/(site)/api.ts b/app/(site)/api.ts index cba54c3..6f9ab74 100644 --- a/app/(site)/api.ts +++ b/app/(site)/api.ts @@ -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 { return client.get('/genres'); @@ -31,17 +33,11 @@ const API = { }); }, torrents: { - tracker({ query }: { query: string }): Promise { - return client.get('/torrents/tracker', { - params: { - query - } - }); - }, - pirateBay({ query }: { query: string }): Promise { + pirateBay({ query, order = ORDER.seeders }: { query: string; order?: ORDER }): Promise { return client.get('/torrents/pirate-bay', { params: { - query + query, + order } }); } diff --git a/app/(site)/top/api.ts b/app/(site)/top/api.ts index ffd441d..1a60fb9 100644 --- a/app/(site)/top/api.ts +++ b/app/(site)/top/api.ts @@ -14,10 +14,11 @@ const API = { } }); }, - person({ id }: { id: string }): Promise { - return client.get('/person', { + similar({ page, movieId }: { page: string; movieId: string }): Promise { + return client.get('/discover/similar', { params: { - id + page, + movieId } }); } diff --git a/app/(site)/top/container.tsx b/app/(site)/top/container.tsx index 3270470..9f010e2 100644 --- a/app/(site)/top/container.tsx +++ b/app/(site)/top/container.tsx @@ -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)) && ( - Starring: {person?.name} + {starring ? 'Starring' : 'Similar'}: {decodeURIComponent(title || '')} )} + {isFetched && !top?.pages?.[0]?.results?.length && ( +
+ Nothing was found +
+ )} ); } diff --git a/app/(site)/top/usePerson.ts b/app/(site)/top/usePerson.ts deleted file mode 100644 index 31a36df..0000000 --- a/app/(site)/top/usePerson.ts +++ /dev/null @@ -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; diff --git a/app/(site)/top/useTop.ts b/app/(site)/top/useTop.ts index ef25d2c..bc015e6 100644 --- a/app/(site)/top/useTop.ts +++ b/app/(site)/top/useTop.ts @@ -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' }); }; diff --git a/app/(site)/useTorrents.ts b/app/(site)/useTorrents.ts index 313343d..82c4ab8 100644 --- a/app/(site)/useTorrents.ts +++ b/app/(site)/useTorrents.ts @@ -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; diff --git a/app/api/person/route.ts b/app/api/discover/similar/route.ts similarity index 68% rename from app/api/person/route.ts rename to app/api/discover/similar/route.ts index 526efcf..53927a6 100644 --- a/app/api/person/route.ts +++ b/app/api/discover/similar/route.ts @@ -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; diff --git a/app/api/torrents/parsers/pirate-bay.ts b/app/api/torrents/parsers/pirate-bay.ts index d264363..8fa227f 100644 --- a/app/api/torrents/parsers/pirate-bay.ts +++ b/app/api/torrents/parsers/pirate-bay.ts @@ -1,7 +1,7 @@ import axios, { AxiosInstance } from 'axios'; import { parse } from 'node-html-parser'; -enum ORDER { +export enum ORDER { size = 5, seeders = 7 } diff --git a/app/api/torrents/pirate-bay/route.ts b/app/api/torrents/pirate-bay/route.ts index e6094de..673a6be 100644 --- a/app/api/torrents/pirate-bay/route.ts +++ b/app/api/torrents/pirate-bay/route.ts @@ -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([]); diff --git a/components/Movie/List.tsx b/components/Movie/List.tsx index 8a428a6..23057e1 100644 --- a/components/Movie/List.tsx +++ b/components/Movie/List.tsx @@ -61,7 +61,7 @@ const List: FC = ({ pages, hasNextPage, fetchNextPage }) => { key={movie.id} movie={movie} className='border-none' - onPersonClick={() => { + onClose={() => { setMovie(null); window.scrollTo({ top: 0, behavior: 'smooth' }); }} diff --git a/components/Movie/Preview.tsx b/components/Movie/Preview.tsx index fc75727..cb3c852 100644 --- a/components/Movie/Preview.tsx +++ b/components/Movie/Preview.tsx @@ -1,5 +1,6 @@ import { FC, useState } from 'react'; import { Play } from 'lucide-react'; +import { useRouter } from 'next/navigation'; import useTrailer from '@/app/(site)/useTrailer'; import Credits from '@/components/Movie/components/Credits'; @@ -9,15 +10,21 @@ import Title from '@/components/Movie/components/Title'; import Trailer from '@/components/Movie/components/Trailer'; import { Separator } from '@/components/ui/separator'; +import { Button } from '../ui/button'; + +import Torrents from './components/Torrents'; + interface Props { movie?: Movie; className?: string; - onPersonClick?: (id: string) => void; + onClose?: () => void; } -const Preview: FC = ({ movie, className, onPersonClick }) => { +const Preview: FC = ({ movie, className, onClose }) => { const [isShowTrailer, setIsShowTrailer] = useState(false); + const [isShowTorrents, setIsShowTorrents] = useState(false); const { data: trailer } = useTrailer({ movieId: movie?.id }); + const router = useRouter(); if (!movie) return null; @@ -59,11 +66,26 @@ const Preview: FC = ({ movie, className, onPersonClick }) => {

{movie.overview}

+
+ + +
- + onClose?.()} /> {isShowTrailer && setIsShowTrailer(false)} />} + {isShowTorrents && setIsShowTorrents(false)} />} ); }; diff --git a/components/Movie/components/Credits/index.tsx b/components/Movie/components/Credits/index.tsx index 0b8150c..d90d327 100644 --- a/components/Movie/components/Credits/index.tsx +++ b/components/Movie/components/Credits/index.tsx @@ -23,7 +23,7 @@ const Credits: FC = ({ movieId, onPersonClick }) => { key={`${actor.id}_${actor.cast_id}`} title={actor.name} onClick={() => { - router.push(`/top?starring=${actor.id}`); + router.push(`/top?starring=${actor.id}&title=${encodeURIComponent(actor.name)}`); onPersonClick?.(actor.id.toString()); }} className='p-2 rounded-lg border bg-card text-card-foreground self-end max-w-[150px] w-full shrink-0 cursor-pointer transition-shadow hover:shadow-lg' diff --git a/components/Movie/components/Poster/index.tsx b/components/Movie/components/Poster/index.tsx index 14b3979..c29b4b6 100644 --- a/components/Movie/components/Poster/index.tsx +++ b/components/Movie/components/Poster/index.tsx @@ -14,7 +14,10 @@ interface Props { const Poster: FC = ({ title, poster, width, height, size = 250, className, children }) => { return ( -
+
{poster ? ( onChange((sorting) => ({ - ...sorting, - by, - direction: sorting.by !== by ? 'desc' : sorting.direction === 'asc' ? 'desc' : 'asc' + by: sorting.by === ORDER.seeders ? ORDER.size : ORDER.seeders })) } className='cursor-pointer select-none' >
{title} - {value.by === by && - (value.direction === 'desc' ? ( - - ) : ( - - ))} - {value.by !== by && } + {value.by === by ? : }
); }; const Torrents: FC = ({ title, onClose }) => { - const { torrents, isLoading, isFetched } = useTorrents({ query: title }); - const [sorting, setSorting] = useState({ by: 'seeders', direction: 'desc' }); - - const sortedTorrents = useMemo(() => { - return [...torrents].sort((a, b) => { - if (sorting.by === 'size') - return sorting.direction === 'asc' ? compareSizes(a.size, b.size) : compareSizes(b.size, a.size); - - return sorting.direction === 'asc' ? a.seeders - b.seeders : b.seeders - a.seeders; - }); - }, [torrents, sorting]); + const [sorting, setSorting] = useState({ by: ORDER.seeders }); + const { data: torrents, isLoading, isFetched } = useTorrents({ query: title, order: sorting.by }); return ( !isOpen && onClose()}> @@ -82,21 +65,21 @@ const Torrents: FC = ({ title, onClose }) => { Title - - + + Magnet - {sortedTorrents.map((torrent) => ( - + {torrents?.map((torrent) => ( + {torrent.title}
{torrent.size}
{torrent.seeders} - + @@ -111,7 +94,7 @@ const Torrents: FC = ({ title, onClose }) => {
)} - {isFetched && !torrents.length && ( + {isFetched && !torrents?.length && (
diff --git a/components/Search/index.tsx b/components/Search/index.tsx deleted file mode 100644 index 5fc9e11..0000000 --- a/components/Search/index.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { FC } from 'react'; - -const Search: FC = () => { - return
Search
; -}; - -export default Search; diff --git a/types.d.ts b/types.d.ts index 61e7395..cb0656a 100644 --- a/types.d.ts +++ b/types.d.ts @@ -109,13 +109,10 @@ interface Torrent { title: string; seeders: number; size: string; - magnetLink: string; + magnet: string; } interface Category { id: string; name: string; } - -declare module 'rutracker-api-with-proxy'; -declare module 'thepiratebayfixed';