node-csfd-api-racintom 1.0.0

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 (67) hide show
  1. package/.editorconfig +13 -0
  2. package/.eslintrc.json +33 -0
  3. package/.gitattributes +2 -0
  4. package/.github/FUNDING.yml +8 -0
  5. package/.github/pull_request_template.md +19 -0
  6. package/.github/workflows/main.yml +40 -0
  7. package/.github/workflows/publish.yml +73 -0
  8. package/.github/workflows/test.yml +43 -0
  9. package/.husky/pre-commit +1 -0
  10. package/.idea/codeStyles/Project.xml +72 -0
  11. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  12. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  13. package/.idea/misc.xml +6 -0
  14. package/.idea/modules.xml +8 -0
  15. package/.idea/node-csfd-api.iml +9 -0
  16. package/.idea/prettier.xml +6 -0
  17. package/.idea/vcs.xml +7 -0
  18. package/.nvmrc +1 -0
  19. package/.prettierignore +8 -0
  20. package/.prettierrc +10 -0
  21. package/.vscode/settings.json +16 -0
  22. package/Dockerfile +19 -0
  23. package/README.md +510 -0
  24. package/demo.ts +35 -0
  25. package/eslint.config.mjs +55 -0
  26. package/package.json +86 -0
  27. package/server.ts +66 -0
  28. package/src/fetchers/fetch.polyfill.ts +7 -0
  29. package/src/fetchers/index.ts +25 -0
  30. package/src/helpers/creator.helper.ts +95 -0
  31. package/src/helpers/global.helper.ts +70 -0
  32. package/src/helpers/movie.helper.ts +276 -0
  33. package/src/helpers/search-user.helper.ts +19 -0
  34. package/src/helpers/search.helper.ts +66 -0
  35. package/src/helpers/user-ratings.helper.ts +62 -0
  36. package/src/index.ts +42 -0
  37. package/src/interfaces/creator.interface.ts +14 -0
  38. package/src/interfaces/global.ts +36 -0
  39. package/src/interfaces/movie.interface.ts +157 -0
  40. package/src/interfaces/search.interface.ts +32 -0
  41. package/src/interfaces/user-ratings.interface.ts +21 -0
  42. package/src/services/creator.service.ts +34 -0
  43. package/src/services/movie.service.ts +89 -0
  44. package/src/services/search.service.ts +101 -0
  45. package/src/services/user-ratings.service.ts +106 -0
  46. package/src/vars.ts +13 -0
  47. package/tests/creator.test.ts +182 -0
  48. package/tests/fetchers.test.ts +109 -0
  49. package/tests/global.test.ts +35 -0
  50. package/tests/helpers.test.ts +59 -0
  51. package/tests/mocks/creator-actor.html.ts +2244 -0
  52. package/tests/mocks/creator-composer-empty.html.ts +683 -0
  53. package/tests/mocks/creator-director.html.ts +3407 -0
  54. package/tests/mocks/movie1.html.ts +1430 -0
  55. package/tests/mocks/movie2.html.ts +740 -0
  56. package/tests/mocks/movie3.html.ts +1843 -0
  57. package/tests/mocks/movie4.html.ts +1568 -0
  58. package/tests/mocks/search.html.ts +838 -0
  59. package/tests/mocks/series1.html.ts +1540 -0
  60. package/tests/mocks/userRatings.html.ts +1354 -0
  61. package/tests/movie.test.ts +606 -0
  62. package/tests/search.test.ts +379 -0
  63. package/tests/services.test.ts +106 -0
  64. package/tests/user-ratings.test.ts +142 -0
  65. package/tests/vars.test.ts +34 -0
  66. package/tsconfig.json +23 -0
  67. package/vitest.config.mts +10 -0
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { CSFDCreator } from './interfaces/creator.interface';
2
+ import { CSFDMovie } from './interfaces/movie.interface';
3
+ import { CSFDSearch } from './interfaces/search.interface';
4
+ import { CSFDUserRatingConfig, CSFDUserRatings } from './interfaces/user-ratings.interface';
5
+ import { CreatorScraper } from './services/creator.service';
6
+ import { MovieScraper } from './services/movie.service';
7
+ import { SearchScraper } from './services/search.service';
8
+ import { UserRatingsScraper } from './services/user-ratings.service';
9
+
10
+ export class Csfd {
11
+ constructor(
12
+ private userRatingsService: UserRatingsScraper,
13
+ private movieService: MovieScraper,
14
+ private creatorService: CreatorScraper,
15
+ private searchService: SearchScraper
16
+ ) {}
17
+
18
+ public async userRatings(
19
+ user: string | number,
20
+ config?: CSFDUserRatingConfig
21
+ ): Promise<CSFDUserRatings[]> {
22
+ return this.userRatingsService.userRatings(user, config);
23
+ }
24
+
25
+ public async movie(movie: number): Promise<CSFDMovie> {
26
+ return this.movieService.movie(+movie);
27
+ }
28
+
29
+ public async creator(creator: number): Promise<CSFDCreator> {
30
+ return this.creatorService.creator(+creator);
31
+ }
32
+
33
+ public async search(text: string): Promise<CSFDSearch> {
34
+ return this.searchService.search(text);
35
+ }
36
+ }
37
+
38
+ const movieScraper = new MovieScraper();
39
+ const userRatingsScraper = new UserRatingsScraper();
40
+ const creatorScraper = new CreatorScraper();
41
+ const searchScraper = new SearchScraper();
42
+ export const csfd = new Csfd(userRatingsScraper, movieScraper, creatorScraper, searchScraper);
@@ -0,0 +1,14 @@
1
+ import { CSFDScreening } from './global';
2
+
3
+ export interface CSFDCreator {
4
+ id: number;
5
+ name: string;
6
+ birthday: string;
7
+ birthplace: string;
8
+ photo: string;
9
+ age: number | string;
10
+ bio: string;
11
+ films: CSFDCreatorScreening[];
12
+ }
13
+
14
+ export type CSFDCreatorScreening = Omit<CSFDScreening, 'url' | 'type'>;
@@ -0,0 +1,36 @@
1
+ export interface CSFDScreening {
2
+ id: number;
3
+ title: string;
4
+ year: number;
5
+ url: string;
6
+ type: CSFDFilmTypes;
7
+ /**
8
+ * Overall aggregated rating. (On the web usually represented by colors).
9
+ *
10
+ * 'unknown': unknown (gray color)
11
+ *
12
+ * 'good': 70% – 100 % (red color)
13
+ *
14
+ * 'average': 30% - 69% (blue color)
15
+ *
16
+ * 'bad': 0% - 29% (black color)
17
+ */
18
+ colorRating: CSFDColorRating;
19
+ }
20
+
21
+ export type CSFDColorRating = 'bad' | 'average' | 'good' | 'unknown';
22
+
23
+ export type CSFDStars = 0 | 1 | 2 | 3 | 4 | 5;
24
+
25
+ export type CSFDFilmTypes =
26
+ | 'film'
27
+ | 'TV film'
28
+ | 'pořad'
29
+ | 'seriál'
30
+ | 'divadelní záznam'
31
+ | 'koncert'
32
+ | 'série'
33
+ | 'studentský film'
34
+ | 'amatérský film'
35
+ | 'hudební videoklip'
36
+ | 'epizoda';
@@ -0,0 +1,157 @@
1
+ import { CSFDScreening } from './global';
2
+
3
+ export interface CSFDMovie extends CSFDScreening {
4
+ rating: number | null;
5
+ poster: string;
6
+ photo: string;
7
+ ratingCount: number | null;
8
+ duration: number | string;
9
+ titlesOther: CSFDTitlesOther[];
10
+ origins: string[];
11
+ descriptions: string[];
12
+ trivia: string[];
13
+ genres: CSFDGenres[] | string[];
14
+ creators: CSFDCreators;
15
+ vod: CSFDVod[];
16
+ tags: string[];
17
+ premieres: CSFDPremiere[];
18
+ related: CSFDMovieListItem[];
19
+ similar: CSFDMovieListItem[];
20
+ seasons: CSFDSeasons | null;
21
+ }
22
+
23
+ export type CSFDVodService =
24
+ | 'Netflix'
25
+ | 'hbogo'
26
+ | 'Prime Video'
27
+ | 'Apple TV+'
28
+ | 'iTunes'
29
+ | 'KVIFF.TV'
30
+ | 'Edisonline'
31
+ | 'o2tv'
32
+ | 'SledovaniTV'
33
+ | 'Starmax'
34
+ | 'DAFilms'
35
+ | 'FILMY ČESKY A ZADARMO'
36
+ | 'Youtube Česká filmová klasika'
37
+ | 'VAPET'
38
+ | 'VOREL FILM'
39
+ | 'ivysilani'
40
+ | 'Google Play'
41
+ | 'Voyo'
42
+ | 'DVD';
43
+
44
+ export interface CSFDVod {
45
+ title: CSFDVodService;
46
+ url: string;
47
+ }
48
+
49
+ export interface CSFDCreators {
50
+ directors: CSFDCreator[];
51
+ writers: CSFDCreator[];
52
+ cinematography: CSFDCreator[];
53
+ music: CSFDCreator[];
54
+ actors: CSFDCreator[];
55
+ basedOn: CSFDCreator[];
56
+ producers: CSFDCreator[];
57
+ filmEditing: CSFDCreator[];
58
+ costumeDesign: CSFDCreator[];
59
+ productionDesign: CSFDCreator[];
60
+ }
61
+
62
+ export interface CSFDTitlesOther {
63
+ country: string;
64
+ title: string;
65
+ }
66
+
67
+ export interface CSFDCreator {
68
+ /**
69
+ * CSFD person ID.
70
+ *
71
+ * You can always assemble url from ID like this:
72
+ *
73
+ * `https://www.csfd.cz/tvurce/${id}`
74
+ */
75
+ id: number;
76
+ name: string;
77
+ url: string;
78
+ }
79
+
80
+ export interface CSFDMovieListItem {
81
+ id: number;
82
+ title: string;
83
+ url: string;
84
+ }
85
+
86
+ export type CSFDGenres =
87
+ | 'Akční'
88
+ | 'Animovaný'
89
+ | 'Dobrodružný'
90
+ | 'Dokumentární'
91
+ | 'Drama'
92
+ | 'Experimentální'
93
+ | 'Fantasy'
94
+ | 'Film-Noir'
95
+ | 'Historický'
96
+ | 'Horor'
97
+ | 'Hudební'
98
+ | 'IMAX'
99
+ | 'Katastrofický'
100
+ | 'Komedie'
101
+ | 'Krátkometrážní'
102
+ | 'Krimi'
103
+ | 'Loutkový'
104
+ | 'Muzikál'
105
+ | 'Mysteriózní'
106
+ | 'Naučný'
107
+ | 'Podobenství'
108
+ | 'Poetický'
109
+ | 'Pohádka'
110
+ | 'Povídkový'
111
+ | 'Psychologický'
112
+ | 'Publicistický'
113
+ | 'Reality-TV'
114
+ | 'Road movie'
115
+ | 'Rodinný'
116
+ | 'Romantický'
117
+ | 'Sci-Fi'
118
+ | 'Soutěžní'
119
+ | 'Sportovní'
120
+ | 'Stand-up'
121
+ | 'Talk-show'
122
+ | 'Taneční'
123
+ | 'Telenovela'
124
+ | 'Thriller'
125
+ | 'Válečný'
126
+ | 'Western'
127
+ | 'Zábavný'
128
+ | 'Životopisný';
129
+
130
+ export type CSFDCreatorGroups =
131
+ | 'Režie'
132
+ | 'Scénář'
133
+ | 'Kamera'
134
+ | 'Hudba'
135
+ | 'Hrají'
136
+ | 'Produkce'
137
+ | 'Střih'
138
+ | 'Předloha'
139
+ | 'Scénografie'
140
+ | 'Kostýmy';
141
+
142
+ export interface CSFDPremiere {
143
+ country: string;
144
+ format: string;
145
+ date: string;
146
+ company: string;
147
+ }
148
+
149
+ export type CSFDBoxContent = 'Související' | 'Podobné';
150
+
151
+ export type CSFDSeasons = CSFDSeasonInfo[]
152
+
153
+ export interface CSFDSeasonInfo {
154
+ name: string
155
+ linkToDetail: string
156
+ additionalInfo: string
157
+ }
@@ -0,0 +1,32 @@
1
+ import { CSFDScreening } from './global';
2
+ import { CSFDCreator } from './movie.interface';
3
+
4
+ export interface CSFDSearch {
5
+ movies: CSFDSearchMovie[];
6
+ tvSeries: CSFDSearchMovie[];
7
+ creators: CSFDSearchCreator[];
8
+ users: CSFDSearchUser[];
9
+ }
10
+
11
+ export interface CSFDSearchMovie extends CSFDScreening {
12
+ poster: string;
13
+ origins: string[];
14
+ creators: CSFDSearchCreators;
15
+ }
16
+
17
+ export interface CSFDSearchUser {
18
+ id: number;
19
+ user: string;
20
+ userRealName: string;
21
+ avatar: string;
22
+ url: string;
23
+ }
24
+
25
+ export interface CSFDSearchCreator extends CSFDCreator {
26
+ image: string;
27
+ }
28
+
29
+ export interface CSFDSearchCreators {
30
+ directors: CSFDCreator[];
31
+ actors: CSFDCreator[];
32
+ }
@@ -0,0 +1,21 @@
1
+ import { CSFDFilmTypes, CSFDScreening, CSFDStars } from './global';
2
+
3
+ export interface CSFDUserRatings extends CSFDScreening {
4
+ userRating: CSFDStars;
5
+ userDate: string; // TODO datetime
6
+ }
7
+
8
+ export interface CSFDUserRatingConfig {
9
+ includesOnly?: CSFDFilmTypes[];
10
+ excludes?: CSFDFilmTypes[];
11
+ /**
12
+ * Fetch all ratings. (Warning: Use it wisely. Can be detected and banned. Consider using it together with `allPagesDelay` attribute)
13
+ */
14
+ allPages?: boolean;
15
+ /**
16
+ * Delay on each page request. In milliseconds
17
+ */
18
+ allPagesDelay?: number;
19
+ }
20
+
21
+ export type Colors = 'lightgrey' | 'blue' | 'red' | 'grey';
@@ -0,0 +1,34 @@
1
+ import { HTMLElement, parse } from 'node-html-parser';
2
+ import { fetchPage } from '../fetchers';
3
+ import { getBio, getBirthdayInfo, getFilms, getName, getPhoto } from '../helpers/creator.helper';
4
+ import { CSFDCreator } from '../interfaces/creator.interface';
5
+ import { creatorUrl } from '../vars';
6
+
7
+ export class CreatorScraper {
8
+ private person: CSFDCreator;
9
+
10
+ public async creator(creatorId: number): Promise<CSFDCreator> {
11
+ const url = creatorUrl(+creatorId);
12
+ const response = await fetchPage(url);
13
+
14
+ const creatorHtml = parse(response);
15
+
16
+ const asideNode = creatorHtml.querySelector('.creator-about');
17
+ const filmsNode = creatorHtml.querySelector('.creator-filmography');
18
+ this.buildCreator(+creatorId, asideNode, filmsNode);
19
+ return this.person;
20
+ }
21
+
22
+ private buildCreator(id: number, asideEl: HTMLElement, filmsNode: HTMLElement) {
23
+ this.person = {
24
+ id,
25
+ name: getName(asideEl),
26
+ birthday: getBirthdayInfo(asideEl)?.birthday,
27
+ birthplace: getBirthdayInfo(asideEl)?.birthPlace,
28
+ photo: getPhoto(asideEl),
29
+ age: getBirthdayInfo(asideEl)?.age || null,
30
+ bio: getBio(asideEl),
31
+ films: getFilms(filmsNode)
32
+ };
33
+ }
34
+ }
@@ -0,0 +1,89 @@
1
+ import { HTMLElement, parse } from 'node-html-parser';
2
+ import { fetchPage } from '../fetchers';
3
+ import {
4
+ getBoxMovies,
5
+ getColorRating,
6
+ getDescriptions,
7
+ getDuration,
8
+ getGenres,
9
+ getGroup,
10
+ getOrigins,
11
+ getPoster,
12
+ getPremieres,
13
+ getRandomPhoto,
14
+ getRating,
15
+ getRatingCount, getSeasonsInfo,
16
+ getTags,
17
+ getTitle,
18
+ getTitlesOther,
19
+ getTrivia,
20
+ getType,
21
+ getVods,
22
+ getYear
23
+ } from '../helpers/movie.helper';
24
+ import { CSFDFilmTypes } from '../interfaces/global';
25
+ import { CSFDMovie } from '../interfaces/movie.interface';
26
+ import { movieUrl } from '../vars';
27
+
28
+ export class MovieScraper {
29
+ private film: CSFDMovie;
30
+
31
+ public async movie(movieId: number): Promise<CSFDMovie> {
32
+ const url = movieUrl(+movieId);
33
+ const response = await fetchPage(url);
34
+
35
+ const movieHtml = parse(response);
36
+
37
+ const pageClasses = movieHtml.querySelector('.page-content').classNames.split(' ');
38
+ const asideNode = movieHtml.querySelector('.aside-movie-profile');
39
+ const movieNode = movieHtml.querySelector('.main-movie-profile');
40
+ const jsonLd = movieHtml.querySelector('script[type="application/ld+json"]').innerText;
41
+ this.buildMovie(+movieId, movieNode, asideNode, pageClasses, jsonLd);
42
+ return this.film;
43
+ }
44
+
45
+ private buildMovie(
46
+ movieId: number,
47
+ el: HTMLElement,
48
+ asideEl: HTMLElement,
49
+ pageClasses: string[],
50
+ jsonLd: string
51
+ ) {
52
+ this.film = {
53
+ id: movieId,
54
+ title: getTitle(el),
55
+ year: getYear(jsonLd),
56
+ duration: getDuration(jsonLd, el),
57
+ descriptions: getDescriptions(el),
58
+ genres: getGenres(el),
59
+ type: getType(el) as CSFDFilmTypes,
60
+ url: movieUrl(movieId),
61
+ origins: getOrigins(el),
62
+ colorRating: getColorRating(pageClasses),
63
+ rating: getRating(asideEl),
64
+ ratingCount: getRatingCount(asideEl),
65
+ titlesOther: getTitlesOther(el),
66
+ poster: getPoster(el),
67
+ photo: getRandomPhoto(el),
68
+ trivia: getTrivia(el),
69
+ creators: {
70
+ directors: getGroup(el, 'Režie'),
71
+ writers: getGroup(el, 'Scénář'),
72
+ cinematography: getGroup(el, 'Kamera'),
73
+ music: getGroup(el, 'Hudba'),
74
+ actors: getGroup(el, 'Hrají'),
75
+ basedOn: getGroup(el, 'Předloha'),
76
+ producers: getGroup(el, 'Produkce'),
77
+ filmEditing: getGroup(el, 'Střih'),
78
+ costumeDesign: getGroup(el, 'Kostýmy'),
79
+ productionDesign: getGroup(el, 'Scénografie')
80
+ },
81
+ vod: getVods(asideEl),
82
+ tags: getTags(asideEl),
83
+ premieres: getPremieres(asideEl),
84
+ related: getBoxMovies(asideEl, 'Související'),
85
+ similar: getBoxMovies(asideEl, 'Podobné'),
86
+ seasons: getSeasonsInfo(el)
87
+ };
88
+ }
89
+ }
@@ -0,0 +1,101 @@
1
+ import { HTMLElement, parse } from 'node-html-parser';
2
+ import { fetchPage } from '../fetchers';
3
+ import { parseIdFromUrl } from '../helpers/global.helper';
4
+ import { getAvatar, getUser, getUserRealName, getUserUrl } from '../helpers/search-user.helper';
5
+ import {
6
+ getColorRating,
7
+ getOrigins,
8
+ getPoster,
9
+ getTitle,
10
+ getType,
11
+ getUrl,
12
+ getYear,
13
+ parsePeople
14
+ } from '../helpers/search.helper';
15
+ import { CSFDSearch, CSFDSearchMovie, CSFDSearchUser } from '../interfaces/search.interface';
16
+ import { searchUrl } from '../vars';
17
+
18
+ export class SearchScraper {
19
+ public async search(text: string): Promise<CSFDSearch> {
20
+ const url = searchUrl(text);
21
+ const response = await fetchPage(url);
22
+
23
+ const html = parse(response);
24
+ const moviesNode = html.querySelectorAll('.main-movies article');
25
+ const usersNode = html.querySelectorAll('.main-users article');
26
+ const tvSeriesNode = html.querySelectorAll('.main-series article');
27
+
28
+ return this.parseSearch(moviesNode, usersNode, tvSeriesNode);
29
+ }
30
+
31
+ private parseSearch(
32
+ moviesNode: HTMLElement[],
33
+ usersNode: HTMLElement[],
34
+ tvSeriesNode: HTMLElement[]
35
+ ) {
36
+ const movies: CSFDSearchMovie[] = [];
37
+ const users: CSFDSearchUser[] = [];
38
+ const tvSeries: CSFDSearchMovie[] = [];
39
+
40
+ moviesNode.map((m) => {
41
+ const url = getUrl(m);
42
+
43
+ const movie: CSFDSearchMovie = {
44
+ id: parseIdFromUrl(url),
45
+ title: getTitle(m),
46
+ year: getYear(m),
47
+ url: `https://www.csfd.cz${url}`,
48
+ type: getType(m),
49
+ colorRating: getColorRating(m),
50
+ poster: getPoster(m),
51
+ origins: getOrigins(m),
52
+ creators: {
53
+ directors: parsePeople(m, 'directors'),
54
+ actors: parsePeople(m, 'actors')
55
+ }
56
+ };
57
+ movies.push(movie);
58
+ });
59
+
60
+ usersNode.map((m) => {
61
+ const url = getUserUrl(m);
62
+
63
+ const user: CSFDSearchUser = {
64
+ id: parseIdFromUrl(url),
65
+ user: getUser(m),
66
+ userRealName: getUserRealName(m),
67
+ avatar: getAvatar(m),
68
+ url: `https://www.csfd.cz${url}`
69
+ };
70
+ users.push(user);
71
+ });
72
+
73
+ tvSeriesNode.map((m) => {
74
+ const url = getUrl(m);
75
+
76
+ const user: CSFDSearchMovie = {
77
+ id: parseIdFromUrl(url),
78
+ title: getTitle(m),
79
+ year: getYear(m),
80
+ url: `https://www.csfd.cz${url}`,
81
+ type: getType(m),
82
+ colorRating: getColorRating(m),
83
+ poster: getPoster(m),
84
+ origins: getOrigins(m),
85
+ creators: {
86
+ directors: parsePeople(m, 'directors'),
87
+ actors: parsePeople(m, 'actors')
88
+ }
89
+ };
90
+ tvSeries.push(user);
91
+ });
92
+
93
+ const search: CSFDSearch = {
94
+ movies: movies,
95
+ users: users,
96
+ tvSeries: tvSeries,
97
+ creators: []
98
+ };
99
+ return search;
100
+ }
101
+ }
@@ -0,0 +1,106 @@
1
+ import { HTMLElement, parse } from 'node-html-parser';
2
+ import { fetchPage } from '../fetchers';
3
+ import {
4
+ getColorRating,
5
+ getDate,
6
+ getId,
7
+ getTitle,
8
+ getType,
9
+ getUrl,
10
+ getUserRating,
11
+ getYear,
12
+ sleep
13
+ } from '../helpers/user-ratings.helper';
14
+ import { CSFDColorRating, CSFDStars } from '../interfaces/global';
15
+ import { CSFDUserRatingConfig, CSFDUserRatings } from '../interfaces/user-ratings.interface';
16
+ import { userRatingsUrl } from '../vars';
17
+
18
+ export class UserRatingsScraper {
19
+ private films: CSFDUserRatings[] = [];
20
+
21
+ public async userRatings(
22
+ user: string | number,
23
+ config?: CSFDUserRatingConfig
24
+ ): Promise<CSFDUserRatings[]> {
25
+ let allMovies: CSFDUserRatings[] = [];
26
+ const url = userRatingsUrl(user);
27
+ const response = await fetchPage(url);
28
+
29
+ const items = parse(response);
30
+ const movies = items.querySelectorAll('.box-user-rating .table-container tbody tr');
31
+
32
+ // Get number of pages
33
+ const pagesNode = items.querySelector('.pagination');
34
+ const pages = +pagesNode?.childNodes[pagesNode.childNodes.length - 4].rawText || 1;
35
+
36
+ allMovies = this.getPage(config, movies);
37
+
38
+ if (config?.allPages) {
39
+ console.log('User', user, url);
40
+ console.log('Fetching all pages', pages);
41
+ for (let i = 2; i <= pages; i++) {
42
+ console.log('Fetching page', i, 'out of', pages, '...');
43
+ const url = userRatingsUrl(user, i);
44
+ const response = await fetchPage(url);
45
+
46
+ const items = parse(response);
47
+ const movies = items.querySelectorAll('.box-user-rating .table-container tbody tr');
48
+ allMovies = [...this.getPage(config, movies)];
49
+
50
+ // Sleep
51
+ if (config.allPagesDelay) {
52
+ await sleep(config.allPagesDelay);
53
+ }
54
+ }
55
+ return allMovies;
56
+ }
57
+
58
+ return allMovies;
59
+ }
60
+
61
+ private getPage(config: CSFDUserRatingConfig, movies: HTMLElement[]) {
62
+ if (config) {
63
+ if (config.includesOnly?.length && config.excludes?.length) {
64
+ console.warn(
65
+ `node-csfd-api:
66
+ You can not use both parameters 'includesOnly' and 'excludes'.
67
+ Parameter 'includesOnly' will be used now:`,
68
+ config.includesOnly
69
+ );
70
+ }
71
+ }
72
+
73
+ for (const el of movies) {
74
+ const type = getType(el);
75
+
76
+ // Filtering includesOnly
77
+ if (config?.includesOnly?.length) {
78
+ if (config.includesOnly.some((include) => type === include)) {
79
+ this.buildUserRatings(el);
80
+ }
81
+ // Filter exludes
82
+ } else if (config?.excludes?.length) {
83
+ if (!config.excludes.some((exclude) => type === exclude)) {
84
+ this.buildUserRatings(el);
85
+ }
86
+ } else {
87
+ // Without filtering
88
+ this.buildUserRatings(el);
89
+ }
90
+ }
91
+ return this.films;
92
+ }
93
+
94
+ private buildUserRatings(el: HTMLElement) {
95
+ this.films.push({
96
+ id: getId(el),
97
+ title: getTitle(el),
98
+ year: getYear(el),
99
+ type: getType(el),
100
+ url: getUrl(el),
101
+ colorRating: getColorRating(el) as CSFDColorRating,
102
+ userDate: getDate(el),
103
+ userRating: getUserRating(el) as CSFDStars
104
+ });
105
+ }
106
+ }
package/src/vars.ts ADDED
@@ -0,0 +1,13 @@
1
+ export const userRatingsUrl = (user: string | number, page?: number): string =>
2
+ `https://www.csfd.cz/uzivatel/${encodeURIComponent(user)}/hodnoceni/${
3
+ page ? '?page=' + page : ''
4
+ }`;
5
+
6
+ export const movieUrl = (movie: number): string =>
7
+ `https://www.csfd.cz/film/${encodeURIComponent(movie)}/prehled/`;
8
+
9
+ export const creatorUrl = (creator: number | string): string =>
10
+ `https://www.csfd.cz/tvurce/${encodeURIComponent(creator)}`;
11
+
12
+ export const searchUrl = (text: string): string =>
13
+ `https://www.csfd.cz/hledat/?q=${encodeURIComponent(text)}`;