From a4d3f8a34ee3cd46a855ec0fbed83f3bb9f92a58 Mon Sep 17 00:00:00 2001 From: Daniel Haller Date: Mon, 22 Apr 2024 16:43:55 +0200 Subject: [PATCH] adn login implementation and search not disappearing fix --- components/ADN/GetAnime.ts | 0 components/ADN/ListEpisodes.ts | 0 pages/addanime.vue | 78 ++++++++-- pages/adnlogin.vue | 59 ++++++++ src/api/routes/adn/adn.service.ts | 146 ++++++++----------- src/api/routes/service/service.controller.ts | 7 +- 6 files changed, 193 insertions(+), 97 deletions(-) create mode 100644 components/ADN/GetAnime.ts create mode 100644 components/ADN/ListEpisodes.ts create mode 100644 pages/adnlogin.vue diff --git a/components/ADN/GetAnime.ts b/components/ADN/GetAnime.ts new file mode 100644 index 0000000..e69de29 diff --git a/components/ADN/ListEpisodes.ts b/components/ADN/ListEpisodes.ts new file mode 100644 index 0000000..e69de29 diff --git a/pages/addanime.vue b/pages/addanime.vue index 84131e2..b16ba6c 100644 --- a/pages/addanime.vue +++ b/pages/addanime.vue @@ -19,7 +19,7 @@ -
+
-
+
-
+
Click to Login
+
+ +
-
@@ -282,7 +286,7 @@ const searchActive = ref(false) const crunchySearchResults = ref() const adnSearchResults = ref() const CRselectedShow = ref() -const ADNselectedShow = ref() +const ADNselectedShow = ref() const url = ref('') const path = ref() const service = ref<'adn' | 'crunchyroll'>('crunchyroll') @@ -302,7 +306,9 @@ const isFetchingEpisodes = ref(0) const isFetchingResults = ref(0) const isLoggedInCR = ref(false) -let interval: NodeJS.Timeout; + const isLoggedInADN = ref(false) +let intervalcr: NodeJS.Timeout; +let intervaladn: NodeJS.Timeout; const checkIfLoggedInCR = async () => { const { data, error } = await checkAccount('CR') @@ -312,8 +318,8 @@ const checkIfLoggedInCR = async () => { return } - if (interval) { - clearInterval(interval) + if (intervalcr) { + clearInterval(intervalcr) } isLoggedInCR.value = true @@ -328,10 +334,39 @@ const openCRLogin = () => { backgroundColor: "#111111" }) - interval = setInterval(checkIfLoggedInCR, 1000) + intervalcr = setInterval(checkIfLoggedInCR, 1000) } +const checkIfLoggedInADN = async () => { + const { data, error } = await checkAccount('ADN') + + if (error.value) { + isLoggedInADN.value = false + return + } + + if (intervaladn) { + clearInterval(intervaladn) + } + + isLoggedInADN.value = true +} + +const openADNLogin = () => { + (window as any).myAPI.openWindow({ + title: "ADN Login", + url: isProduction ? 'http://localhost:8079/adnlogin' : 'http://localhost:3000/adnlogin', + width: 600, + height: 300, + backgroundColor: "#111111" + }) + + intervalcr = setInterval(checkIfLoggedInADN, 1000) +} + + checkIfLoggedInCR() +checkIfLoggedInADN() const fetchSearch = async () => { if (!search.value || search.value.length === 0) { @@ -341,7 +376,9 @@ const fetchSearch = async () => { return } - isFetchingResults.value++ + if (!isFetchingResults.value) { + isFetchingResults.value++ + } if (service.value === 'adn') { adnSearchResults.value = await searchADN(search.value) @@ -351,7 +388,9 @@ const fetchSearch = async () => { crunchySearchResults.value = await searchCrunchy(search.value) } - isFetchingResults.value-- + if (isFetchingResults.value) { + isFetchingResults.value-- + } searchActive.value = true } @@ -367,6 +406,12 @@ const handleInputChange = () => { debounceFetchSearch() } +watch(url, () => { + if (url.value.length === 0 || !url.value) { + searchActive.value = false; + } +}) + onMounted(() => { ;(window as any).myAPI.getFolder().then((result: any) => { path.value = result @@ -396,12 +441,21 @@ const selectShow = async (show: any) => { crunchySearchResults.value = [] adnSearchResults.value = [] searchActive.value = false + if (isFetchingResults.value) { + isFetchingResults.value-- + } } watch(selectedSeason, () => { refetchEpisodes() }) +watch(service, () => { + url.value = "", + CRselectedShow.value = null, + ADNselectedShow.value = null +}) + watch(selectedStartEpisode, () => { if (!selectedEndEpisode.value) return if (!episodes.value) return diff --git a/pages/adnlogin.vue b/pages/adnlogin.vue new file mode 100644 index 0000000..acb9947 --- /dev/null +++ b/pages/adnlogin.vue @@ -0,0 +1,59 @@ + + + + + diff --git a/src/api/routes/adn/adn.service.ts b/src/api/routes/adn/adn.service.ts index ba19b30..7f77133 100644 --- a/src/api/routes/adn/adn.service.ts +++ b/src/api/routes/adn/adn.service.ts @@ -378,92 +378,74 @@ import { useFetch } from '../useFetch' // } // } -export async function adnyLogin(user: string, passw: string) { - const cachedData: - | { - access_token: string - refresh_token: string - expires_in: number - token_type: string - scope: string - country: string - account_id: string - profile_id: string - } - | undefined = server.CacheController.get('adntoken') - - if (!cachedData) { - var { data, error } = await adnLoginFetch(user, passw) - - if (error) { - messageBox( - 'error', - ['Cancel'], - 2, - 'Failed to login', - 'Failed to login to ADN', - (error.error as string) - ) - return { data: null, error: error.error } +export async function adnLogin(user: string, passw: string) { + const cachedData: + | { + access_token: string + refresh_token: string + expires_in: number + token_type: string + scope: string + country: string + account_id: string + profile_id: string } - - if (!data) { - messageBox('error', ['Cancel'], 2, 'Failed to login', 'Failed to login to ADN', 'ADN returned null') - return { data: null, error: 'ADN returned null' } - } - - if (!data.access_token) { - messageBox('error', ['Cancel'], 2, 'Failed to login', 'Failed to login to ADN', 'ADN returned malformed data') - return { data: null, error: 'ADN returned malformed data' } - } - - server.CacheController.set('adntoken', data, data.expires_in - 30) - - return { data: data, error: null } - } - - return { data: cachedData, error: null } - } - - async function adnLoginFetch(user: string, passw: string) { - const headers = { - Authorization: 'Basic dC1rZGdwMmg4YzNqdWI4Zm4wZnE6eWZMRGZNZnJZdktYaDRKWFMxTEVJMmNDcXUxdjVXYW4=', - 'Content-Type': 'application/x-www-form-urlencoded', - 'User-Agent': 'Crunchyroll/3.46.2 Android/13 okhttp/4.12.0' - } - - const body: any = { - username: user, - password: passw, - grant_type: 'password', - scope: 'offline_access', - device_name: 'RMX2170', - device_type: 'realme RMX2170' - } - - const { data, error } = await useFetch<{ - access_token: string - refresh_token: string - expires_in: number - token_type: string - scope: string - country: string - account_id: string - profile_id: string - }>('https://beta-api.crunchyroll.com/auth/v1/token', { - type: 'POST', - body: new URLSearchParams(body).toString(), - header: headers, - credentials: 'same-origin' - }) - + | undefined = server.CacheController.get('adntoken') + + if (!cachedData) { + var { data, error } = await adnLoginFetch(user, passw) + if (error) { - return { data: null, error: error } + messageBox('error', ['Cancel'], 2, 'Failed to login', 'Failed to login to ADN', error.error as string) + return { data: null, error: error.error } } - + if (!data) { - return { data: null, error: null } + messageBox('error', ['Cancel'], 2, 'Failed to login', 'Failed to login to ADN', 'ADN returned null') + return { data: null, error: 'ADN returned null' } } - + + if (!data.accessToken) { + messageBox('error', ['Cancel'], 2, 'Failed to login', 'Failed to login to ADN', 'ADN returned malformed data') + return { data: null, error: 'ADN returned malformed data' } + } + + server.CacheController.set('adntoken', data, 300) + return { data: data, error: null } } + + return { data: cachedData, error: null } +} + +async function adnLoginFetch(user: string, passw: string) { + const headers = { + 'x-target-distribution': 'de', + 'Content-Type': 'application/json' + } + + const body = { + username: user, + password: passw, + source: 'Web', + rememberMe: true + } + + const { data, error } = await useFetch<{ + accessToken: string; + }>('https://gw.api.animationdigitalnetwork.fr/authentication/login', { + type: 'POST', + body: JSON.stringify(body), + header: headers, + }) + + if (error) { + return { data: null, error: error } + } + + if (!data) { + return { data: null, error: null } + } + + return { data: data, error: null } +} diff --git a/src/api/routes/service/service.controller.ts b/src/api/routes/service/service.controller.ts index fe4533f..060d209 100644 --- a/src/api/routes/service/service.controller.ts +++ b/src/api/routes/service/service.controller.ts @@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify" import { crunchyLogin } from "../crunchyroll/crunchyroll.service" import { addEpisodeToPlaylist, getDownloading, getPlaylist, loggedInCheck, safeLoginData } from "./service.service" import { CrunchyEpisodes } from "../../types/crunchyroll" +import { adnLogin } from "../adn/adn.service" export async function checkLoginController(request: FastifyRequest<{ Params: { @@ -48,9 +49,9 @@ export async function loginController( } if (params.id === 'ADN') { - // const { data, error } = await adnLogin(body.user, body.password) - // responseError = error, - // responseData = data + const { data, error } = await adnLogin(body.user, body.password) + responseError = error, + responseData = data } if (responseError || !responseData) {