UNPKG

node-csfd-api-racintom

Version:

ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)

85 lines (84 loc) 3.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserRatingsScraper = void 0; const node_html_parser_1 = require("node-html-parser"); const fetchers_1 = require("../fetchers"); const user_ratings_helper_1 = require("../helpers/user-ratings.helper"); const vars_1 = require("../vars"); class UserRatingsScraper { constructor() { this.films = []; } async userRatings(user, config) { let allMovies = []; const url = (0, vars_1.userRatingsUrl)(user); const response = await (0, fetchers_1.fetchPage)(url); const items = (0, node_html_parser_1.parse)(response); const movies = items.querySelectorAll('.box-user-rating .table-container tbody tr'); // Get number of pages const pagesNode = items.querySelector('.pagination'); const pages = +(pagesNode === null || pagesNode === void 0 ? void 0 : pagesNode.childNodes[pagesNode.childNodes.length - 4].rawText) || 1; allMovies = this.getPage(config, movies); if (config === null || config === void 0 ? void 0 : config.allPages) { console.log('User', user, url); console.log('Fetching all pages', pages); for (let i = 2; i <= pages; i++) { console.log('Fetching page', i, 'out of', pages, '...'); const url = (0, vars_1.userRatingsUrl)(user, i); const response = await (0, fetchers_1.fetchPage)(url); const items = (0, node_html_parser_1.parse)(response); const movies = items.querySelectorAll('.box-user-rating .table-container tbody tr'); allMovies = [...this.getPage(config, movies)]; // Sleep if (config.allPagesDelay) { await (0, user_ratings_helper_1.sleep)(config.allPagesDelay); } } return allMovies; } return allMovies; } getPage(config, movies) { var _a, _b, _c, _d; if (config) { if (((_a = config.includesOnly) === null || _a === void 0 ? void 0 : _a.length) && ((_b = config.excludes) === null || _b === void 0 ? void 0 : _b.length)) { console.warn(`node-csfd-api: You can not use both parameters 'includesOnly' and 'excludes'. Parameter 'includesOnly' will be used now:`, config.includesOnly); } } for (const el of movies) { const type = (0, user_ratings_helper_1.getType)(el); // Filtering includesOnly if ((_c = config === null || config === void 0 ? void 0 : config.includesOnly) === null || _c === void 0 ? void 0 : _c.length) { if (config.includesOnly.some((include) => type === include)) { this.buildUserRatings(el); } // Filter exludes } else if ((_d = config === null || config === void 0 ? void 0 : config.excludes) === null || _d === void 0 ? void 0 : _d.length) { if (!config.excludes.some((exclude) => type === exclude)) { this.buildUserRatings(el); } } else { // Without filtering this.buildUserRatings(el); } } return this.films; } buildUserRatings(el) { this.films.push({ id: (0, user_ratings_helper_1.getId)(el), title: (0, user_ratings_helper_1.getTitle)(el), year: (0, user_ratings_helper_1.getYear)(el), type: (0, user_ratings_helper_1.getType)(el), url: (0, user_ratings_helper_1.getUrl)(el), colorRating: (0, user_ratings_helper_1.getColorRating)(el), userDate: (0, user_ratings_helper_1.getDate)(el), userRating: (0, user_ratings_helper_1.getUserRating)(el) }); } } exports.UserRatingsScraper = UserRatingsScraper;