node-csfd-api 3.0.0-next.21 → 3.0.0-next.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/esm/fetchers/fetch.polyfill.js +6 -0
  2. package/esm/fetchers/index.js +23 -0
  3. package/esm/helpers/cinema.helper.js +93 -0
  4. package/esm/helpers/creator.helper.js +74 -0
  5. package/esm/helpers/global.helper.js +59 -0
  6. package/esm/helpers/movie.helper.js +228 -0
  7. package/esm/helpers/search-user.helper.js +15 -0
  8. package/esm/helpers/search.helper.js +51 -0
  9. package/esm/helpers/user-ratings.helper.js +48 -0
  10. package/esm/index.js +35 -0
  11. package/esm/interfaces/cinema.interface.js +1 -0
  12. package/esm/interfaces/creator.interface.js +1 -0
  13. package/esm/interfaces/global.js +1 -0
  14. package/esm/interfaces/movie.interface.js +1 -0
  15. package/esm/interfaces/search.interface.js +1 -0
  16. package/esm/interfaces/user-ratings.interface.js +1 -0
  17. package/esm/services/cinema.service.js +30 -0
  18. package/esm/services/creator.service.js +28 -0
  19. package/esm/services/movie.service.js +54 -0
  20. package/esm/services/search.service.js +76 -0
  21. package/esm/services/user-ratings.service.js +80 -0
  22. package/esm/types/fetchers/fetch.polyfill.d.ts +1 -0
  23. package/esm/types/fetchers/index.d.ts +1 -0
  24. package/esm/types/helpers/cinema.helper.d.ts +18 -0
  25. package/esm/types/helpers/creator.helper.d.ts +17 -0
  26. package/esm/types/helpers/global.helper.d.ts +17 -0
  27. package/esm/types/helpers/movie.helper.d.ts +25 -0
  28. package/esm/types/helpers/search-user.helper.d.ts +5 -0
  29. package/esm/types/helpers/search.helper.d.ts +11 -0
  30. package/esm/types/helpers/user-ratings.helper.d.ts +13 -0
  31. package/esm/types/index.d.ts +24 -0
  32. package/esm/types/interfaces/cinema.interface.d.ts +23 -0
  33. package/esm/types/interfaces/creator.interface.d.ts +12 -0
  34. package/esm/types/interfaces/global.d.ts +22 -0
  35. package/esm/types/interfaces/movie.interface.d.ts +66 -0
  36. package/esm/types/interfaces/search.interface.d.ts +27 -0
  37. package/esm/types/interfaces/user-ratings.interface.d.ts +18 -0
  38. package/esm/types/services/cinema.service.d.ts +6 -0
  39. package/esm/types/services/creator.service.d.ts +6 -0
  40. package/esm/types/services/movie.service.d.ts +6 -0
  41. package/esm/types/services/search.service.d.ts +5 -0
  42. package/esm/types/services/user-ratings.service.d.ts +7 -0
  43. package/esm/types/vars.d.ts +6 -0
  44. package/esm/vars.js +7 -0
  45. package/helpers/cinema.helper.d.ts +2 -3
  46. package/helpers/cinema.helper.js +6 -6
  47. package/index.d.ts +1 -1
  48. package/package.json +14 -6
  49. package/services/cinema.service.js +4 -4
  50. package/services/search.service.js +3 -3
