Skip to content

Commit fafd5d6

Browse files
committed
feat(movie): episodeCode + seasons
1 parent 3c208a0 commit fafd5d6

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

src/dto/movie.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface CSFDMovie extends CSFDScreening {
2020
seasons: CSFDSeason[] | null;
2121
episodes: CSFDSeason[] | null;
2222
parent: CSFDParent | null;
23+
episodeCode: string | null;
24+
seasonName: string | null;
2325
}
2426

2527
export interface CSFDParent {

src/helpers/movie.helper.ts

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ export const getMovieId = (el: HTMLElement): number => {
2020
return parseIdFromUrl(url);
2121
};
2222

23+
export const getSerieasAndSeasonTitle = (el: HTMLElement): { seriesName: string; seasonName: string | null } => {
24+
const titleElement = el.querySelector('h1');
25+
if (!titleElement) {
26+
return { seriesName: null, seasonName: null };
27+
}
28+
29+
const fullText = titleElement.innerText.trim();
30+
31+
// Check if there's a series part indicated by ' - '
32+
if (fullText.includes(' - ')) {
33+
const [seriesName, seasonName] = fullText.split(' - ').map(part => part.trim());
34+
return { seriesName, seasonName };
35+
}
36+
37+
// If no series part found, return just the name
38+
return { seriesName: fullText, seasonName: null };
39+
};
40+
2341
export const getMovieTitle = (el: HTMLElement): string => {
2442
return el.querySelector('h1').innerText.split(`(`)[0].trim();
2543
};
@@ -179,7 +197,7 @@ const parseMoviePeople = (el: HTMLElement): CSFDMovieCreator[] => {
179197
);
180198
};
181199

182-
export const getSeasonsOrEpisodes = (el: HTMLElement): CSFDSeason[] | null => {
200+
export const getSeasonsOrEpisodes = (el: HTMLElement, serie?: { id: number; title: string; }): CSFDSeason[] | null => {
183201
const childrenList = el.querySelector('.film-episodes-list');
184202
if (!childrenList) return null;
185203

@@ -199,20 +217,45 @@ export const getSeasonsOrEpisodes = (el: HTMLElement): CSFDSeason[] | null => {
199217
});
200218
}
201219

202-
export const getParent = (el: HTMLElement): CSFDParent | null => {
220+
export const getEpisodeCode = (el: HTMLElement): string | null => {
221+
const filmHeaderName = el.querySelector('.film-header-name h1');
222+
if (!filmHeaderName) return null;
223+
224+
const text = filmHeaderName.textContent?.trim() || '';
225+
const match = text.match(/\(([^)]+)\)/);
226+
const code = match ? match[1] : null;
227+
228+
return code;
229+
}
230+
231+
export const detectSeasonOrEpisodeListType = (el: HTMLElement) => {
232+
const headerText = el.querySelector('.box-header h3')?.innerText.trim() ?? '';
233+
234+
if (headerText.includes('Série')) return 'seasons';
235+
if (headerText.startsWith('Epizody')) return 'episodes';
236+
return null;
237+
}
238+
239+
export const getSeasonorEpisodeParent = (el: HTMLElement, serie?: { id: number; name: string; }): CSFDParent | null => {
203240
const parents = el.querySelectorAll('.film-header h2 a');
204-
if (parents.length === 0) return null;
241+
if (parents.length === 0) {
242+
if (!serie) return null;
243+
return { series: serie, season: null };
244+
}
205245

206246
const [parentSeries, parentSeason] = parents;
207247

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-
};
248+
const seriesId = parseIdFromUrl(parentSeries?.getAttribute('href'));
249+
const seasonId = parseIdFromUrl(parentSeason?.getAttribute('href'));
250+
const seriesName = parentSeries?.textContent?.trim() || null;
251+
const seasonName = parentSeason?.textContent?.trim() || null;
252+
253+
const series = seriesId && seriesName ? { id: seriesId, name: seriesName } : null;
254+
const season = seasonId && seasonName ? { id: seasonId, name: seasonName } : null;
255+
256+
if (!series && !season) return null;
257+
258+
return { series, season };
216259
}
217260

218261
export const getMovieGroup = (el: HTMLElement, group: CSFDCreatorGroups): CSFDMovieCreator[] => {

src/services/movie.service.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { CSFDFilmTypes } from '../dto/global';
33
import { CSFDMovie } from '../dto/movie';
44
import { fetchPage } from '../fetchers';
55
import {
6+
detectSeasonOrEpisodeListType,
7+
getEpisodeCode,
68
getMovieBoxMovies,
79
getMovieColorRating,
810
getMovieDescriptions,
@@ -22,8 +24,9 @@ import {
2224
getMovieType,
2325
getMovieVods,
2426
getMovieYear,
25-
getParent,
26-
getSeasonsOrEpisodes
27+
getSeasonorEpisodeParent,
28+
getSeasonsOrEpisodes,
29+
getSerieasAndSeasonTitle
2730
} from '../helpers/movie.helper';
2831
import { movieUrl } from '../vars';
2932

@@ -56,9 +59,13 @@ export class MovieScraper {
5659
jsonLd: string
5760
) {
5861
const type = getMovieType(el) as CSFDFilmTypes;
62+
const { seriesName = null, seasonName = null } = type === 'série' ? getSerieasAndSeasonTitle(el) : {};
63+
const seasonOrEpisodeListType = detectSeasonOrEpisodeListType(el);
64+
65+
const title = type === 'série' && seriesName ? seriesName : getMovieTitle(el);
5966
this.film = {
6067
id: movieId,
61-
title: getMovieTitle(el),
68+
title,
6269
year: getMovieYear(jsonLd),
6370
duration: getMovieDuration(jsonLd, el),
6471
descriptions: getMovieDescriptions(el),
@@ -83,16 +90,18 @@ export class MovieScraper {
8390
producers: getMovieGroup(el, 'Produkce'),
8491
filmEditing: getMovieGroup(el, 'Střih'),
8592
costumeDesign: getMovieGroup(el, 'Kostýmy'),
86-
productionDesign: getMovieGroup(el, 'Scénografie')
93+
productionDesign: getMovieGroup(el, 'Scénografie'),
8794
},
8895
vod: getMovieVods(asideEl),
8996
tags: getMovieTags(asideEl),
9097
premieres: getMoviePremieres(asideEl),
9198
related: getMovieBoxMovies(asideEl, 'Související'),
9299
similar: getMovieBoxMovies(asideEl, 'Podobné'),
93-
seasons: type === 'seriál' ? getSeasonsOrEpisodes(el) : null,
94-
episodes: type === 'série' ? getSeasonsOrEpisodes(el) : null,
95-
parent: getParent(el)
100+
seasons: seasonOrEpisodeListType === 'seasons' ? getSeasonsOrEpisodes(el) : null,
101+
episodes: seasonOrEpisodeListType === 'episodes' ? getSeasonsOrEpisodes(el) : null,
102+
parent: (type === 'seriál') ? null : getSeasonorEpisodeParent(el, { id: movieId, name: title }),
103+
episodeCode: type === 'epizoda' ? getEpisodeCode(el) : null,
104+
seasonName,
96105
};
97106
}
98107
}

0 commit comments

Comments
 (0)