Skip to content

Commit 3c208a0

Browse files
committed
feat(tv-series): seasons, episodes [kickoff]
1 parent 5b8519c commit 3c208a0

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

demo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import { csfd } from './src';
33

44
// Parse movie
5-
csfd.movie(10135).then((movie) => console.log(movie));
5+
csfd.movie(621073).then((movie) => console.log(movie));
66

77
// csfd.search('matrix').then((search) => console.log(search));
88
// csfd.cinema(1, 'today').then((cinema) => console.log(cinema));
99

1010
// Parse creator
11-
csfd.creator(2120).then((creator) => console.log(creator));
11+
// csfd.creator(2120).then((creator) => console.log(creator));
1212

1313
/**
1414
* USER RATINGS

src/dto/movie.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export interface CSFDMovie extends CSFDScreening {
1717
premieres: CSFDPremiere[];
1818
related: CSFDMovieListItem[];
1919
similar: CSFDMovieListItem[];
20+
seasons: CSFDSeason[] | null;
21+
episodes: CSFDSeason[] | null;
22+
parent: CSFDParent | null;
23+
}
24+
25+
export interface CSFDParent {
26+
season: { id: number; name: string; };
27+
series: { id: number; name: string; };
2028
}
2129

2230
export type CSFDVodService =
@@ -82,6 +90,8 @@ export interface CSFDMovieListItem {
8290
url: string;
8391
}
8492

93+
94+
8595
export type CSFDGenres =
8696
| 'Akční'
8797
| 'Animovaný'
@@ -146,3 +156,10 @@ export interface CSFDPremiere {
146156
}
147157

148158
export type CSFDBoxContent = 'Související' | 'Podobné';
159+
160+
export interface CSFDSeason {
161+
id: number;
162+
name: string;
163+
url: string;
164+
info: string | null;
165+
}

src/helpers/global.helper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ export const parseIdFromUrl = (url: string): number => {
1111
}
1212
};
1313

14+
export const parseLastIdFromUrl = (url: string): number => {
15+
if (url) {
16+
const idSlug = url?.split('/')[3];
17+
const id = idSlug?.split('-')[0];
18+
return +id || null;
19+
} else {
20+
return null;
21+
}
22+
};
23+
1424
export const getColor = (cls: string): CSFDColorRating => {
1525
switch (cls) {
1626
case 'page-lightgrey':

src/helpers/movie.helper.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import {
66
CSFDGenres,
77
CSFDMovieCreator,
88
CSFDMovieListItem,
9+
CSFDParent,
910
CSFDPremiere,
11+
CSFDSeason,
1012
CSFDTitlesOther,
1113
CSFDVod,
1214
CSFDVodService
1315
} from '../dto/movie';
14-
import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl } from './global.helper';
16+
import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl, parseLastIdFromUrl } from './global.helper';
1517

1618
export const getMovieId = (el: HTMLElement): number => {
1719
const url = el.querySelector('.tabs .tab-nav-list a').attributes.href;
@@ -177,6 +179,42 @@ const parseMoviePeople = (el: HTMLElement): CSFDMovieCreator[] => {
177179
);
178180
};
179181

182+
export const getSeasonsOrEpisodes = (el: HTMLElement): CSFDSeason[] | null => {
183+
const childrenList = el.querySelector('.film-episodes-list');
184+
if (!childrenList) return null;
185+
186+
const childrenNodes = childrenList.querySelectorAll('.film-title');
187+
if (!childrenNodes?.length) return [];
188+
189+
return childrenNodes.map((season) => {
190+
const nameContainer = season.querySelector('.film-title-name');
191+
const infoContainer = season.querySelector('.info');
192+
193+
return {
194+
id: parseLastIdFromUrl(nameContainer?.getAttribute('href') || ''),
195+
name: nameContainer?.textContent?.trim() || null,
196+
url: nameContainer?.getAttribute('href') || null,
197+
info: infoContainer?.textContent?.replace(/[{()}]/g, '').trim() || null,
198+
};
199+
});
200+
}
201+
202+
export const getParent = (el: HTMLElement): CSFDParent | null => {
203+
const parents = el.querySelectorAll('.film-header h2 a');
204+
if (parents.length === 0) return null;
205+
206+
const [parentSeries, parentSeason] = parents;
207+
208+
return {
209+
series: {
210+
id: parseIdFromUrl(parentSeries?.getAttribute('href') || ''), name: parentSeries?.textContent?.trim() || null
211+
},
212+
season: {
213+
id: parseIdFromUrl(parentSeason?.getAttribute('href') || ''), name: parentSeason?.textContent?.trim() || null
214+
}
215+
};
216+
}
217+
180218
export const getMovieGroup = (el: HTMLElement, group: CSFDCreatorGroups): CSFDMovieCreator[] => {
181219
const creators = el.querySelectorAll('.creators h4');
182220
const element = creators.filter((elem) => elem.textContent.trim().includes(group))[0];

src/services/movie.service.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import {
2121
getMovieTrivia,
2222
getMovieType,
2323
getMovieVods,
24-
getMovieYear
24+
getMovieYear,
25+
getParent,
26+
getSeasonsOrEpisodes
2527
} from '../helpers/movie.helper';
2628
import { movieUrl } from '../vars';
2729

@@ -53,14 +55,15 @@ export class MovieScraper {
5355
pageClasses: string[],
5456
jsonLd: string
5557
) {
58+
const type = getMovieType(el) as CSFDFilmTypes;
5659
this.film = {
5760
id: movieId,
5861
title: getMovieTitle(el),
5962
year: getMovieYear(jsonLd),
6063
duration: getMovieDuration(jsonLd, el),
6164
descriptions: getMovieDescriptions(el),
6265
genres: getMovieGenres(el),
63-
type: getMovieType(el) as CSFDFilmTypes,
66+
type,
6467
url: movieUrl(movieId),
6568
origins: getMovieOrigins(el),
6669
colorRating: getMovieColorRating(pageClasses),
@@ -86,7 +89,10 @@ export class MovieScraper {
8689
tags: getMovieTags(asideEl),
8790
premieres: getMoviePremieres(asideEl),
8891
related: getMovieBoxMovies(asideEl, 'Související'),
89-
similar: getMovieBoxMovies(asideEl, 'Podobné')
92+
similar: getMovieBoxMovies(asideEl, 'Podobné'),
93+
seasons: type === 'seriál' ? getSeasonsOrEpisodes(el) : null,
94+
episodes: type === 'série' ? getSeasonsOrEpisodes(el) : null,
95+
parent: getParent(el)
9096
};
9197
}
9298
}

0 commit comments

Comments
 (0)