@@ -0,0 +1,6 @@
1
+ // Check if `fetch` is available in global scope (nodejs 18+) or in window (browser). If not, use cross-fetch polyfill.
2
+ import { fetch as crossFetch } from 'cross-fetch';
3
+ export const fetchSafe = (typeof fetch === 'function' && fetch) || // ServiceWorker fetch (Cloud Functions + Chrome extension)
4
+ (typeof global === 'object' && global.fetch) || // Node.js 18+ fetch
5
+ (typeof window !== 'undefined' && window.fetch) || // Browser fetch
6
+ crossFetch; // Polyfill fetch
@@ -0,0 +1,23 @@
1
+ import { fetchSafe } from './fetch.polyfill';
2
+ const USER_AGENTS = [
3
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
4
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.77 Mobile/15E148 Safari/604.1',
5
+ 'Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Mobile Safari/537.36',
6
+ 'Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Mobile Safari/537.36'
7
+ ];
8
+ const headers = {
9
+ 'User-Agent': USER_AGENTS[Math.floor(Math.random() * USER_AGENTS.length)]
10
+ };
11
+ export const fetchPage = async (url) => {
12
+ try {
13
+ const response = await fetchSafe(url, { headers });
14
+ if (response.status >= 400 && response.status < 600) {
15
+ throw new Error(`node-csfd-api: Bad response ${response.status} for url: ${url}`);
16
+ }
17
+ return await response.text();
18
+ }
19
+ catch (e) {
20
+ console.error(e);
21
+ return 'Error';
22
+ }
23
+ };
@@ -0,0 +1,93 @@
1
+ import { parseColor, parseIdFromUrl } from './global.helper';
2
+ export const getColorRating = (el) => {
3
+ return parseColor(el === null || el === void 0 ? void 0 : el.classNames.split(' ').pop());
4
+ };
5
+ export const getCinemaId = (el) => {
6
+ var _a;
7
+ const id = (_a = el === null || el === void 0 ? void 0 : el.id) === null || _a === void 0 ? void 0 : _a.split('-')[1];
8
+ return +id;
9
+ };
10
+ export const getId = (url) => {
11
+ if (url) {
12
+ return parseIdFromUrl(url);
13
+ }
14
+ return null;
15
+ };
16
+ export const getCoords = (el) => {
17
+ if (!el)
18
+ return null;
19
+ const linkMapsEl = el.querySelector('a[href*="q="]');
20
+ if (!linkMapsEl)
21
+ return null;
22
+ const linkMaps = linkMapsEl.getAttribute('href');
23
+ const [_, latLng] = linkMaps.split('q=');
24
+ const coords = latLng.split(',');
25
+ if (coords.length !== 2)
26
+ return null;
27
+ const lat = Number(coords[0]);
28
+ const lng = Number(coords[1]);
29
+ if (Number.isFinite(lat) && Number.isFinite(lng)) {
30
+ return { lat, lng };
31
+ }
32
+ return null;
33
+ };
34
+ export const getCinemaUrl = (el) => {
35
+ var _a, _b;
36
+ if (!el)
37
+ return '';
38
+ 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 : '';
39
+ };
40
+ export const parseCinema = (el) => {
41
+ const title = el.querySelector('.box-header h2').innerText.trim();
42
+ const [city, name] = title.split(' - ');
43
+ return { city, name };
44
+ };
45
+ export const getGroupedFilmsByDate = (el) => {
46
+ const divs = el.querySelectorAll(':scope > div');
47
+ const getDatesAndFilms = divs
48
+ .map((_, index) => index)
49
+ .filter((index) => index % 2 === 0)
50
+ .map((index) => {
51
+ var _a, _b, _c;
52
+ const [date, films] = divs.slice(index, index + 2);
53
+ 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;
54
+ return { date: dateText, films: getFilms('', films) };
55
+ });
56
+ return getDatesAndFilms;
57
+ };
58
+ export const getFilms = (date, el) => {
59
+ const filmNodes = el.querySelectorAll('.cinema-table tr');
60
+ const films = filmNodes.map((filmNode) => {
61
+ var _a, _b, _c, _d;
62
+ const url = (_a = filmNode.querySelector('td.name h3 a')) === null || _a === void 0 ? void 0 : _a.attributes.href;
63
+ const id = getId(url);
64
+ const title = (_b = filmNode.querySelector('.name h3')) === null || _b === void 0 ? void 0 : _b.text.trim();
65
+ const colorRating = getColorRating(filmNode.querySelector('.name .icon'));
66
+ const showTimes = (_c = filmNode.querySelectorAll('.td-time')) === null || _c === void 0 ? void 0 : _c.map((x) => x.textContent.trim());
67
+ const meta = (_d = filmNode.querySelectorAll('.td-title span')) === null || _d === void 0 ? void 0 : _d.map((x) => x.text.trim());
68
+ return {
69
+ id,
70
+ title,
71
+ url,
72
+ colorRating,
73
+ showTimes,
74
+ meta: parseMeta(meta)
75
+ };
76
+ });
77
+ return films;
78
+ };
79
+ export const parseMeta = (meta) => {
80
+ const metaConvert = [];
81
+ for (const element of meta) {
82
+ if (element === 'T') {
83
+ metaConvert.push('subtitles');
84
+ }
85
+ else if (element === 'D') {
86
+ metaConvert.push('dubbing');
87
+ }
88
+ else {
89
+ metaConvert.push(element);
90
+ }
91
+ }
92
+ return metaConvert;
93
+ };
@@ -0,0 +1,74 @@
1
+ import { addProtocol, parseColor, parseIdFromUrl } from './global.helper';
2
+ export const getColorRating = (el) => {
3
+ return parseColor(el === null || el === void 0 ? void 0 : el.classNames.split(' ').pop());
4
+ };
5
+ export const getId = (url) => {
6
+ if (url) {
7
+ return parseIdFromUrl(url);
8
+ }
9
+ return null;
10
+ };
11
+ export const getName = (el) => {
12
+ return el.querySelector('h1').innerText.trim();
13
+ };
14
+ export const getBirthdayInfo = (el) => {
15
+ var _a, _b;
16
+ const infoBlock = el.querySelector('h1 + p');
17
+ const text = infoBlock === null || infoBlock === void 0 ? void 0 : infoBlock.innerHTML.trim();
18
+ const birthPlaceRow = (_a = infoBlock === null || infoBlock === void 0 ? void 0 : infoBlock.querySelector('.info-place')) === null || _a === void 0 ? void 0 : _a.innerHTML.trim();
19
+ const ageRow = (_b = infoBlock === null || infoBlock === void 0 ? void 0 : infoBlock.querySelector('.info')) === null || _b === void 0 ? void 0 : _b.innerHTML.trim();
20
+ let birthday = '';
21
+ if (text) {
22
+ const parts = text.split('\n');
23
+ const birthdayRow = parts.find((x) => x.includes('nar.'));
24
+ birthday = birthdayRow ? parseBirthday(birthdayRow) : '';
25
+ }
26
+ const age = ageRow ? +parseAge(ageRow) : null;
27
+ const birthPlace = birthPlaceRow ? parseBirthPlace(birthPlaceRow) : '';
28
+ return { birthday, age, birthPlace };
29
+ };
30
+ export const getBio = (el) => {
31
+ var _a;
32
+ return ((_a = el.querySelector('.article-content p')) === null || _a === void 0 ? void 0 : _a.text.trim().split('\n')[0].trim()) || null;
33
+ };
34
+ export const getPhoto = (el) => {
35
+ const image = el.querySelector('img').attributes.src;
36
+ return addProtocol(image);
37
+ };
38
+ export const parseBirthday = (text) => {
39
+ return text.replace(/nar./g, '').trim();
40
+ };
41
+ export const parseAge = (text) => {
42
+ return text.trim().replace(/\(/g, '').replace(/let\)/g, '').trim();
43
+ };
44
+ export const parseBirthPlace = (text) => {
45
+ return text.trim().replace(/<br>/g, '').trim();
46
+ };
47
+ export const getFilms = (el) => {
48
+ var _a;
49
+ const filmNodes = (_a = el.querySelectorAll('.box')[0]) === null || _a === void 0 ? void 0 : _a.querySelectorAll('table tr');
50
+ let yearCache;
51
+ const films = filmNodes.map((filmNode) => {
52
+ var _a, _b, _c;
53
+ const id = getId((_a = filmNode.querySelector('td.name .film-title-name')) === null || _a === void 0 ? void 0 : _a.attributes.href);
54
+ const title = (_b = filmNode.querySelector('.name')) === null || _b === void 0 ? void 0 : _b.text.trim();
55
+ const year = +((_c = filmNode.querySelector('.year')) === null || _c === void 0 ? void 0 : _c.text.trim());
56
+ const colorRating = getColorRating(filmNode.querySelector('.name .icon'));
57
+ // Cache year from previous film because there is a gap between movies with same year
58
+ if (year) {
59
+ yearCache = +year;
60
+ }
61
+ if (id && title) {
62
+ return {
63
+ id,
64
+ title,
65
+ year: year || yearCache,
66
+ colorRating
67
+ };
68
+ }
69
+ return {};
70
+ });
71
+ // Remove empty objects
72
+ const filmsUnique = films.filter((value) => Object.keys(value).length !== 0);
73
+ return filmsUnique;
74
+ };
@@ -0,0 +1,59 @@
1
+ export const parseIdFromUrl = (url) => {
2
+ if (url) {
3
+ const idSlug = url === null || url === void 0 ? void 0 : url.split('/')[2];
4
+ const id = idSlug === null || idSlug === void 0 ? void 0 : idSlug.split('-')[0];
5
+ return +id || null;
6
+ }
7
+ else {
8
+ return null;
9
+ }
10
+ };
11
+ export const getColor = (cls) => {
12
+ switch (cls) {
13
+ case 'page-lightgrey':
14
+ return 'unknown';
15
+ case 'page-red':
16
+ return 'good';
17
+ case 'page-blue':
18
+ return 'average';
19
+ case 'page-grey':
20
+ return 'bad';
21
+ default:
22
+ return 'unknown';
23
+ }
24
+ };
25
+ export const parseColor = (quality) => {
26
+ switch (quality) {
27
+ case 'lightgrey':
28
+ return 'unknown';
29
+ case 'red':
30
+ return 'good';
31
+ case 'blue':
32
+ return 'average';
33
+ case 'grey':
34
+ return 'bad';
35
+ default:
36
+ return 'unknown';
37
+ }
38
+ };
39
+ export const addProtocol = (url) => {
40
+ return url.startsWith('//') ? 'https:' + url : url;
41
+ };
42
+ export const getDuration = (matches) => {
43
+ return {
44
+ sign: matches[1] === undefined ? '+' : '-',
45
+ years: matches[2] === undefined ? 0 : matches[2],
46
+ months: matches[3] === undefined ? 0 : matches[3],
47
+ weeks: matches[4] === undefined ? 0 : matches[4],
48
+ days: matches[5] === undefined ? 0 : matches[5],
49
+ hours: matches[6] === undefined ? 0 : matches[6],
50
+ minutes: matches[7] === undefined ? 0 : matches[7],
51
+ seconds: matches[8] === undefined ? 0 : matches[8]
52
+ };
53
+ };
54
+ export const parseISO8601Duration = (iso) => {
55
+ const iso8601DurationRegex = /(-)?P(?:([.,\d]+)Y)?(?:([.,\d]+)M)?(?:([.,\d]+)W)?(?:([.,\d]+)D)?T(?:([.,\d]+)H)?(?:([.,\d]+)M)?(?:([.,\d]+)S)?/;
56
+ const matches = iso.match(iso8601DurationRegex);
57
+ const duration = getDuration(matches);
58
+ return +duration.minutes;
59
+ };
@@ -0,0 +1,228 @@
1
+ import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl } from './global.helper';
2
+ export const getId = (el) => {
3
+ const url = el.querySelector('.tabs .tab-nav-list a').attributes.href;
4
+ return parseIdFromUrl(url);
5
+ };
6
+ export const getTitle = (el) => {
7
+ return el.querySelector('h1').innerText.split(`(`)[0].trim();
8
+ };
9
+ export const getGenres = (el) => {
10
+ const genresRaw = el.querySelector('.genres').textContent;
11
+ return genresRaw.split(' / ');
12
+ };
13
+ export const getOrigins = (el) => {
14
+ const originsRaw = el.querySelector('.origin').textContent;
15
+ const origins = originsRaw.split(',')[0];
16
+ return origins.split(' / ');
17
+ };
18
+ export const getColorRating = (bodyClasses) => {
19
+ return getColor(bodyClasses[1]);
20
+ };
21
+ export const getRating = (el) => {
22
+ const ratingRaw = el.querySelector('.film-rating-average').textContent;
23
+ const rating = ratingRaw === null || ratingRaw === void 0 ? void 0 : ratingRaw.replace(/%/g, '').trim();
24
+ const ratingInt = parseInt(rating);
25
+ if (Number.isInteger(ratingInt)) {
26
+ return ratingInt;
27
+ }
28
+ else {
29
+ return null;
30
+ }
31
+ };
32
+ export const getRatingCount = (el) => {
33
+ var _a;
34
+ const ratingCountRaw = (_a = el.querySelector('.box-rating-container .counter')) === null || _a === void 0 ? void 0 : _a.textContent;
35
+ const ratingCount = +(ratingCountRaw === null || ratingCountRaw === void 0 ? void 0 : ratingCountRaw.replace(/[(\s)]/g, ''));
36
+ if (Number.isInteger(ratingCount)) {
37
+ return ratingCount;
38
+ }
39
+ else {
40
+ return null;
41
+ }
42
+ };
43
+ export const getYear = (el) => {
44
+ try {
45
+ const jsonLd = JSON.parse(el);
46
+ return +jsonLd.dateCreated;
47
+ }
48
+ catch (error) {
49
+ console.error('node-csfd-api: Error parsing JSON-LD', error);
50
+ return null;
51
+ }
52
+ };
53
+ export const getDuration = (jsonLdRaw, el) => {
54
+ let duration = null;
55
+ try {
56
+ const jsonLd = JSON.parse(jsonLdRaw);
57
+ duration = jsonLd.duration;
58
+ return parseISO8601Duration(duration);
59
+ }
60
+ catch (error) {
61
+ const origin = el.querySelector('.origin').innerText;
62
+ const timeString = origin.split(',');
63
+ if (timeString.length > 2) {
64
+ // Get last time elelment
65
+ const timeString2 = timeString.pop().trim();
66
+ // Clean it
67
+ const timeRaw = timeString2.split('(')[0].trim();
68
+ // Split by minutes and hours
69
+ const hoursMinsRaw = timeRaw.split('min')[0];
70
+ const hoursMins = hoursMinsRaw.split('h');
71
+ // Resolve hours + minutes format
72
+ duration = hoursMins.length > 1 ? +hoursMins[0] * 60 + +hoursMins[1] : +hoursMins[0];
73
+ return duration;
74
+ }
75
+ else {
76
+ return null;
77
+ }
78
+ }
79
+ };
80
+ export const getTitlesOther = (el) => {
81
+ const namesNode = el.querySelectorAll('.film-names li');
82
+ if (!namesNode.length) {
83
+ return [];
84
+ }
85
+ const titlesOther = namesNode.map((el) => {
86
+ const country = el.querySelector('img.flag').attributes.alt;
87
+ const title = el.textContent.trim().split('\n')[0];
88
+ if (country && title) {
89
+ return {
90
+ country,
91
+ title
92
+ };
93
+ }
94
+ else {
95
+ return null;
96
+ }
97
+ });
98
+ return titlesOther.filter((x) => x);
99
+ };
100
+ export const getPoster = (el) => {
101
+ var _a;
102
+ const poster = el.querySelector('.film-posters img');
103
+ // Resolve empty image
104
+ if (poster) {
105
+ if ((_a = poster.classNames) === null || _a === void 0 ? void 0 : _a.includes('empty-image')) {
106
+ return null;
107
+ }
108
+ else {
109
+ // Full sized image (not thumb)
110
+ const imageThumb = poster.attributes.src.split('?')[0];
111
+ const image = imageThumb.replace(/\/w140\//, '/w1080/');
112
+ return addProtocol(image);
113
+ }
114
+ }
115
+ else {
116
+ return null;
117
+ }
118
+ };
119
+ export const getRandomPhoto = (el) => {
120
+ var _a;
121
+ const imageNode = el.querySelector('.gallery-item picture img');
122
+ const image = (_a = imageNode === null || imageNode === void 0 ? void 0 : imageNode.attributes) === null || _a === void 0 ? void 0 : _a.src;
123
+ if (image) {
124
+ return image.replace(/\/w663\//, '/w1326/');
125
+ }
126
+ else {
127
+ return null;
128
+ }
129
+ };
130
+ export const getTrivia = (el) => {
131
+ const triviaNodes = el.querySelectorAll('.article-trivia ul li');
132
+ if (triviaNodes === null || triviaNodes === void 0 ? void 0 : triviaNodes.length) {
133
+ return triviaNodes.map((node) => node.textContent.trim().replace(/(\r\n|\n|\r|\t)/gm, ''));
134
+ }
135
+ else {
136
+ return null;
137
+ }
138
+ };
139
+ export const getDescriptions = (el) => {
140
+ return el
141
+ .querySelectorAll('.body--plots .plot-full p, .body--plots .plots .plots-item p')
142
+ .map((movie) => { var _a; return (_a = movie.textContent) === null || _a === void 0 ? void 0 : _a.trim().replace(/(\r\n|\n|\r|\t)/gm, ''); });
143
+ };
144
+ export const parsePeople = (el) => {
145
+ const people = el.querySelectorAll('a');
146
+ return (people
147
+ // Filter out "more" links
148
+ .filter((x) => x.classNames.length === 0)
149
+ .map((person) => {
150
+ return {
151
+ id: parseIdFromUrl(person.attributes.href),
152
+ name: person.innerText.trim(),
153
+ url: `https://www.csfd.cz${person.attributes.href}`
154
+ };
155
+ }));
156
+ };
157
+ export const getGroup = (el, group) => {
158
+ const creators = el.querySelectorAll('.creators h4');
159
+ const element = creators.filter((elem) => elem.textContent.trim().includes(group))[0];
160
+ if (element === null || element === void 0 ? void 0 : element.parentNode) {
161
+ return parsePeople(element.parentNode);
162
+ }
163
+ else {
164
+ return [];
165
+ }
166
+ };
167
+ export const getType = (el) => {
168
+ var _a;
169
+ const type = el.querySelector('.film-header-name .type');
170
+ return ((_a = type === null || type === void 0 ? void 0 : type.innerText) === null || _a === void 0 ? void 0 : _a.replace(/[{()}]/g, '')) || 'film';
171
+ };
172
+ export const getVods = (el) => {
173
+ let vods = [];
174
+ if (el) {
175
+ const buttons = el.querySelectorAll('.box-buttons .button');
176
+ const buttonsVod = buttons.filter((x) => !x.classNames.includes('button-social'));
177
+ vods = buttonsVod.map((btn) => {
178
+ return {
179
+ title: btn.textContent.trim(),
180
+ url: btn.attributes.href
181
+ };
182
+ });
183
+ }
184
+ return vods.length ? vods : [];
185
+ };
186
+ // Get box content
187
+ export const getBoxContent = (el, box) => {
188
+ var _a;
189
+ const headers = el.querySelectorAll('section.box .box-header');
190
+ return (_a = headers.find((header) => header.querySelector('h3').textContent.trim().includes(box))) === null || _a === void 0 ? void 0 : _a.parentNode;
191
+ };
192
+ export const getBoxMovies = (el, boxName) => {
193
+ const movieListItem = [];
194
+ const box = getBoxContent(el, boxName);
195
+ const movieTitleNodes = box === null || box === void 0 ? void 0 : box.querySelectorAll('.article-header .film-title-name');
196
+ if (movieTitleNodes === null || movieTitleNodes === void 0 ? void 0 : movieTitleNodes.length) {
197
+ for (const item of movieTitleNodes) {
198
+ movieListItem.push({
199
+ id: parseIdFromUrl(item.attributes.href),
200
+ title: item.textContent.trim(),
201
+ url: `https://www.csfd.cz${item.attributes.href}`
202
+ });
203
+ }
204
+ }
205
+ return movieListItem;
206
+ };
207
+ export const getPremieres = (el) => {
208
+ var _a, _b;
209
+ const premiereNodes = el.querySelectorAll('.box-premieres li');
210
+ const premiere = [];
211
+ for (const premiereNode of premiereNodes) {
212
+ const title = premiereNode.querySelector('p + span').attributes.title;
213
+ if (title) {
214
+ const [date, ...company] = title === null || title === void 0 ? void 0 : title.split(' ');
215
+ premiere.push({
216
+ country: ((_a = premiereNode.querySelector('.flag')) === null || _a === void 0 ? void 0 : _a.attributes.title) || null,
217
+ format: (_b = premiereNode.querySelector('p').textContent.trim()) === null || _b === void 0 ? void 0 : _b.split(' od')[0],
218
+ date,
219
+ company: company.join(' ')
220
+ });
221
+ }
222
+ }
223
+ return premiere;
224
+ };
225
+ export const getTags = (el) => {
226
+ const tagsRaw = el.querySelectorAll('.box-content a[href*="/podrobne-vyhledavani/?tag="]');
227
+ return tagsRaw.map((tag) => tag.textContent);
228
+ };
@@ -0,0 +1,15 @@
1
+ import { addProtocol } from './global.helper';
2
+ export const getUser = (el) => {
3
+ return el.querySelector('.user-title-name').text;
4
+ };
5
+ export const getUserRealName = (el) => {
6
+ var _a;
7
+ return ((_a = el.querySelector('.user-real-name')) === null || _a === void 0 ? void 0 : _a.text.trim()) || null;
8
+ };
9
+ export const getAvatar = (el) => {
10
+ const image = el.querySelector('.article-img img').attributes.src;
11
+ return addProtocol(image);
12
+ };
13
+ export const getUserUrl = (el) => {
14
+ return el.querySelector('.user-title-name').attributes.href;
15
+ };
@@ -0,0 +1,51 @@
1
+ import { addProtocol, parseColor, parseIdFromUrl } from './global.helper';
2
+ export const getType = (el) => {
3
+ const type = el.querySelectorAll('.film-title-info .info')[1];
4
+ return ((type === null || type === void 0 ? void 0 : type.innerText.replace(/[{()}]/g, '')) || 'film');
5
+ };
6
+ export const getTitle = (el) => {
7
+ return el.querySelector('.film-title-name').text;
8
+ };
9
+ export const getYear = (el) => {
10
+ var _a;
11
+ return +((_a = el.querySelectorAll('.film-title-info .info')[0]) === null || _a === void 0 ? void 0 : _a.innerText.replace(/[{()}]/g, ''));
12
+ };
13
+ export const getUrl = (el) => {
14
+ return el.querySelector('.film-title-name').attributes.href;
15
+ };
16
+ export const getColorRating = (el) => {
17
+ return parseColor(el.querySelector('.article-header i.icon').classNames.split(' ').pop());
18
+ };
19
+ export const getPoster = (el) => {
20
+ const image = el.querySelector('img').attributes.src;
21
+ return addProtocol(image);
22
+ };
23
+ export const getOrigins = (el) => {
24
+ var _a, _b;
25
+ const originsRaw = (_a = el.querySelector('.article-content p .info')) === null || _a === void 0 ? void 0 : _a.text;
26
+ if (!originsRaw)
27
+ return [];
28
+ const originsAll = (_b = originsRaw === null || originsRaw === void 0 ? void 0 : originsRaw.split(', ')) === null || _b === void 0 ? void 0 : _b[0];
29
+ return originsAll === null || originsAll === void 0 ? void 0 : originsAll.split('/').map((country) => country.trim());
30
+ };
31
+ export const parsePeople = (el, type) => {
32
+ let who;
33
+ if (type === 'directors')
34
+ who = 'Režie:';
35
+ if (type === 'actors')
36
+ who = 'Hrají:';
37
+ const peopleNode = Array.from(el && el.querySelectorAll('.article-content p')).find((el) => el.textContent.includes(who));
38
+ if (peopleNode) {
39
+ const people = Array.from(peopleNode.querySelectorAll('a'));
40
+ return people.map((person) => {
41
+ return {
42
+ id: parseIdFromUrl(person.attributes.href),
43
+ name: person.innerText.trim(),
44
+ url: `https://www.csfd.cz${person.attributes.href}`
45
+ };
46
+ });
47
+ }
48
+ else {
49
+ return [];
50
+ }
51
+ };
@@ -0,0 +1,48 @@
1
+ import { parseIdFromUrl } from './global.helper';
2
+ export const getId = (el) => {
3
+ const url = el.querySelector('td.name .film-title-name').attributes.href;
4
+ return parseIdFromUrl(url);
5
+ };
6
+ export const getUserRating = (el) => {
7
+ const ratingText = el.querySelector('td.star-rating-only .stars').classNames.split(' ').pop();
8
+ const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
9
+ return rating;
10
+ };
11
+ export const getType = (el) => {
12
+ const typeText = el.querySelectorAll('td.name .film-title-info .info');
13
+ return (typeText.length > 1 ? typeText[1].text.slice(1, -1) : 'film');
14
+ };
15
+ export const getTitle = (el) => {
16
+ return el.querySelector('td.name .film-title-name').text;
17
+ };
18
+ export const getYear = (el) => {
19
+ var _a;
20
+ return +((_a = el.querySelectorAll('td.name .film-title-info .info')[0]) === null || _a === void 0 ? void 0 : _a.text.slice(1, -1)) || null;
21
+ };
22
+ export const getColorRating = (el) => {
23
+ const color = parseColor(el.querySelector('td.name .icon').classNames.split(' ').pop());
24
+ return color;
25
+ };
26
+ export const getDate = (el) => {
27
+ return el.querySelector('td.date-only').text.trim();
28
+ };
29
+ export const getUrl = (el) => {
30
+ const url = el.querySelector('td.name .film-title-name').attributes.href;
31
+ return `https://www.csfd.cz${url}`;
32
+ };
33
+ export const parseColor = (quality) => {
34
+ switch (quality) {
35
+ case 'lightgrey':
36
+ return 'unknown';
37
+ case 'red':
38
+ return 'good';
39
+ case 'blue':
40
+ return 'average';
41
+ case 'grey':
42
+ return 'bad';
43
+ default:
44
+ return 'unknown';
45
+ }
46
+ };
47
+ // Sleep in loop
48
+ export const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
package/esm/index.js ADDED
@@ -0,0 +1,35 @@
1
+ import { CinemaScraper } from './services/cinema.service';
2
+ import { CreatorScraper } from './services/creator.service';
3
+ import { MovieScraper } from './services/movie.service';
4
+ import { SearchScraper } from './services/search.service';
5
+ import { UserRatingsScraper } from './services/user-ratings.service';
6
+ export class Csfd {
7
+ constructor(userRatingsService, movieService, creatorService, searchService, cinemaService) {
8
+ this.userRatingsService = userRatingsService;
9
+ this.movieService = movieService;
10
+ this.creatorService = creatorService;
11
+ this.searchService = searchService;
12
+ this.cinemaService = cinemaService;
13
+ }
14
+ async userRatings(user, config) {
15
+ return this.userRatingsService.userRatings(user, config);
16
+ }
17
+ async movie(movie) {
18
+ return this.movieService.movie(+movie);
19
+ }
20
+ async creator(creator) {
21
+ return this.creatorService.creator(+creator);
22
+ }
23
+ async search(text) {
24
+ return this.searchService.search(text);
25
+ }
26
+ async cinema(district, period) {
27
+ return this.cinemaService.cinemas(+district, period);
28
+ }
29
+ }
30
+ const movieScraper = new MovieScraper();
31
+ const userRatingsScraper = new UserRatingsScraper();
32
+ const cinemaScraper = new CinemaScraper();
33
+ const creatorScraper = new CreatorScraper();
34
+ const searchScraper = new SearchScraper();
35
+ export const csfd = new Csfd(userRatingsScraper, movieScraper, creatorScraper, searchScraper, cinemaScraper);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};