From bc706930b8eb2557de70395e25156352d23d7110 Mon Sep 17 00:00:00 2001 From: Histmy Date: Tue, 18 Mar 2025 13:20:21 +0100 Subject: [PATCH] =?UTF-8?q?Brut=C3=A1ln=C3=AD=20ordinace=20sjeta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/vordinace.ts | 165 ++++++++++++++++++++++++++++++++++++--- src/utils/utils.ts | 2 +- 2 files changed, 154 insertions(+), 13 deletions(-) diff --git a/src/modules/vordinace.ts b/src/modules/vordinace.ts index 4c59d19..92e3c0b 100644 --- a/src/modules/vordinace.ts +++ b/src/modules/vordinace.ts @@ -1,24 +1,162 @@ import { GuildScheduledEventEntityType, GuildScheduledEventPrivacyLevel, TextChannel } from "discord.js"; import { CClient, Modul } from "../utils/types"; import { log } from "../utils/utils"; +import { RawData, WebSocket } from "ws"; let client: CClient; -async function ziskatNazev() { - const stranka = await fetch("https://voyo.nova.cz/api/v1/show/content?showId=1&type=episodes&season=6947&count=1&offset=0&url=%2Fserialy%2F1-ordinace-v-ruzove-zahrade-2") - .then(r => r.text()) - .catch(e => log("chyba pri accessovani VOYO stránky", e)); +const logg = (...cokoli: any[]) => log("[OnePlayTest]: ", ...cokoli); - const nazevFull = /h3 class=\"title\">.+?>(.+?)<\//s.exec(stranka || ""); +const token = process.env.OnePlayToken; - if (!nazevFull) { - log(new Error("neni nazev")); - return "Vordinačka"; +let clientId: string | undefined = undefined; +let sessionId: string | undefined = undefined; +let serverId: string | undefined = undefined; + +function createOneplayBody(payload: Record, customData: string) { + + if (typeof clientId == "undefined" || typeof sessionId == "undefined" || typeof serverId == "undefined") { + throw new Error("není všechnno nastaveno"); } - const nazevCasti = /(?\d+). díl - (?.+)/.exec(nazevFull[1]); + const vec = { + "deviceInfo": { + "deviceType": "web", + "appVersion": "1.0.18", + "deviceManufacturer": "Unknown", + "deviceOs": "Windows" + }, + "capabilities": { + "async": "websockets" + }, + "payload": payload, + "context": { + "customData": customData, + "requestId": crypto.randomUUID(), + "clientId": clientId, + "sessionId": sessionId, + "serverId": serverId + } + }; - return nazevCasti ? `${nazevCasti.groups!.cislo}. ${nazevCasti.groups!.nazev}` : nazevFull[1]; + return JSON.stringify(vec); +} + +async function udelejOneplayRquest(cesta: string, body: string) { + if (!token) { + throw new Error("token more"); + } + + const res = await fetch(`https://http.cms.jyxo.cz/api/v3/${cesta}`, { + method: "POST", + body, + headers: { Authorization: token } + }).then(res => res.json()) + .catch(e => { throw new Error(`chyba pri udelejoneplayrequest fetch ${e}`); }); + + logg("nejspis uspesna odpoved", res); +} + +const sleep = (time: number) => new Promise(res => setTimeout(res, time)); + +async function executeOrdinaceFlow() { + + // Inicializace + await udelejOneplayRquest("app.init", createOneplayBody({ + "reason": "start", + "route": { + "url": "https://www.oneplay.cz/porad/1-ordinace-v-ruzove-zahrade-2" + } + }, `{"requireStartAction":true}`)); + + await sleep(500); + + // Navigace na stránku Ordinace + await udelejOneplayRquest("page.content.display", createOneplayBody({ + contentId: "show.1" + }, `{"shouldBeInModal":true}`)); + + await sleep(500); + + // Otevření 24. řady + await udelejOneplayRquest("carousel.display", createOneplayBody({ + carouselId: "show:1;tab:65;parent:64;carousel:65", + criteria: { + filterCriterias: "season:6947", + sortOption: "DESC" + } + }, `{"blockId":"show:1;tab:65;parent:64;block:65"}`)); + +} + +const handleMessageAndGetNazev = (socket: WebSocket) => new Promise((res, rej) => { + const listener = (m: RawData) => { + //const mes = m.toString(); + //logg("data:", mes); + + const json = JSON.parse(m.toString()); + + logg("celyjson je", json); + + // if (mes.includes("\"schema\":\"Ping\"")) { + // logg("pingec, odpovidam"); + + // socket.send(`{"schema":"Pong"}`); + // } + + if (json.result?.schema == "ConnectionInitData") { + logg("jojo,slysim a jdu na to"); + serverId = json.data.serverId; + sessionId = json.data.sessionId; + logg("serverid", serverId, "sessionid", sessionId); + executeOrdinaceFlow().catch(rej); + } + else if (json?.schema == "ApiCall" && json?.command == "carousel.display") { + logg("máme tady názvy epizod zvolené série"); + + const nazev = json?.response?.data?.carousel?.tiles?.[0]?.title; + + //logg(typeof json, typeof json?.response, typeof json?.response?.data, typeof json?.response?.data?.carousel, typeof json?.response?.data?.carousel?.titles); + + if (!nazev) { + return rej("nazev neexistuje"); + } + + clearTimeout(timeout); + socket.off("message", listener); + res(nazev); + } + }; + + const timeout = setTimeout(() => { + socket.off("message", listener); // Odstraníme posluchač + rej(new Error("Časový limit vypršel, jméno nebylo přijato.")); + }, 10000); // Timeout 10 sekund + + + socket.on("message", listener); +}); + +async function ziskatNazev() { + // Tak tohle stojí fakt za hovno oproti voyu :sjeta: + + clientId = crypto.randomUUID(); + + logg("moje Ws id je", clientId); + + let socket: WebSocket | null = null; + + try { + socket = new WebSocket(`wss://ws.cms.jyxo.cz/websocket/${clientId}`); + + return await handleMessageAndGetNazev(socket); + } catch (e) { + logg("nazev ordinace se nepodarilo ziskat", e); + throw new Error("nazev neni"); + } finally { + socket?.close(); + logg("zavrel jsem soket"); + } } async function naplanovat() { @@ -45,7 +183,7 @@ async function urobit() { kdyToBude.setSeconds(0); const event = await guilda.scheduledEvents.create({ - name: await ziskatNazev(), + name: (await ziskatNazev()) || "Vordinačka", scheduledStartTime: kdyToBude, entityType: GuildScheduledEventEntityType.Voice, privacyLevel: GuildScheduledEventPrivacyLevel.GuildOnly, @@ -69,7 +207,10 @@ const exp: Modul = { on_ready: () => { client = module.exports.client; - naplanovat(); + if (token) { + naplanovat(); + //logg("nazev ordinace je:", await ziskatNazev()); + } } }; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 00a7cad..8e5b611 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -9,7 +9,7 @@ if (!existsSync("config.json")) throw new Error("config.json neexistuje"); const konfig = require("../../config.json"); if (!konfig.ignoreUnknownKeys) for (const klic of Object.keys(konfig)) { - if (!["adminChannel", "adminID", "DBPwd", "DBUser", "dieOnError", "ignoreMess", "ignorePresence", "ignoreSpink", "ignoreUnknownKeys", "noGeneralSync", "prefix", "sachyDomena", "statPass", "token"].includes(klic)) throw new Error(`config.json obsahuje neznámý klíč: ${klic}`); + if (!["adminChannel", "adminID", "DBPwd", "DBUser", "dieOnError", "ignoreMess", "ignorePresence", "ignoreSpink", "ignoreUnknownKeys", "noGeneralSync", "OnePlayToken", "prefix", "sachyDomena", "statPass", "token"].includes(klic)) throw new Error(`config.json obsahuje neznámý klíč: ${klic}`); } process.env = { ...process.env, ...konfig };