node-csfd-api
Version:
ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)
73 lines (71 loc) • 2.73 kB
JavaScript
const require_global_helper = require('./global.helper.js');
//#region src/helpers/creator.helper.ts
const getCreatorColorRating = (el) => {
const classes = el?.classNames.split(" ") ?? [];
const last = classes[classes.length - 1];
return require_global_helper.parseColor(last);
};
const getCreatorId = (url) => {
return url ? require_global_helper.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 ? require_global_helper.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
exports.getCreatorBio = getCreatorBio;
exports.getCreatorBirthdayInfo = getCreatorBirthdayInfo;
exports.getCreatorFilms = getCreatorFilms;
exports.getCreatorName = getCreatorName;
exports.getCreatorPhoto = getCreatorPhoto;
//# sourceMappingURL=creator.helper.js.map