node-csfd-api
Version:
ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)
219 lines (217 loc) • 7.69 kB
JavaScript
const require_global_helper = require('./global.helper.js');
//#region src/helpers/movie.helper.ts
/**
* Maps language-specific movie creator group labels.
* @param language - The language code (e.g., 'en', 'cs')
* @param key - The key of the creator group (e.g., 'directors', 'writers')
* @returns The localized label for the creator group
*/
const getLocalizedCreatorLabel = (language, key) => {
const labels = {
en: {
directors: "Directed by",
writers: "Screenplay",
cinematography: "Cinematography",
music: "Composer",
actors: "Cast",
basedOn: "Based on",
producers: "Produced by",
filmEditing: "Editing",
costumeDesign: "Costumes",
productionDesign: "Production design",
casting: "Casting",
sound: "Sound",
makeup: "Make-up"
},
cs: {
directors: "Režie",
writers: "Scénář",
cinematography: "Kamera",
music: "Hudba",
actors: "Hrají",
basedOn: "Předloha",
producers: "Produkce",
filmEditing: "Střih",
costumeDesign: "Kostýmy",
productionDesign: "Scénografie",
casting: "Casting",
sound: "Zvuk",
makeup: "Masky"
},
sk: {
directors: "Réžia",
writers: "Scenár",
cinematography: "Kamera",
music: "Hudba",
actors: "Hrajú",
basedOn: "Predloha",
producers: "Produkcia",
filmEditing: "Strih",
costumeDesign: "Kostýmy",
productionDesign: "Scénografia",
casting: "Casting",
sound: "Zvuk",
makeup: "Masky"
}
};
return (labels[language || "cs"] || labels["cs"])[key];
};
const getMovieTitle = (el) => {
return el.querySelector("h1").innerText.split(`(`)[0].trim();
};
const getMovieGenres = (el) => {
return el.querySelector(".genres").textContent.split(" / ");
};
const getMovieOrigins = (el) => {
return el.querySelector(".origin").textContent.split(",")[0].split(" / ");
};
const getMovieColorRating = (bodyClasses) => {
return require_global_helper.getColor(bodyClasses[1]);
};
const getMovieRating = (el) => {
const rating = el.querySelector(".film-rating-average").textContent?.replace(/%/g, "").trim();
const ratingInt = parseInt(rating);
if (Number.isInteger(ratingInt)) return ratingInt;
else return null;
};
const getMovieRatingCount = (el) => {
const ratingCount = +(el.querySelector(".box-rating-container .counter")?.textContent)?.replace(/[(\s)]/g, "");
if (Number.isInteger(ratingCount)) return ratingCount;
else return null;
};
const getMovieYear = (el) => {
try {
return +JSON.parse(el).dateCreated;
} catch (error) {
console.error("node-csfd-api: Error parsing JSON-LD", error);
return null;
}
};
const getMovieDuration = (jsonLdRaw, el) => {
let duration = null;
try {
duration = JSON.parse(jsonLdRaw).duration;
return require_global_helper.parseISO8601Duration(duration);
} catch (error) {
const timeString = el.querySelector(".origin").innerText.split(",");
if (timeString.length > 2) {
const hoursMins = timeString.pop().trim().split("(")[0].trim().split("min")[0].split("h");
duration = hoursMins.length > 1 ? +hoursMins[0] * 60 + +hoursMins[1] : +hoursMins[0];
return duration;
} else return null;
}
};
const getMovieTitlesOther = (el) => {
const namesNode = el.querySelectorAll(".film-names li");
if (!namesNode.length) return [];
return namesNode.map((el$1) => {
const country = el$1.querySelector("img.flag").attributes.alt;
const title = el$1.textContent.trim().split("\n")[0];
if (country && title) return {
country,
title
};
else return null;
}).filter((x) => x);
};
const getMoviePoster = (el) => {
const poster = el.querySelector(".film-posters img");
if (poster) if (poster.classNames?.includes("empty-image")) return null;
else return require_global_helper.addProtocol(poster.attributes.src.split("?")[0].replace(/\/w140\//, "/w1080/"));
else return null;
};
const getMovieRandomPhoto = (el) => {
const image = el.querySelector(".gallery-item picture img")?.attributes?.src;
if (image) return image.replace(/\/w663\//, "/w1326/");
else return null;
};
const getMovieTrivia = (el) => {
const triviaNodes = el.querySelectorAll(".article-trivia ul li");
if (triviaNodes?.length) return triviaNodes.map((node) => node.textContent.trim().replace(/(\r\n|\n|\r|\t)/gm, ""));
else return null;
};
const getMovieDescriptions = (el) => {
return el.querySelectorAll(".body--plots .plot-full p, .body--plots .plots .plots-item p").map((movie) => movie.textContent?.trim().replace(/(\r\n|\n|\r|\t)/gm, ""));
};
const parseMoviePeople = (el) => {
return el.querySelectorAll("a").filter((x) => x.classNames.length === 0).map((person) => {
return {
id: require_global_helper.parseIdFromUrl(person.attributes.href),
name: person.innerText.trim(),
url: `https://www.csfd.cz${person.attributes.href}`
};
});
};
const getMovieGroup = (el, group) => {
const element = el.querySelectorAll(".creators h4").filter((elem) => elem.textContent.trim().includes(group))[0];
if (element?.parentNode) return parseMoviePeople(element.parentNode);
else return [];
};
const getMovieType = (el) => {
return el.querySelector(".film-header-name .type")?.innerText?.replace(/[{()}]/g, "") || "film";
};
const getMovieVods = (el) => {
let vods = [];
if (el) vods = el.querySelectorAll(".box-buttons .button").filter((x) => !x.classNames.includes("button-social")).map((btn) => {
return {
title: btn.textContent.trim(),
url: btn.attributes.href
};
});
return vods.length ? vods : [];
};
const getBoxContent = (el, box) => {
return el.querySelectorAll("section.box .box-header").find((header) => header.querySelector("h3")?.textContent.trim().includes(box))?.parentNode;
};
const getMovieBoxMovies = (el, boxName) => {
const movieListItem = [];
const movieTitleNodes = getBoxContent(el, boxName)?.querySelectorAll(".article-header .film-title-name");
if (movieTitleNodes?.length) for (const item of movieTitleNodes) movieListItem.push({
id: require_global_helper.parseIdFromUrl(item.attributes.href),
title: item.textContent.trim(),
url: `https://www.csfd.cz${item.attributes.href}`
});
return movieListItem;
};
const getMoviePremieres = (el) => {
const premiereNodes = el.querySelectorAll(".box-premieres li");
const premiere = [];
for (const premiereNode of premiereNodes) {
const title = premiereNode.querySelector("p + span").attributes.title;
if (title) {
const [date, ...company] = title?.split(" ");
premiere.push({
country: premiereNode.querySelector(".flag")?.attributes.title || null,
format: premiereNode.querySelector("p").textContent.trim()?.split(" od")[0],
date,
company: company.join(" ")
});
}
}
return premiere;
};
const getMovieTags = (el) => {
return el.querySelectorAll(".box-content a[href*=\"/tag/\"]").map((tag) => tag.textContent);
};
//#endregion
exports.getLocalizedCreatorLabel = getLocalizedCreatorLabel;
exports.getMovieBoxMovies = getMovieBoxMovies;
exports.getMovieColorRating = getMovieColorRating;
exports.getMovieDescriptions = getMovieDescriptions;
exports.getMovieDuration = getMovieDuration;
exports.getMovieGenres = getMovieGenres;
exports.getMovieGroup = getMovieGroup;
exports.getMovieOrigins = getMovieOrigins;
exports.getMoviePoster = getMoviePoster;
exports.getMoviePremieres = getMoviePremieres;
exports.getMovieRandomPhoto = getMovieRandomPhoto;
exports.getMovieRating = getMovieRating;
exports.getMovieRatingCount = getMovieRatingCount;
exports.getMovieTags = getMovieTags;
exports.getMovieTitle = getMovieTitle;
exports.getMovieTitlesOther = getMovieTitlesOther;
exports.getMovieTrivia = getMovieTrivia;
exports.getMovieType = getMovieType;
exports.getMovieVods = getMovieVods;
exports.getMovieYear = getMovieYear;
//# sourceMappingURL=movie.helper.js.map