From 55c45b9c0bf7a3b28dbef5b8328753c592a953f1 Mon Sep 17 00:00:00 2001 From: stratuma Date: Mon, 27 May 2024 17:37:50 +0200 Subject: [PATCH] added watch url input support --- components/Crunchyroll/ListAnimes.ts | 79 ++++++++++++++++++++++++++-- components/Crunchyroll/Types.ts | 12 +++++ pages/addanime.vue | 47 ++++++++++++++++- 3 files changed, 132 insertions(+), 6 deletions(-) diff --git a/components/Crunchyroll/ListAnimes.ts b/components/Crunchyroll/ListAnimes.ts index 64e2366..90ec799 100644 --- a/components/Crunchyroll/ListAnimes.ts +++ b/components/Crunchyroll/ListAnimes.ts @@ -1,7 +1,7 @@ import type { CrunchyrollSearchResults } from '../Search/Types' import { crunchyLogin } from './Account' import { getProxies } from './Proxy' -import type { CrunchyAnimeFetch, CrunchySearchFetch } from './Types' +import type { CrunchyAnimeFetch, CrunchyEpisodeFetch, CrunchyEpisodesFetch, CrunchySearchFetch } from './Types' export async function searchCrunchy(q: string) { var isProxyActive: boolean | undefined @@ -123,7 +123,13 @@ export async function getCRSeries(q: string) { isProxyActive = result }) - const { data: proxies } = await getProxies() + var proxies + + if (isProxyActive) { + const { data: prox } = await getProxies() + + proxies = prox.value + } const { data: token, error: tokenerror } = await crunchyLogin('LOCAL') @@ -143,8 +149,8 @@ export async function getCRSeries(q: string) { throw new Error(JSON.stringify(error.value)) } - if (!data.value && proxies.value && isProxyActive) { - for (const p of proxies.value) { + if (!data.value && proxies && isProxyActive) { + for (const p of proxies) { if (p.status !== 'offline') { const { data: tokeng, error: tokenerrorg } = await crunchyLogin(p.code) @@ -192,3 +198,68 @@ export async function getCRSeries(q: string) { Geo: undefined } } + +export async function getCREpisodeSeriesID(q: string) { + var isProxyActive: boolean | undefined + ;(window as any).myAPI.getProxyActive().then((result: boolean) => { + isProxyActive = result + }) + + var proxies + + if (isProxyActive) { + const { data: prox } = await getProxies() + + proxies = prox.value + } + + const { data: token, error: tokenerror } = await crunchyLogin('LOCAL') + + if (!token.value) { + return + } + + const { data, error } = await useFetch(`https://beta-api.crunchyroll.com/content/v2/cms/objects/${q}`, { + method: 'GET', + headers: { + Authorization: `Bearer ${token.value.access_token}` + } + }) + + if (error.value) { + console.error(error.value) + throw new Error(JSON.stringify(error.value)) + } + + if (!data.value && proxies && isProxyActive) { + for (const p of proxies) { + if (p.status !== 'offline') { + const { data: tokeng, error: tokenerrorg } = await crunchyLogin(p.code) + + if (!tokeng.value) { + return + } + + const { data: fdata, error: ferror } = await useFetch(`https://beta-api.crunchyroll.com/content/v2/cms/series/${q}`, { + method: 'GET', + headers: { + Authorization: `Bearer ${tokeng.value.access_token}` + } + }) + + if (ferror.value) { + console.error(ferror.value) + throw new Error(JSON.stringify(ferror.value)) + } + + data.value = fdata.value + } + } + } + + if (!data.value) return + + const episode = data.value.data[0] + + return episode.episode_metadata.series_id +} diff --git a/components/Crunchyroll/Types.ts b/components/Crunchyroll/Types.ts index 90c4318..b0b8d9f 100644 --- a/components/Crunchyroll/Types.ts +++ b/components/Crunchyroll/Types.ts @@ -239,6 +239,18 @@ export interface CrunchyEpisodesFetch { } } +export interface CrunchyEpisodeFetch { + total: number + data: Array<{ + episode_metadata: { + series_id: string + } + }> + meta: { + versions_considered: boolean + } +} + export interface Proxy { name: string code: string diff --git a/pages/addanime.vue b/pages/addanime.vue index 3589cca..55e299a 100644 --- a/pages/addanime.vue +++ b/pages/addanime.vue @@ -347,7 +347,7 @@ import { searchADN } from '~/components/ADN/ListAnimes' import { getEpisodesWithShowIdADN } from '~/components/ADN/ListEpisodes' import type { ADNEpisode, ADNEpisodes } from '~/components/ADN/Types' import { checkAccount } from '~/components/Crunchyroll/Account' -import { getCRSeries, searchCrunchy } from '~/components/Crunchyroll/ListAnimes' +import { getCREpisodeSeriesID, getCRSeries, searchCrunchy } from '~/components/Crunchyroll/ListAnimes' import { listEpisodeCrunchy } from '~/components/Crunchyroll/ListEpisodes' import { listSeasonCrunchy } from '~/components/Crunchyroll/ListSeasons' import type { CrunchyEpisode, CrunchyEpisodes } from '~/components/Episode/Types' @@ -674,7 +674,7 @@ const switchToSeason = async () => { } } - if (url.value && url.value.includes('crunchyroll') && !CRselectedShow.value) { + if (url.value && url.value.includes('crunchyroll') && url.value.includes('/series/') && !CRselectedShow.value) { const seriesID = url.value.split('/') CRselectedShow.value = await getCRSeries(seriesID[5]) if (!CRselectedShow.value) return @@ -715,6 +715,49 @@ const switchToSeason = async () => { } } + if (url.value && url.value.includes('crunchyroll') && url.value.includes('/watch/') && !CRselectedShow.value) { + const episodeID = url.value.split('/') + const seriesID = await getCREpisodeSeriesID(episodeID[5]) + if (!seriesID) return + CRselectedShow.value = await getCRSeries(seriesID) + if (!CRselectedShow.value) return + seasons.value = await listSeasonCrunchy(CRselectedShow.value.ID, CRselectedShow.value.Geo) + if (!seasons.value) { + isFetchingSeasons.value-- + return + } + selectedSeason.value = seasons.value[0] + episodes.value = await listEpisodeCrunchy(selectedSeason.value.id, CRselectedShow.value.Geo) + if (episodes.value) { + selectedStartEpisode.value = episodes.value[0] + selectedEndEpisode.value = episodes.value[0] + } + tab.value = 2 + + selectedDubs.value = [] + selectedSubs.value = [] + + if (dubLocales.value && dubLocales.value.length !== 0) { + for (const a of dubLocales.value) { + if (CRselectedShow.value.Dubs.find((cr) => cr === a.locale)) { + toggleDub(a) + } + } + } else { + selectedDubs.value = [{ locale: 'ja-JP', name: 'JP' }] + } + + if (subLocales.value && subLocales.value.length !== 0) { + for (const a of subLocales.value) { + if (CRselectedShow.value.Subs.find((cr) => cr === a.locale)) { + toggleSub(a) + } + } + } else { + selectedSubs.value = [] + } + } + isFetchingSeasons.value-- }