node-csfd-api
Version:
ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)
62 lines (61 loc) • 2.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = exports.parseColor = exports.getUrl = exports.getDate = exports.getColorRating = exports.getYear = exports.getTitle = exports.getType = exports.getUserRating = exports.getId = void 0;
const global_helper_1 = require("./global.helper");
const getId = (el) => {
const url = el.querySelector('td.name .film-title-name').attributes.href;
return (0, global_helper_1.parseIdFromUrl)(url);
};
exports.getId = getId;
const getUserRating = (el) => {
const ratingText = el.querySelector('td.star-rating-only .stars').classNames.split(' ').pop();
const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
return rating;
};
exports.getUserRating = getUserRating;
const getType = (el) => {
const typeText = el.querySelectorAll('td.name .film-title-info .info');
return (typeText.length > 1 ? typeText[1].text.slice(1, -1) : 'film');
};
exports.getType = getType;
const getTitle = (el) => {
return el.querySelector('td.name .film-title-name').text;
};
exports.getTitle = getTitle;
const getYear = (el) => {
var _a;
return +((_a = el.querySelectorAll('td.name .film-title-info .info')[0]) === null || _a === void 0 ? void 0 : _a.text.slice(1, -1)) || null;
};
exports.getYear = getYear;
const getColorRating = (el) => {
const color = (0, exports.parseColor)(el.querySelector('td.name .icon').classNames.split(' ').pop());
return color;
};
exports.getColorRating = getColorRating;
const getDate = (el) => {
return el.querySelector('td.date-only').text.trim();
};
exports.getDate = getDate;
const getUrl = (el) => {
const url = el.querySelector('td.name .film-title-name').attributes.href;
return `https://www.csfd.cz${url}`;
};
exports.getUrl = getUrl;
const parseColor = (quality) => {
switch (quality) {
case 'lightgrey':
return 'unknown';
case 'red':
return 'good';
case 'blue':
return 'average';
case 'grey':
return 'bad';
default:
return 'unknown';
}
};
exports.parseColor = parseColor;
// Sleep in loop
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
exports.sleep = sleep;
;