From 8a9def00ded6382aa268b51252344a174255db09 Mon Sep 17 00:00:00 2001 From: Megh Rathod Date: Thu, 11 Apr 2024 23:34:40 +0530 Subject: [PATCH 1/7] fix: tmdb fetch failure due ISP blocks in India Signed-off-by: Megh Rathod --- src/backend/metadata/tmdb.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 67c7d56f..2e11bf73 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -143,7 +143,7 @@ export function decodeTMDBId( }; } -const baseURL = "https://api.themoviedb.org/3"; +const baseURL = "https://api.tmdb.org/3"; const apiKey = conf().TMDB_READ_API_KEY; From 1ec51699d1bfcda0d284f97d7d90c4aa9b614d47 Mon Sep 17 00:00:00 2001 From: Megh Rathod Date: Fri, 12 Apr 2024 11:33:18 +0530 Subject: [PATCH 2/7] fix: add alternate tmdb endpoint to fix errors when main url is blocked Signed-off-by: Megh Rathod --- src/backend/metadata/tmdb.ts | 44 +++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 2e11bf73..0f01d684 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -143,7 +143,8 @@ export function decodeTMDBId( }; } -const baseURL = "https://api.tmdb.org/3"; +const otherUrl = "https://api.tmdb.org/3"; +let baseURL = "https://api.themoviedb.org/3"; const apiKey = conf().TMDB_READ_API_KEY; @@ -155,13 +156,40 @@ const headers = { async function get(url: string, params?: object): Promise { if (!apiKey) throw new Error("TMDB API key not set"); - const res = await mwFetch(encodeURI(url), { - headers, - baseURL, - params: { - ...params, - }, - }); + const controller = new AbortController(); + const { signal } = controller; + + const timeoutId = + baseURL === otherUrl + ? setTimeout(() => controller.abort(), 15000) + : setTimeout(() => controller.abort(), 3000); + let res: Promise; + + try { + res = await mwFetch(encodeURI(url), { + headers, + baseURL, + params: { + ...params, + }, + signal, + }); + clearTimeout(timeoutId); + } catch (err) { + if (baseURL !== otherUrl) { + baseURL = otherUrl; + res = await mwFetch(encodeURI(url), { + headers, + baseURL, + params: { + ...params, + }, + }); + } else { + res = Promise.reject(); + } + } + return res; } From 76d906c95a6de6cc0043d8c4c5c3fcddba7f895e Mon Sep 17 00:00:00 2001 From: Megh Rathod Date: Fri, 12 Apr 2024 23:29:42 +0530 Subject: [PATCH 3/7] fix: use AbortSignal.timeout instead of setTimeout Signed-off-by: Megh Rathod --- src/backend/metadata/tmdb.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 0f01d684..f9d21c6c 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -156,13 +156,6 @@ const headers = { async function get(url: string, params?: object): Promise { if (!apiKey) throw new Error("TMDB API key not set"); - const controller = new AbortController(); - const { signal } = controller; - - const timeoutId = - baseURL === otherUrl - ? setTimeout(() => controller.abort(), 15000) - : setTimeout(() => controller.abort(), 3000); let res: Promise; try { @@ -172,9 +165,8 @@ async function get(url: string, params?: object): Promise { params: { ...params, }, - signal, + signal: AbortSignal.timeout(baseURL !== otherUrl ? 5000 : 30000), }); - clearTimeout(timeoutId); } catch (err) { if (baseURL !== otherUrl) { baseURL = otherUrl; From 0e3f82df302693d70e622666022877793a6ef1a9 Mon Sep 17 00:00:00 2001 From: Megh Rathod Date: Sun, 14 Apr 2024 15:14:14 +0530 Subject: [PATCH 4/7] Return values instead of promise Co-authored-by: William Oldham --- src/backend/metadata/tmdb.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index f9d21c6c..2e675f10 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -156,10 +156,10 @@ const headers = { async function get(url: string, params?: object): Promise { if (!apiKey) throw new Error("TMDB API key not set"); - let res: Promise; + let res: T; try { - res = await mwFetch(encodeURI(url), { + res = await mwFetch(encodeURI(url), { headers, baseURL, params: { @@ -170,7 +170,7 @@ async function get(url: string, params?: object): Promise { } catch (err) { if (baseURL !== otherUrl) { baseURL = otherUrl; - res = await mwFetch(encodeURI(url), { + res = await mwFetch(encodeURI(url), { headers, baseURL, params: { From 995c855ac2b77001b2541b3010bb944076ec6bf8 Mon Sep 17 00:00:00 2001 From: Megh Rathod Date: Sun, 14 Apr 2024 16:19:51 +0530 Subject: [PATCH 5/7] added useFallback to decide which TMDB url to use Signed-off-by: Megh Rathod --- src/backend/metadata/tmdb.ts | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 2e675f10..67a79490 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -143,8 +143,9 @@ export function decodeTMDBId( }; } +const baseURL = "https://api.themoviedb.org/3"; const otherUrl = "https://api.tmdb.org/3"; -let baseURL = "https://api.themoviedb.org/3"; +let useFallback = false; const apiKey = conf().TMDB_READ_API_KEY; @@ -155,33 +156,26 @@ const headers = { async function get(url: string, params?: object): Promise { if (!apiKey) throw new Error("TMDB API key not set"); - let res: T; - try { res = await mwFetch(encodeURI(url), { headers, - baseURL, + baseURL: !useFallback ? baseURL : otherUrl, params: { ...params, }, - signal: AbortSignal.timeout(baseURL !== otherUrl ? 5000 : 30000), + signal: AbortSignal.timeout(!useFallback ? 5000 : 30000), }); } catch (err) { - if (baseURL !== otherUrl) { - baseURL = otherUrl; - res = await mwFetch(encodeURI(url), { - headers, - baseURL, - params: { - ...params, - }, - }); - } else { - res = Promise.reject(); - } + useFallback = true; + res = await mwFetch(encodeURI(url), { + headers, + baseURL: otherUrl, + params: { + ...params, + }, + }); } - return res; } From 90c4365422b225a4ab6fe429872e35cbdb5c4231 Mon Sep 17 00:00:00 2001 From: Seun Taiwo Date: Sat, 13 Apr 2024 09:28:13 +0100 Subject: [PATCH 6/7] Fixed next episode button not showing next season --- src/assets/locales/en.json | 3 +- .../player/atoms/NextEpisodeButton.tsx | 79 ++++++++++++++++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/assets/locales/en.json b/src/assets/locales/en.json index bc042462..e0d33ff5 100644 --- a/src/assets/locales/en.json +++ b/src/assets/locales/en.json @@ -362,7 +362,8 @@ }, "nextEpisode": { "cancel": "Cancel", - "next": "Next episode" + "next": "Next episode", + "nextSeason": "Next season" }, "playbackError": { "badge": "Playback error", diff --git a/src/components/player/atoms/NextEpisodeButton.tsx b/src/components/player/atoms/NextEpisodeButton.tsx index 906b6829..29d424c7 100644 --- a/src/components/player/atoms/NextEpisodeButton.tsx +++ b/src/components/player/atoms/NextEpisodeButton.tsx @@ -1,7 +1,10 @@ import classNames from "classnames"; import { useCallback } from "react"; import { useTranslation } from "react-i18next"; +import { useAsync } from "react-use"; +import { getMetaFromId } from "@/backend/metadata/getmeta"; +import { MWMediaType, MWSeasonMeta } from "@/backend/metadata/types/mw"; import { Icon, Icons } from "@/components/Icon"; import { usePlayerMeta } from "@/components/player/hooks/usePlayerMeta"; import { Transition } from "@/components/utils/Transition"; @@ -9,6 +12,8 @@ import { PlayerMeta } from "@/stores/player/slices/source"; import { usePlayerStore } from "@/stores/player/store"; import { useProgressStore } from "@/stores/progress"; +import { hasAired } from "../utils/aired"; + function shouldShowNextEpisodeButton( time: number, duration: number, @@ -39,6 +44,45 @@ function Button(props: { ); } +function useSeasons(mediaId: string, isLastEpisode: boolean = false) { + const state = useAsync(async () => { + if (isLastEpisode) { + const data = await getMetaFromId(MWMediaType.SERIES, mediaId ?? ""); + if (data?.meta.type !== MWMediaType.SERIES) return null; + return data.meta.seasons; + } + }, [mediaId, isLastEpisode]); + + return state; +} + +function useNextSeasonEpisode( + nextSeason: MWSeasonMeta | undefined, + mediaId: string, +) { + const state = useAsync(async () => { + if (nextSeason) { + const data = await getMetaFromId( + MWMediaType.SERIES, + mediaId ?? "", + nextSeason?.id, + ); + if (data?.meta.type !== MWMediaType.SERIES) return null; + + const nextSeasonEpisodes = data?.meta?.seasonData?.episodes + .filter((episode) => hasAired(episode.air_date)) + .map((episode) => ({ + number: episode.number, + title: episode.title, + tmdbId: episode.id, + })); + + if (nextSeasonEpisodes.length > 0) return nextSeasonEpisodes[0]; + } + }, [mediaId, nextSeason?.id]); + return state; +} + export function NextEpisodeButton(props: { controlsShowing: boolean; onChange?: (meta: PlayerMeta) => void; @@ -58,6 +102,20 @@ export function NextEpisodeButton(props: { ); const updateItem = useProgressStore((s) => s.updateItem); + const isLastEpisode = + meta?.episode?.number === meta?.episodes?.at(-1)?.number; + + const seasons = useSeasons(meta?.tmdbId ?? "", isLastEpisode); + + const nextSeason = seasons.value?.find( + (season) => season.number === (meta?.season?.number ?? 0) + 1, + ); + + const nextSeasonEpisode = useNextSeasonEpisode( + nextSeason, + meta?.tmdbId ?? "", + ); + let show = false; if (showingState === "always") show = true; else if (showingState === "hover" && props.controlsShowing) show = true; @@ -70,14 +128,23 @@ export function NextEpisodeButton(props: { ? bottom : "bottom-[calc(3rem+env(safe-area-inset-bottom))]"; - const nextEp = meta?.episodes?.find( - (v) => v.number === (meta?.episode?.number ?? 0) + 1, - ); + const nextEp = isLastEpisode + ? nextSeasonEpisode.value + : meta?.episodes?.find( + (v) => v.number === (meta?.episode?.number ?? 0) + 1, + ); const loadNextEpisode = useCallback(() => { if (!meta || !nextEp) return; const metaCopy = { ...meta }; metaCopy.episode = nextEp; + metaCopy.season = + isLastEpisode && nextSeason + ? { + ...nextSeason, + tmdbId: nextSeason.id, + } + : metaCopy.season; setShouldStartFromBeginning(true); setDirectMeta(metaCopy); props.onChange?.(metaCopy); @@ -93,6 +160,8 @@ export function NextEpisodeButton(props: { props, setShouldStartFromBeginning, updateItem, + isLastEpisode, + nextSeason, ]); if (!meta?.episode || !nextEp) return null; @@ -121,7 +190,9 @@ export function NextEpisodeButton(props: { className="bg-buttons-primary hover:bg-buttons-primaryHover text-buttons-primaryText flex justify-center items-center" > - {t("player.nextEpisode.next")} + {isLastEpisode && nextEp + ? t("player.nextEpisode.nextSeason") + : t("player.nextEpisode.next")} From 926018310e0cb5fad8dc0f61ea7bd23b3adf0dd9 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 14 Apr 2024 21:29:45 +0100 Subject: [PATCH 7/7] Fix TMDB code --- src/backend/metadata/tmdb.ts | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 67a79490..331f022f 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -143,40 +143,37 @@ export function decodeTMDBId( }; } -const baseURL = "https://api.themoviedb.org/3"; -const otherUrl = "https://api.tmdb.org/3"; -let useFallback = false; +const tmdbBaseUrl1 = "https://api.themoviedb.org/3"; +const tmdbBaseUrl2 = "https://api.tmdb.org/3"; const apiKey = conf().TMDB_READ_API_KEY; -const headers = { +const tmdbHeaders = { accept: "application/json", Authorization: `Bearer ${apiKey}`, }; async function get(url: string, params?: object): Promise { if (!apiKey) throw new Error("TMDB API key not set"); - let res: T; try { - res = await mwFetch(encodeURI(url), { - headers, - baseURL: !useFallback ? baseURL : otherUrl, + return await mwFetch(encodeURI(url), { + headers: tmdbHeaders, + baseURL: tmdbBaseUrl1, params: { ...params, }, - signal: AbortSignal.timeout(!useFallback ? 5000 : 30000), + signal: AbortSignal.timeout(5000), }); } catch (err) { - useFallback = true; - res = await mwFetch(encodeURI(url), { - headers, - baseURL: otherUrl, + return mwFetch(encodeURI(url), { + headers: tmdbHeaders, + baseURL: tmdbBaseUrl2, params: { ...params, }, + signal: AbortSignal.timeout(30000), }); } - return res; } export async function multiSearch(