diff --git a/api/Http.ts b/api/Http.ts index d1a4a5d..1eccf8a 100644 --- a/api/Http.ts +++ b/api/Http.ts @@ -1,5 +1,5 @@ class Http { - base = 'https://api.themoviedb.org/3'; + private base = 'https://api.themoviedb.org/3'; get(url: string, { params }: { params?: Record } = {}, base?: string): Promise { const serializedParams = Object.entries(params || {}).reduce((params, [key, value]) => { diff --git a/api/dto/Show.ts b/api/dto/Show.ts index 1db8cc1..0a35648 100644 --- a/api/dto/Show.ts +++ b/api/dto/Show.ts @@ -1,3 +1,18 @@ +export const mapMovieSeriesToShow = ( + { id, overview, backdrop_path, poster_path, vote_average, vote_count, ...item }: Movie | Series, + type: Show['type'] = 'movie' +): Show => ({ + id, + type, + overview, + rating: vote_average, + votes: vote_count, + backdrop: backdrop_path ? `https://image.tmdb.org/t/p/w1280/${backdrop_path}` : '', + poster: poster_path ? `https://image.tmdb.org/t/p/w500/${poster_path}` : '', + release: 'release_date' in item ? item.release_date : item.first_air_date, + title: 'release_date' in item ? item.title : item.name +}); + const mapMoviesSeriesResponseToShows = ( { total_pages, page, results }: TMDBPagination | TMDBPagination, type: Show['type'] = 'movie' @@ -5,19 +20,8 @@ const mapMoviesSeriesResponseToShows = ( page, total: total_pages, results: results - .map(({ id, overview, backdrop_path, poster_path, vote_average, vote_count, ...item }) => ({ - id, - type, - overview, - rating: vote_average, - votes: vote_count, - backdrop: backdrop_path ? `https://image.tmdb.org/t/p/w1280/${backdrop_path}` : '', - poster: poster_path ? `https://image.tmdb.org/t/p/w500/${poster_path}` : '', - release: 'release_date' in item ? item.release_date : item.first_air_date, - title: 'release_date' in item ? item.title : item.name, - genres: [] - })) - .filter((show) => show.votes && show.release && show.backdrop && show.poster) + .map(show => mapMovieSeriesToShow(show, type)) + .filter((show) => show.votes && show.release && show.backdrop && show.poster && show.votes > 500) }); export default mapMoviesSeriesResponseToShows; diff --git a/api/index.ts b/api/index.ts index 022cd31..5bded55 100644 --- a/api/index.ts +++ b/api/index.ts @@ -1,8 +1,8 @@ 'use server'; -import { createUniqueRandomGenerator } from '../lib/utils'; +import { createUniqueRandomGenerator } from '@/lib/utils'; -import mapMoviesSeriesResponseToShows from './dto/Show'; +import mapMoviesSeriesResponseToShows, { mapMovieSeriesToShow } from './dto/Show'; import pirateBay from './parsers/pirate-bay'; import yts from './parsers/yts'; import http from './Http'; @@ -27,14 +27,27 @@ export const search = async ({ .then((response) => mapMoviesSeriesResponseToShows(response, type)); }; -const TOTAL_PAGES = 500; +const TOTAL_PAGES = 400; const generatePage = createUniqueRandomGenerator(TOTAL_PAGES); +export const details = async ({ + showId, + showType = 'movie' +}: { + showId: Show['id']; + showType: Show['type']; +}): Promise => { + return http + .get<(Movie | Series) & Details>(`/${showType}/${showId}`) + .then((response) => ({ ...mapMovieSeriesToShow(response, showType), ...response })); +}; + export const randomMovies = async (): Promise> => { return http .get>('/discover/movie', { params: { + 'vote_count.gte': 300, sort_by: 'popularity.desc', with_origin_country: 'UA|GB|JP|AU|US|IT|DE|FR', page: generatePage() @@ -147,16 +160,17 @@ export const credits = async ({ showType?: Show['type']; }): Promise> => { return http - .get<{ cast: Array }>(`/${showType}/${showId}/${showType === 'tv' ? 'aggregate_credits' : 'credits'}`) - .then((data) => { - if (showType === 'tv') { - return data.cast.map((item) => { - const actor = item as AggregatedActor; - const [role] = actor.roles || [{}]; - return { ...actor, ...role } as unknown as Actor; - }); - } - return data.cast as Array; + .get<{ cast: Array }>( + `/${showType}/${showId}/${showType === 'tv' ? 'aggregate_credits' : 'credits'}` + ) + .then(({ cast }) => { + return cast.map((actor) => { + if ('character' in actor) return actor; + + const [{ character }] = actor.roles || [{}]; + + return { ...actor, character }; + }); }); }; @@ -176,41 +190,21 @@ export const trailers = async ({ .then((data) => data.results); }; -export const torrentFiles = (magnet: string): Promise => { - return http - .get<{ name: string; subtitles: Subtitles[] }[]>('/files', { params: { magnet } }, process.env.TRACKER_PROXY_BASE) - .then((files) => - files.map((file) => { - const base = `${process.env.TRACKER_PROXY_BASE}/stream?magnet=${encodeURIComponent(magnet)}}`; - - return { - name: file.name, - src: `${base}&filename=${encodeURIComponent(file.name)}`, - subtitles: file.subtitles - }; - }) - ); -}; - export const YTSTorrents = async ({ query, sort, - showType, - showId + imdbID }: { + imdbID: string; query: string; sort: Sort; - showId: Show['id']; - showType: Show['type']; }) => { try { - const imdbID = await externalIDs({ showType, showId }); - const torrents = await yts.search({ + return yts.search({ imdbID, query, sort }); - return torrents; } catch (error) { return []; } @@ -220,17 +214,15 @@ export const TPBTorrents = async ({ query, sort }: { + imdbID: string; query: string; sort: Sort; - showId: Show['id']; - showType: Show['type']; }) => { try { - const torrents = await pirateBay.search({ + return pirateBay.search({ query, sort - }); - return torrents; + }) } catch (error) { return []; } diff --git a/components/Preview/components/Actions.tsx b/components/Preview/components/Actions.tsx index d76b553..13f88c0 100644 --- a/components/Preview/components/Actions.tsx +++ b/components/Preview/components/Actions.tsx @@ -7,7 +7,7 @@ import ToggleTrailer from '@/components/Show/components/Trailer'; import { Button } from '@/components/ui/button'; interface Props { - show: Show; + show: Show & Partial
; onClose?: () => void; } @@ -24,8 +24,7 @@ const Actions = ({ onClose, show }: Props) => ( className='h-8 w-1/2 grow' onClick={() => showTorrentsModal({ - showId: show.id, - showType: show.type, + imdbID: show.imdb_id!, title: show.title, backdrop: show.backdrop, year: new Date(show.release).getFullYear() diff --git a/components/Preview/index.tsx b/components/Preview/index.tsx index 5b7a4fc..0163a05 100644 --- a/components/Preview/index.tsx +++ b/components/Preview/index.tsx @@ -3,6 +3,7 @@ import { FC, ReactNode } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; +import useDetails from '@/components/Preview/useDetails'; import Card from '@/components/Show'; import Credits from '@/components/Show/components/Credits'; import { Modal } from '@/components/ui/dialog'; @@ -19,8 +20,12 @@ interface Props { card?: ReactNode; } -const Preview: FC = ({ show, className, onClose, children, card }) => { - if (!show) return null; +const Preview: FC = ({ show: baseShow, className, onClose, children, card }) => { + const { data: detailedShow } = useDetails({ showId: baseShow?.id, showType: baseShow?.type }); + + if (!baseShow) return null; + + const show: Show & Partial
| undefined = detailedShow || baseShow; return (
diff --git a/components/Preview/useDetails.ts b/components/Preview/useDetails.ts new file mode 100644 index 0000000..4bf6a9f --- /dev/null +++ b/components/Preview/useDetails.ts @@ -0,0 +1,12 @@ +import { useQuery } from '@tanstack/react-query'; + +import { details } from '@/api'; + +const useDetails = ({ showId, showType = 'movie' }: { showId?: Show['id']; showType?: Show['type'] }) => + useQuery({ + enabled: Boolean(showId && showType), + queryKey: ['details', showId, showType], + queryFn: () => details({ showId: showId!, showType }) + }); + +export default useDetails; diff --git a/components/Show/components/Torrents/index.tsx b/components/Show/components/Torrents/index.tsx index cb8d7ed..d598f44 100644 --- a/components/Show/components/Torrents/index.tsx +++ b/components/Show/components/Torrents/index.tsx @@ -16,14 +16,13 @@ import showHostManagerModal from './HostManager'; import useTorrents from './useTorrents'; interface Props extends InstanceProps { - showId: Show['id']; - showType: Show['type']; + imdbID: string; title: string; year: number; backdrop: string; } -const showTorrentsModal = create(({ title, year, showType, showId, backdrop, onResolve }: Props) => { +const showTorrentsModal = create(({ title, year, imdbID, backdrop, onResolve }: Props) => { const isMobile = matchMedia('(max-width: 600px)').matches; const prefix = usePrefix(); const [sort, setSort] = useState(Sort.seeds); @@ -39,8 +38,7 @@ const showTorrentsModal = create(({ title, year, showType, showId, backdrop, onR } = useTorrents({ query, sort, - showId, - showType, + imdbID, key, queryFn }); diff --git a/components/Show/components/Torrents/useTorrents.ts b/components/Show/components/Torrents/useTorrents.ts index 1789f30..efe8162 100644 --- a/components/Show/components/Torrents/useTorrents.ts +++ b/components/Show/components/Torrents/useTorrents.ts @@ -5,22 +5,20 @@ import { Sort } from '@/api/parsers'; const useTorrents = ({ query, sort, - showId, - showType, + imdbID, key, queryFn }: { - showId: Show['id']; - showType: Show['type']; key: string; query: string; sort: Sort; - queryFn: (params: { query: string; sort: Sort; showId: Show['id']; showType: Show['type'] }) => Promise; + imdbID: string; + queryFn: (params: { query: string; sort: Sort; imdbID: string; }) => Promise; }) => { return useQuery({ enabled: Boolean(query), - queryKey: [key, query, sort, showId, showType], - queryFn: () => queryFn({ query, sort, showId, showType }) + queryKey: [key, query, sort, imdbID], + queryFn: () => queryFn({ query, sort, imdbID }) }); }; diff --git a/env.example b/env.example index 395ee28..c0d8373 100644 --- a/env.example +++ b/env.example @@ -1,5 +1,4 @@ MOVIE_DB_TOKEN=YOUR_TOKEN -TRACKER_PROXY_BASE=HOST_URL_OF_YOUR_INSTANCE #https://github.com/noisy5ilence/tracker-proxy NEXT_PUBLIC_TRACKER_PROXY_BASE=HOST_URL_OF_YOUR_INSTANCE #https://github.com/noisy5ilence/tracker-proxy PIRATE_BAY_HOST=https://thepiratebay10.org YTS_HOST=https://yts.mx/api/v2/list_movies.json diff --git a/types.d.ts b/types.d.ts index 5818d37..8087b03 100644 --- a/types.d.ts +++ b/types.d.ts @@ -13,6 +13,13 @@ interface Pagination { results: T[]; } +interface Details { + genres: IDName[]; + imdb_id: string; + runtime: number; + tagline: string; +} + interface Movie { adult: boolean; backdrop_path: string; @@ -57,10 +64,9 @@ interface Show { release: Date; rating: number; votes: number; - genres: string[]; } -interface Actor { +interface Person { adult: boolean; gender: number; id: number; @@ -70,23 +76,17 @@ interface Actor { popularity: number; profile_path: string; cast_id: number; - character: string; credit_id: string; order: number; } -interface AggregatedActor { - adult: boolean; - gender: number; - id: number; - known_for_department: string; - name: string; - original_name: string; - popularity: number; - profile_path: string; +interface Actor extends Person { + character: string; +} + +interface AggregatedActor extends Person { roles: Role[]; total_episode_count: number; - order: number; } interface Role { @@ -96,8 +96,6 @@ interface Role { } interface Trailer { - iso_639_1: string; - iso_3166_1: string; name: string; key: string; site: string;