Skip to content

Commit 9fa8caf

Browse files
committed
feat(tv-series): seasons, episodes [kickoff]
1 parent 7d6498f commit 9fa8caf

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

demo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { csfd } from './src';
44
// csfd.setOptions({ optionsRequest: { credentials: 'include' } });
55

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

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

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

1515
/**
1616
* 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 =
@@ -86,6 +94,8 @@ export interface CSFDMovieListItem {
8694
url: string;
8795
}
8896

97+
98+
8999
export type CSFDGenres =
90100
| 'Akční'
91101
| 'Animovaný'
@@ -181,3 +191,10 @@ export interface CSFDPremiere {
181191
}
182192

183193
export type CSFDBoxContent = 'Související' | 'Podobné';
194+
195+
export interface CSFDSeason {
196+
id: number;
197+
name: string;
198+
url: string;
199+
info: string | null;
200+
}

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
@@ -8,12 +8,14 @@ import {
88
CSFDGenres,
99
CSFDMovieCreator,
1010
CSFDMovieListItem,
11+
CSFDParent,
1112
CSFDPremiere,
13+
CSFDSeason,
1214
CSFDTitlesOther,
1315
CSFDVod,
1416
CSFDVodService
1517
} from '../dto/movie';
16-
import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl } from './global.helper';
18+
import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl, parseLastIdFromUrl } from './global.helper';
1719

1820
/**
1921
* Maps language-specific movie creator group labels.
@@ -241,6 +243,42 @@ const parseMoviePeople = (el: HTMLElement): CSFDMovieCreator[] => {
241243
);
242244
};
243245

246+
export const getSeasonsOrEpisodes = (el: HTMLElement): CSFDSeason[] | null => {
247+
const childrenList = el.querySelector('.film-episodes-list');
248+
if (!childrenList) return null;
249+
250+
const childrenNodes = childrenList.querySelectorAll('.film-title');
251+
if (!childrenNodes?.length) return [];
252+
253+
return childrenNodes.map((season) => {
254+
const nameContainer = season.querySelector('.film-title-name');
255+
const infoContainer = season.querySelector('.info');
256+
257+
return {
258+
id: parseLastIdFromUrl(nameContainer?.getAttribute('href') || ''),
259+
name: nameContainer?.textContent?.trim() || null,
260+
url: nameContainer?.getAttribute('href') || null,
261+
info: infoContainer?.textContent?.replace(/[{()}]/g, '').trim() || null,
262+
};
263+
});
264+
}
265+
266+
export const getParent = (el: HTMLElement): CSFDParent | null => {
267+
const parents = el.querySelectorAll('.film-header h2 a');
268+
if (parents.length === 0) return null;
269+
270+
const [parentSeries, parentSeason] = parents;
271+
272+
return {
273+
series: {
274+
id: parseIdFromUrl(parentSeries?.getAttribute('href') || ''), name: parentSeries?.textContent?.trim() || null
275+
},
276+
season: {
277+
id: parseIdFromUrl(parentSeason?.getAttribute('href') || ''), name: parentSeason?.textContent?.trim() || null
278+
}
279+
};
280+
}
281+
244282
export const getMovieGroup = (el: HTMLElement, group: CSFDCreatorGroups | CSFDCreatorGroupsEnglish | CSFDCreatorGroupsSlovak): CSFDMovieCreator[] => {
245283
const creators = el.querySelectorAll('.creators h4');
246284
const element = creators.filter((elem) => elem.textContent.trim().includes(group))[0];

src/services/movie.service.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {
2222
getMovieTrivia,
2323
getMovieType,
2424
getMovieVods,
25-
getMovieYear
25+
getMovieYear,
26+
getParent,
27+
getSeasonsOrEpisodes
2628
} from '../helpers/movie.helper';
2729
import { CSFDOptions } from '../types';
2830
import { movieUrl } from '../vars';
@@ -52,15 +54,16 @@ export class MovieScraper {
5254
pageClasses: string[],
5355
jsonLd: string,
5456
options: CSFDOptions
55-
): CSFDMovie {
57+
) {
58+
const type = getMovieType(el) as CSFDFilmTypes;
5659
return {
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, { language: options?.language }),
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)