node-csfd-api
Version:
ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)
88 lines (87 loc) • 3.86 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilms = exports.parseBirthPlace = exports.parseAge = exports.parseBirthday = exports.getPhoto = exports.getBio = exports.getBirthdayInfo = exports.getName = exports.getId = exports.getColorRating = void 0;
const global_helper_1 = require("./global.helper");
const getColorRating = (el) => {
return (0, global_helper_1.parseColor)(el === null || el === void 0 ? void 0 : el.classNames.split(' ').pop());
};
exports.getColorRating = getColorRating;
const getId = (url) => {
if (url) {
return (0, global_helper_1.parseIdFromUrl)(url);
}
return null;
};
exports.getId = getId;
const getName = (el) => {
return el.querySelector('h1').innerText.trim();
};
exports.getName = getName;
const getBirthdayInfo = (el) => {
var _a, _b;
const infoBlock = el.querySelector('h1 + p');
const text = infoBlock === null || infoBlock === void 0 ? void 0 : infoBlock.innerHTML.trim();
const birthPlaceRow = (_a = infoBlock === null || infoBlock === void 0 ? void 0 : infoBlock.querySelector('.info-place')) === null || _a === void 0 ? void 0 : _a.innerHTML.trim();
const ageRow = (_b = infoBlock === null || infoBlock === void 0 ? void 0 : infoBlock.querySelector('.info')) === null || _b === void 0 ? void 0 : _b.innerHTML.trim();
let birthday = '';
if (text) {
const parts = text.split('\n');
const birthdayRow = parts.find((x) => x.includes('nar.'));
birthday = birthdayRow ? (0, exports.parseBirthday)(birthdayRow) : '';
}
const age = ageRow ? +(0, exports.parseAge)(ageRow) : null;
const birthPlace = birthPlaceRow ? (0, exports.parseBirthPlace)(birthPlaceRow) : '';
return { birthday, age, birthPlace };
};
exports.getBirthdayInfo = getBirthdayInfo;
const getBio = (el) => {
var _a;
return ((_a = el.querySelector('.article-content p')) === null || _a === void 0 ? void 0 : _a.text.trim().split('\n')[0].trim()) || null;
};
exports.getBio = getBio;
const getPhoto = (el) => {
const image = el.querySelector('img').attributes.src;
return (0, global_helper_1.addProtocol)(image);
};
exports.getPhoto = getPhoto;
const parseBirthday = (text) => {
return text.replace(/nar./g, '').trim();
};
exports.parseBirthday = parseBirthday;
const parseAge = (text) => {
return text.trim().replace(/\(/g, '').replace(/let\)/g, '').trim();
};
exports.parseAge = parseAge;
const parseBirthPlace = (text) => {
return text.trim().replace(/<br>/g, '').trim();
};
exports.parseBirthPlace = parseBirthPlace;
const getFilms = (el) => {
var _a;
const filmNodes = (_a = el.querySelectorAll('.box')[0]) === null || _a === void 0 ? void 0 : _a.querySelectorAll('table tr');
let yearCache;
const films = filmNodes.map((filmNode) => {
var _a, _b, _c;
const id = (0, exports.getId)((_a = filmNode.querySelector('td.name .film-title-name')) === null || _a === void 0 ? void 0 : _a.attributes.href);
const title = (_b = filmNode.querySelector('.name')) === null || _b === void 0 ? void 0 : _b.text.trim();
const year = +((_c = filmNode.querySelector('.year')) === null || _c === void 0 ? void 0 : _c.text.trim());
const colorRating = (0, exports.getColorRating)(filmNode.querySelector('.name .icon'));
// Cache year from previous film because there is a gap between movies with same year
if (year) {
yearCache = +year;
}
if (id && title) {
return {
id,
title,
year: year || yearCache,
colorRating
};
}
return {};
});
// Remove empty objects
const filmsUnique = films.filter((value) => Object.keys(value).length !== 0);
return filmsUnique;
};
exports.getFilms = getFilms;
;