Skip to content

Commit 27c1bec

Browse files
committed
fix: more robust parsing
1 parent 621c197 commit 27c1bec

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

src/helpers/cinema.helper.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import { CSFDColorRating } from '../dto/global';
88
import { Colors } from '../dto/user-ratings';
99
import { parseColor, parseIdFromUrl } from './global.helper';
1010

11-
export const getCinemaColorRating = (el: HTMLElement): CSFDColorRating => {
12-
return parseColor(el?.classNames.split(' ').pop() as Colors);
11+
export const getCinemaColorRating = (el: HTMLElement | null): CSFDColorRating => {
12+
const classes: string[] = el?.classNames.split(' ') ?? [];
13+
const last = classes.length ? classes[classes.length - 1] : undefined;
14+
return last ? parseColor(last as Colors) : 'unknown';
1315
};
1416

1517
export const getCinemaId = (el: HTMLElement | null): number => {
1618
const id = el?.id?.split('-')[1];
1719
return +id;
1820
};
1921

20-
export const getCinemaUrlId = (url: string): number | null => {
21-
if (url) {
22-
return parseIdFromUrl(url);
23-
}
24-
return null;
22+
export const getCinemaUrlId = (url: string | null | undefined): number | null => {
23+
if (!url) return null;
24+
return parseIdFromUrl(url);
2525
};
2626

2727
export const getCinemaCoords = (el: HTMLElement | null): { lat: number; lng: number } | null => {
@@ -74,7 +74,7 @@ export const getCinemaFilms = (date: string, el: HTMLElement | null): CSFDCinema
7474

7575
const films = filmNodes.map((filmNode) => {
7676
const url = filmNode.querySelector('td.name h3 a')?.attributes.href;
77-
const id = getCinemaUrlId(url);
77+
const id = url ? getCinemaUrlId(url) : null;
7878
const title = filmNode.querySelector('.name h3')?.text.trim();
7979
const colorRating = getCinemaColorRating(filmNode.querySelector('.name .icon'));
8080
const showTimes = filmNode.querySelectorAll('.td-time')?.map((x) => x.textContent.trim());

src/helpers/creator.helper.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { CSFDColorRating } from '../dto/global';
44
import { Colors } from '../dto/user-ratings';
55
import { addProtocol, parseColor, parseIdFromUrl } from './global.helper';
66

7-
const getCreatorColorRating = (el: HTMLElement): CSFDColorRating => {
8-
return parseColor(el?.classNames.split(' ').pop() as Colors);
7+
const getCreatorColorRating = (el: HTMLElement | null): CSFDColorRating => {
8+
const classes: string[] = el?.classNames.split(' ') ?? [];
9+
const last = classes[classes.length - 1] as Colors | undefined;
10+
return parseColor(last);
911
};
1012

11-
export const getCreatorId = (url: string): number | null => {
12-
if (url) {
13-
return parseIdFromUrl(url);
14-
}
15-
return null;
16-
}
13+
export const getCreatorId = (url: string | null | undefined): number | null => {
14+
return url ? parseIdFromUrl(url) : null;
15+
};
1716

18-
export const getCreatorName = (el: HTMLElement | null): string => {
19-
return el.querySelector('h1').innerText.trim();
17+
export const getCreatorName = (el: HTMLElement | null): string | null => {
18+
const h1 = el?.querySelector('h1');
19+
return h1?.innerText?.trim() ?? null;
2020
};
2121

2222
export const getCreatorBirthdayInfo = (
@@ -42,8 +42,10 @@ export const getCreatorBirthdayInfo = (
4242
return { birthday, age, birthPlace };
4343
};
4444

45-
export const getCreatorBio = (el: HTMLElement | null): string => {
46-
return el.querySelector('.article-content p')?.text.trim().split('\n')[0].trim() || null;
45+
export const getCreatorBio = (el: HTMLElement | null): string | null => {
46+
const p = el?.querySelector('.article-content p');
47+
const first = p?.text?.trim().split('\n')[0]?.trim();
48+
return first || null;
4749
};
4850

4951
export const getCreatorPhoto = (el: HTMLElement | null): string | null => {
@@ -63,32 +65,27 @@ const parseBirthPlace = (text: string): string =>
6365

6466

6567
export const getCreatorFilms = (el: HTMLElement | null): CSFDCreatorScreening[] => {
66-
const filmNodes = el.querySelectorAll('.box')[0]?.querySelectorAll('table tr');
67-
let yearCache: number;
68+
const filmNodes = el?.querySelectorAll('.box')?.[0]?.querySelectorAll('table tr') ?? [];
69+
let yearCache: number | null = null;
6870
const films = filmNodes.map((filmNode) => {
69-
const id = getCreatorId(filmNode.querySelector('td.name .film-title-name')?.attributes.href);
70-
const title = filmNode.querySelector('.name')?.text.trim();
71-
const year = +filmNode.querySelector('.year')?.text.trim();
71+
const id = getCreatorId(filmNode.querySelector('td.name .film-title-name')?.attributes?.href);
72+
const title = filmNode.querySelector('.name')?.text?.trim();
73+
const yearText = filmNode.querySelector('.year')?.text?.trim();
74+
const year = yearText ? +yearText : null;
7275
const colorRating = getCreatorColorRating(filmNode.querySelector('.name .icon'));
7376

7477
// Cache year from previous film because there is a gap between movies with same year
75-
if (year) {
78+
if (typeof year === 'number' && !isNaN(year)) {
7679
yearCache = +year;
7780
}
7881

79-
if (id && title) {
80-
return {
81-
id,
82-
title,
83-
year: year || yearCache,
84-
colorRating
85-
};
82+
const finalYear = year ?? yearCache;
83+
if (id != null && title && finalYear != null) {
84+
return { id, title, year: finalYear, colorRating };
8685
}
87-
return {};
86+
return null;
8887
});
8988
// Remove empty objects
90-
const filmsUnique = films.filter(
91-
(value) => Object.keys(value).length !== 0
92-
) as CSFDCreatorScreening[];
89+
const filmsUnique = films.filter(Boolean) as CSFDCreatorScreening[];
9390
return filmsUnique;
9491
};

src/helpers/search.helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Creator = 'Režie:' | 'Hrají:';
88

99
export const getSearchType = (el: HTMLElement): CSFDFilmTypes => {
1010
const type = el.querySelectorAll('.film-title-info .info')[1];
11-
return (type?.innerText.replace(/[{()}]/g, '') || 'film') as CSFDFilmTypes;
11+
return (type?.innerText?.replace(/[{()}]/g, '')?.trim() || 'film') as CSFDFilmTypes;
1212
};
1313

1414
export const getSearchTitle = (el: HTMLElement): string => {

0 commit comments

Comments
 (0)