UNPKG

node-csfd-api

Version:

ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)

69 lines (67 loc) 2.56 kB
import { addProtocol, parseColor, parseIdFromUrl } from "./global.helper.mjs"; //#region src/helpers/creator.helper.ts const getCreatorColorRating = (el) => { const classes = el?.classNames.split(" ") ?? []; const last = classes[classes.length - 1]; return parseColor(last); }; const getCreatorId = (url) => { return url ? parseIdFromUrl(url) : null; }; const getCreatorName = (el) => { return (el?.querySelector("h1"))?.innerText?.trim() ?? null; }; const getCreatorBirthdayInfo = (el) => { const infoBlock = el.querySelector("h1 + p"); const text = infoBlock?.innerHTML.trim(); const birthPlaceRow = infoBlock?.querySelector(".info-place")?.innerHTML.trim(); const ageRow = infoBlock?.querySelector(".info")?.innerHTML.trim(); let birthday = ""; if (text) { const birthdayRow = text.split("\n").find((x) => x.includes("nar.")); birthday = birthdayRow ? parseBirthday(birthdayRow) : ""; } const age = ageRow ? +parseAge(ageRow) : null; const birthPlace = birthPlaceRow ? parseBirthPlace(birthPlaceRow) : ""; return { birthday, age, birthPlace }; }; const getCreatorBio = (el) => { return (el?.querySelector(".article-content p"))?.text?.trim().split("\n")[0]?.trim() || null; }; const getCreatorPhoto = (el) => { const src = el?.querySelector("img")?.getAttribute("src"); return src ? addProtocol(src) : null; }; const parseBirthday = (text) => text.replace(/nar\./g, "").trim(); const parseAge = (text) => { const digits = text.replace(/[^\d]/g, ""); return digits ? Number(digits) : null; }; const parseBirthPlace = (text) => text.trim().replace(/<br>/g, "").trim(); const getCreatorFilms = (el) => { const filmNodes = el?.querySelectorAll(".box")?.[0]?.querySelectorAll("table tr") ?? []; let yearCache = null; return filmNodes.map((filmNode) => { const id = getCreatorId(filmNode.querySelector("td.name .film-title-name")?.attributes?.href); const title = filmNode.querySelector(".name")?.text?.trim(); const yearText = filmNode.querySelector(".year")?.text?.trim(); const year = yearText ? +yearText : null; const colorRating = getCreatorColorRating(filmNode.querySelector(".name .icon")); if (typeof year === "number" && !isNaN(year)) yearCache = +year; const finalYear = year ?? yearCache; if (id != null && title && finalYear != null) return { id, title, year: finalYear, colorRating }; return null; }).filter(Boolean); }; //#endregion export { getCreatorBio, getCreatorBirthdayInfo, getCreatorFilms, getCreatorName, getCreatorPhoto }; //# sourceMappingURL=creator.helper.mjs.map