kinoklub-api
Version:
Support library for KinoKlub
123 lines (122 loc) • 5.05 kB
JavaScript
import { resolveCinema, resolveDate } from "./global.helper.js";
import { NodeType } from "node-html-parser";
//#region src/helpers/movie.helper.ts
const getTitle = (el) => {
return el.querySelector("h3").textContent.trim();
};
const getInfo = (el) => {
return el.querySelector("h6.show-on-mobile")?.textContent?.replace(/(\r\n|\n|\r|\t)/gm, "")?.trim()?.split("/")?.map((x) => x.trim());
};
const getDescription = (el) => {
const container = el.querySelector(".modal-body__mobile-bottom");
if (!container) return "";
const cleanedText = (text) => text.replace(/(\r\n|\n|\r|\t)/gm, "").trim();
return container.childNodes.filter((node) => node.nodeType === NodeType.ELEMENT_NODE).map((node) => node).filter((node) => !node.classNames.includes("modal-body__buttons") && !node.classNames.includes("modal-body__projections")).flatMap((node) => {
if (node.tagName === "P") return [cleanedText(node.textContent)];
const nestedParagraphs = node.querySelectorAll("p").map((child) => cleanedText(child.textContent)).filter(Boolean);
if (nestedParagraphs.length) return nestedParagraphs;
const directDivChildren = node.childNodes.filter((child) => child.nodeType === NodeType.ELEMENT_NODE).map((child) => child).filter((child) => child.tagName === "DIV").map((child) => cleanedText(child.textContent)).filter(Boolean);
if (directDivChildren.length) return directDivChildren;
return [cleanedText(node.textContent)].filter(Boolean);
}).join("\n");
};
const getButton = (el, name) => {
const buttons = el.querySelectorAll(".modal-body__buttons a");
const separator = name === "IMDB" ? "title" : "film";
const button = buttons.find((x) => x.textContent.trim() === name);
if (button) {
const link = button.attributes.href;
const parts = link.split("/");
return {
id: parts[parts.findIndex((el) => el === separator) + 1],
link
};
} else return {
id: null,
link: null
};
};
const getTags = (el) => {
return el.querySelectorAll(".modal-body__tags.show-on-mobile span").map((x) => x.textContent.trim());
};
const getYoutube = (el) => {
const id = el.querySelector("#trailer-button")?.attributes["data-youtube-id"];
return id ? "https://youtu.be/" + id : null;
};
const getImage = (el) => {
const id = el.querySelector(".modal-body__image img").attributes.src;
return id ? "https://www.kinoaero.cz" + id : null;
};
const pickInfo = (info, parseString) => {
const index = info.findIndex((x) => x.includes(parseString));
const pickedInfo = info[index];
if (index !== -1 && pickedInfo) return {
text: pickedInfo.split(parseString)[1].trim(),
index
};
return null;
};
const getTitleOriginal = (info) => {
return pickInfo(info, "Originální název:") || pickInfo(info, "Original title:") || {
text: null,
index: null
};
};
const getDirector = (info) => {
return pickInfo(info, "režie:") || pickInfo(info, "director:") || {
text: null,
index: null
};
};
const getLanguage = (info) => {
return pickInfo(info, "znění:") || pickInfo(info, "language:") || {
text: null,
index: null
};
};
const getSubtitles = (info) => {
return pickInfo(info, "titulky:") || pickInfo(info, "subtitles:") || {
text: null,
index: null
};
};
const getCurrentProjection = (el) => {
return parseProjection(el.querySelector(".modal-body__projection-row--current"));
};
const removeIndexesFromArray = (arr, indexes) => {
const indexSet = new Set(indexes);
return arr.filter((value, i) => !indexSet.has(i));
};
const parseProjection = (el) => {
if (el) {
const cinemaName = el.querySelector(".modal-body__projection-cinema span")?.textContent;
const cinema = resolveCinema(cinemaName);
const hall = getMovieHall(el);
const idString = el.querySelector(".modal-body__projection-calendar")?.getAttribute("href")?.split("/").pop();
const id = idString && !isNaN(Number(idString)) ? Number(idString) : null;
const day = el.querySelector(".modal-body__projection-day")?.textContent?.replace(/(\r\n|\n|\r|\t)/gm, "").trim();
const time = el.querySelector(".modal-body__projection-time")?.textContent?.replace(/(\r\n|\n|\r|\t)/gm, "").trim();
return {
id,
cinema,
date: resolveDate(day, time),
price: el.querySelector("button.modal-body__projection-price")?.textContent?.replace(/(\r\n|\n|\r|\t)/gm, "").trim() ?? null,
hall
};
} else return null;
};
const getProjections = (el) => {
return el.querySelectorAll(".modal-body__left > .modal-body__projections .modal-body__projection-row").map((el) => parseProjection(el));
};
const getMovieHall = (el) => {
const cinemaEl = el.querySelector(".modal-body__projection-cinema");
if (!cinemaEl) return null;
for (const node of cinemaEl.childNodes) if (node.nodeType === NodeType.TEXT_NODE) {
const text = node.textContent?.trim();
if (text) return text;
}
return null;
};
//#endregion
export { getButton, getCurrentProjection, getDescription, getDirector, getImage, getInfo, getLanguage, getProjections, getSubtitles, getTags, getTitle, getTitleOriginal, getYoutube, removeIndexesFromArray };
//# sourceMappingURL=movie.helper.js.map