-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmovie.service.ts
More file actions
98 lines (94 loc) · 3.73 KB
/
movie.service.ts
File metadata and controls
98 lines (94 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { HTMLElement, parse } from 'node-html-parser';
import { CSFDFilmTypes } from '../dto/global';
import { CSFDMovie, MovieJsonLd } from '../dto/movie';
import { fetchPage } from '../fetchers';
import {
getLocalizedCreatorLabel,
getMovieBoxMovies,
getMovieColorRating,
getMovieDescriptions,
getMovieDuration,
getMovieGenres,
getMovieGroup,
getMovieOrigins,
getMoviePoster,
getMoviePremieres,
getMovieRandomPhoto,
getMovieRating,
getMovieRatingCount,
getMovieTags,
getMovieTitle,
getMovieTitlesOther,
getMovieTrivia,
getMovieType,
getMovieVods,
getMovieYear
} from '../helpers/movie.helper';
import { CSFDOptions } from '../types';
import { movieUrl } from '../vars';
export class MovieScraper {
public async movie(movieId: number, options?: CSFDOptions): Promise<CSFDMovie> {
const id = Number(movieId);
if (isNaN(id)) {
throw new Error('node-csfd-api: movieId must be a valid number');
}
const url = movieUrl(id, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });
const movieHtml = parse(response);
const pageClasses = movieHtml.querySelector('.page-content').classNames.split(' ');
const asideNode = movieHtml.querySelector('.aside-movie-profile');
const movieNode = movieHtml.querySelector('.main-movie-profile');
const jsonLdString = movieHtml.querySelector('script[type="application/ld+json"]').innerText;
let jsonLd: MovieJsonLd | null = null;
try {
jsonLd = JSON.parse(jsonLdString);
} catch (e) {
console.error('node-csfd-api: Error parsing JSON-LD', e);
}
return this.buildMovie(+movieId, movieNode, asideNode, pageClasses, jsonLd, options);
}
private buildMovie(
movieId: number,
el: HTMLElement,
asideEl: HTMLElement,
pageClasses: string[],
jsonLd: MovieJsonLd | null,
options: CSFDOptions
): CSFDMovie {
return {
id: movieId,
title: getMovieTitle(el),
year: getMovieYear(jsonLd),
duration: getMovieDuration(jsonLd, el),
descriptions: getMovieDescriptions(el),
genres: getMovieGenres(el),
type: getMovieType(el) as CSFDFilmTypes,
url: movieUrl(movieId, { language: options?.language }),
origins: getMovieOrigins(el),
colorRating: getMovieColorRating(pageClasses),
rating: getMovieRating(asideEl),
ratingCount: getMovieRatingCount(asideEl),
titlesOther: getMovieTitlesOther(el),
poster: getMoviePoster(el),
photo: getMovieRandomPhoto(el),
trivia: getMovieTrivia(el),
creators: {
directors: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'directors')),
writers: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'writers')),
cinematography: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'cinematography')),
music: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'music')),
actors: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'actors')),
basedOn: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'basedOn')),
producers: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'producers')),
filmEditing: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'filmEditing')),
costumeDesign: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'costumeDesign')),
productionDesign: getMovieGroup(el, getLocalizedCreatorLabel(options?.language, 'productionDesign'))
},
vod: getMovieVods(asideEl),
tags: getMovieTags(asideEl),
premieres: getMoviePremieres(asideEl),
related: getMovieBoxMovies(asideEl, 'Související'),
similar: getMovieBoxMovies(asideEl, 'Podobné')
};
}
}