node-csfd-api
Version:
ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)
106 lines (105 loc) • 4.16 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseMeta = exports.getFilms = exports.getGroupedFilmsByDate = exports.parseCinema = exports.getCinemaUrl = exports.getCoords = exports.getId = exports.getCinemaId = 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 getCinemaId = (el) => {
var _a;
const id = (_a = el === null || el === void 0 ? void 0 : el.id) === null || _a === void 0 ? void 0 : _a.split('-')[1];
return +id;
};
exports.getCinemaId = getCinemaId;
const getId = (url) => {
if (url) {
return (0, global_helper_1.parseIdFromUrl)(url);
}
return null;
};
exports.getId = getId;
const getCoords = (el) => {
if (!el)
return null;
const linkMapsEl = el.querySelector('a[href*="q="]');
if (!linkMapsEl)
return null;
const linkMaps = linkMapsEl.getAttribute('href');
const [_, latLng] = linkMaps.split('q=');
const coords = latLng.split(',');
if (coords.length !== 2)
return null;
const lat = Number(coords[0]);
const lng = Number(coords[1]);
if (Number.isFinite(lat) && Number.isFinite(lng)) {
return { lat, lng };
}
return null;
};
exports.getCoords = getCoords;
const getCinemaUrl = (el) => {
var _a, _b;
if (!el)
return '';
return (_b = (_a = el.querySelector('a[title="Přejít na webovou stránku kina"]')) === null || _a === void 0 ? void 0 : _a.attributes.href) !== null && _b !== void 0 ? _b : '';
};
exports.getCinemaUrl = getCinemaUrl;
const parseCinema = (el) => {
const title = el.querySelector('.box-header h2').innerText.trim();
const [city, name] = title.split(' - ');
return { city, name };
};
exports.parseCinema = parseCinema;
const getGroupedFilmsByDate = (el) => {
const divs = el.querySelectorAll(':scope > div');
const getDatesAndFilms = divs
.map((_, index) => index)
.filter((index) => index % 2 === 0)
.map((index) => {
var _a, _b, _c;
const [date, films] = divs.slice(index, index + 2);
const dateText = (_c = (_b = (_a = date === null || date === void 0 ? void 0 : date.firstChild) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : null;
return { date: dateText, films: (0, exports.getFilms)('', films) };
});
return getDatesAndFilms;
};
exports.getGroupedFilmsByDate = getGroupedFilmsByDate;
const getFilms = (date, el) => {
const filmNodes = el.querySelectorAll('.cinema-table tr');
const films = filmNodes.map((filmNode) => {
var _a, _b, _c, _d;
const url = (_a = filmNode.querySelector('td.name h3 a')) === null || _a === void 0 ? void 0 : _a.attributes.href;
const id = (0, exports.getId)(url);
const title = (_b = filmNode.querySelector('.name h3')) === null || _b === void 0 ? void 0 : _b.text.trim();
const colorRating = (0, exports.getColorRating)(filmNode.querySelector('.name .icon'));
const showTimes = (_c = filmNode.querySelectorAll('.td-time')) === null || _c === void 0 ? void 0 : _c.map((x) => x.textContent.trim());
const meta = (_d = filmNode.querySelectorAll('.td-title span')) === null || _d === void 0 ? void 0 : _d.map((x) => x.text.trim());
return {
id,
title,
url,
colorRating,
showTimes,
meta: (0, exports.parseMeta)(meta)
};
});
return films;
};
exports.getFilms = getFilms;
const parseMeta = (meta) => {
const metaConvert = [];
for (const element of meta) {
if (element === 'T') {
metaConvert.push('subtitles');
}
else if (element === 'D') {
metaConvert.push('dubbing');
}
else {
metaConvert.push(element);
}
}
return metaConvert;
};
exports.parseMeta = parseMeta;
;