Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dto/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export interface CSFDOptions {
request?: RequestInit;
}

export type CSFDLanguage = 'cs' | 'en' | 'sk';
export type CSFDLanguage = 'cs' | 'en' | 'sk';
4 changes: 3 additions & 1 deletion src/helpers/search-user.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const getUserRealName = (el: HTMLElement): string => {
const p = el.querySelector('.article-content p');
if (!p) return null;

const textNodes = p.childNodes.filter(n => n.nodeType === NodeType.TEXT_NODE && n.rawText.trim() !== '');
const textNodes = p.childNodes.filter(
(n) => n.nodeType === NodeType.TEXT_NODE && n.rawText.trim() !== ''
);
const name = textNodes.length ? textNodes[0].rawText.trim() : null;

return name;
Expand Down
47 changes: 26 additions & 21 deletions src/helpers/search.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { CSFDMovieCreator } from '../dto/movie';
import { CSFDColors } from '../dto/user-ratings';
import { addProtocol, parseColor, parseFilmType, parseIdFromUrl } from './global.helper';

type Creator = 'Režie:' | 'Hrají:';

export const getSearchType = (el: HTMLElement): CSFDFilmTypes => {
const type = el.querySelectorAll('.film-title-info .info')[1];
return parseFilmType(type?.innerText?.replace(/[{()}]/g, '')?.trim() || 'film');
Expand Down Expand Up @@ -42,29 +40,36 @@ export const getSearchOrigins = (el: HTMLElement): string[] => {
return originsAll?.split('/').map((country) => country.trim());
};

export const parseSearchPeople = (
el: HTMLElement,
type: 'directors' | 'actors'
): CSFDMovieCreator[] => {
let who: Creator;
if (type === 'directors') who = 'Režie:';
if (type === 'actors') who = 'Hrají:';

const peopleNode = Array.from(el && el.querySelectorAll('.article-content p')).find((el) =>
el.textContent.includes(who)
);
export const parseSearchCreators = (
el: HTMLElement
): { directors: CSFDMovieCreator[]; actors: CSFDMovieCreator[] } => {
const creators = {
directors: [] as CSFDMovieCreator[],
actors: [] as CSFDMovieCreator[]
};

if (peopleNode) {
const people = Array.from(peopleNode.querySelectorAll('a')) as unknown as HTMLElement[];
// Optimization: Consolidate repeated DOM traversals for directors and actors into a single pass
const peopleNodes = el?.querySelectorAll('.article-content p');
if (!peopleNodes || !peopleNodes.length) return creators;

return people.map((person) => {
return {
for (const node of peopleNodes) {
const text = node.textContent;
if (text.includes('Režie:')) {
const people = node.querySelectorAll('a');
creators.directors = people.map((person) => ({
id: parseIdFromUrl(person.attributes.href),
name: person.innerText.trim(),
url: `https://www.csfd.cz${person.attributes.href}`
};
});
} else {
return [];
}));
} else if (text.includes('Hrají:')) {
const people = node.querySelectorAll('a');
creators.actors = people.map((person) => ({
id: parseIdFromUrl(person.attributes.href),
name: person.innerText.trim(),
url: `https://www.csfd.cz${person.attributes.href}`
}));
}
Comment on lines +55 to +71
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Prevent silent creator data loss in mixed/split paragraphs.

Current logic can drop results: else if skips actors when both labels occur in one <p>, and = overwrites previously parsed entries if matching blocks repeat.

💡 Proposed fix
   for (const node of peopleNodes) {
     const text = node.textContent;
     if (text.includes('Režie:')) {
       const people = node.querySelectorAll('a');
-      creators.directors = people.map((person) => ({
+      creators.directors.push(...people.map((person) => ({
         id: parseIdFromUrl(person.attributes.href),
         name: person.innerText.trim(),
         url: `https://www.csfd.cz${person.attributes.href}`
-      }));
-    } else if (text.includes('Hrají:')) {
+      })));
+    }
+    if (text.includes('Hrají:')) {
       const people = node.querySelectorAll('a');
-      creators.actors = people.map((person) => ({
+      creators.actors.push(...people.map((person) => ({
         id: parseIdFromUrl(person.attributes.href),
         name: person.innerText.trim(),
         url: `https://www.csfd.cz${person.attributes.href}`
-      }));
+      })));
     }
   }

As per coding guidelines: "Never assume an element exists. CSFD changes layouts. Use optional chaining ?. or try/catch inside helpers for robust scraping."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/helpers/search.helper.ts` around lines 55 - 71, The loop over peopleNodes
currently uses an else if and direct assignment which can drop or overwrite
creators; change it to independent checks for both labels (use if instead of
else if), safely access DOM properties with optional chaining (e.g.,
node.textContent?.includes, person.attributes?.href, person.innerText?.trim()),
build people arrays from Array.from(node.querySelectorAll('a') || []), and merge
results into creators.directors/creators.actors (e.g., concatenate to existing
arrays and deduplicate by id) rather than replacing them; use parseIdFromUrl
when href exists and skip entries missing required fields so scraping remains
robust.

}

return creators;
};
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,3 @@ export const csfd = new Csfd(
);

export type * from './dto';

7 changes: 4 additions & 3 deletions src/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getSearchType,
getSearchUrl,
getSearchYear,
parseSearchPeople
parseSearchCreators
} from '../helpers/search.helper';
import { CSFDLanguage, CSFDOptions } from '../types';
import { getUrlByLanguage, searchUrl } from '../vars';
Expand Down Expand Up @@ -47,6 +47,7 @@ export class SearchScraper {

const movieMapper = (m: HTMLElement): CSFDSearchMovie => {
const url = getSearchUrl(m);
const parsedCreators = parseSearchCreators(m);
return {
id: parseIdFromUrl(url),
title: getSearchTitle(m),
Expand All @@ -57,8 +58,8 @@ export class SearchScraper {
poster: getSearchPoster(m),
origins: getSearchOrigins(m),
creators: {
directors: parseSearchPeople(m, 'directors'),
actors: parseSearchPeople(m, 'actors')
directors: parsedCreators.directors,
actors: parsedCreators.actors
}
};
};
Expand Down
18 changes: 8 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export * from "./dto/cinema";
export * from "./dto/creator";
export * from "./dto/global";
export * from "./dto/movie";
export * from "./dto/options";
export * from "./dto/search";
export * from "./dto/user-ratings";
export * from "./dto/user-reviews";


export * from './dto/cinema';
export * from './dto/creator';
export * from './dto/global';
export * from './dto/movie';
export * from './dto/options';
export * from './dto/search';
export * from './dto/user-ratings';
export * from './dto/user-reviews';
25 changes: 17 additions & 8 deletions src/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Options = {
const LANGUAGE_DOMAIN_MAP: Record<CSFDLanguage, string> = {
cs: 'https://www.csfd.cz',
en: 'https://www.csfd.cz/en',
sk: 'https://www.csfd.cz/sk',
sk: 'https://www.csfd.cz/sk'
};

let BASE_URL = LANGUAGE_DOMAIN_MAP.cs;
Expand All @@ -30,10 +30,16 @@ export const getUrlByLanguage = (language?: CSFDLanguage): string => {
export const userUrl = (user: string | number, options: Options): string =>
`${getUrlByLanguage(options?.language)}/uzivatel/${encodeURIComponent(user)}`;

export const userRatingsUrl = (user: string | number, page?: number, options: Options = {}): string =>
`${userUrl(user, options)}/hodnoceni/${page ? '?page=' + page : ''}`;
export const userReviewsUrl = (user: string | number, page?: number, options: Options = {}): string =>
`${userUrl(user, options)}/recenze/${page ? '?page=' + page : ''}`;
export const userRatingsUrl = (
user: string | number,
page?: number,
options: Options = {}
): string => `${userUrl(user, options)}/hodnoceni/${page ? '?page=' + page : ''}`;
export const userReviewsUrl = (
user: string | number,
page?: number,
options: Options = {}
): string => `${userUrl(user, options)}/recenze/${page ? '?page=' + page : ''}`;

// Movie URLs
export const movieUrl = (movie: number, options: Options): string =>
Expand All @@ -43,9 +49,12 @@ export const creatorUrl = (creator: number | string, options: Options): string =
`${getUrlByLanguage(options?.language)}/tvurce/${encodeURIComponent(creator)}`;

// Cinema URLs
export const cinemasUrl = (district: number | string, period: CSFDCinemaPeriod, options: Options): string =>
`${getUrlByLanguage(options?.language)}/kino/?period=${period}&district=${district}`;
export const cinemasUrl = (
district: number | string,
period: CSFDCinemaPeriod,
options: Options
): string => `${getUrlByLanguage(options?.language)}/kino/?period=${period}&district=${district}`;

// Search URLs
export const searchUrl = (text: string, options: Options): string =>
`${getUrlByLanguage(options?.language)}/hledat/?q=${encodeURIComponent(text)}`;
`${getUrlByLanguage(options?.language)}/hledat/?q=${encodeURIComponent(text)}`;
33 changes: 14 additions & 19 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getSearchType,
getSearchUrl,
getSearchYear,
parseSearchPeople
parseSearchCreators
} from '../src/helpers/search.helper';
import { searchMock } from './mocks/search.html';

Expand Down Expand Up @@ -147,8 +147,8 @@ describe('Get Movie origins', () => {

describe('Get Movie creators', () => {
test('First movie directors', () => {
const movie = parseSearchPeople(moviesNode[0], 'directors');
expect(movie).toEqual<CSFDMovieCreator[]>([
const creators = parseSearchCreators(moviesNode[0]);
expect(creators.directors).toEqual<CSFDMovieCreator[]>([
{
id: 3112,
name: 'Lilly Wachowski',
Expand All @@ -162,8 +162,8 @@ describe('Get Movie creators', () => {
]);
});
test('Last movie actors', () => {
const movie = parseSearchPeople(moviesNode[moviesNode.length - 1], 'actors');
expect(movie).toEqual<CSFDMovieCreator[]>([
const creators = parseSearchCreators(moviesNode[moviesNode.length - 1]);
expect(creators.actors).toEqual<CSFDMovieCreator[]>([
{
id: 101,
name: 'Carrie-Anne Moss',
Expand All @@ -176,10 +176,6 @@ describe('Get Movie creators', () => {
}
]);
});
// test('Empty actors', () => {
// const movie = parseSearchPeople(moviesNode[5], 'actors');
// expect(movie).toEqual<CSFDCreator[]>([]);
// });
});

// TV SERIES
Expand Down Expand Up @@ -295,8 +291,8 @@ describe('Get TV series origins', () => {

describe('Get TV series creators', () => {
test('First TV series directors', () => {
const movie = parseSearchPeople(tvSeriesNode[0], 'directors');
expect(movie).toEqual<CSFDMovieCreator[]>([
const creators = parseSearchCreators(tvSeriesNode[0]);
expect(creators.directors).toEqual<CSFDMovieCreator[]>([
{
id: 8877,
name: 'Allan Eastman',
Expand All @@ -310,8 +306,8 @@ describe('Get TV series creators', () => {
]);
});
test('Last TV series actors', () => {
const movie = parseSearchPeople(tvSeriesNode[tvSeriesNode.length - 1], 'actors');
expect(movie).toEqual<CSFDMovieCreator[]>([
const creators = parseSearchCreators(tvSeriesNode[tvSeriesNode.length - 1]);
expect(creators.actors).toEqual<CSFDMovieCreator[]>([
{
id: 74751,
name: 'Takeru Sató',
Expand All @@ -325,20 +321,19 @@ describe('Get TV series creators', () => {
]);
});
test('Empty directors', () => {
const movie = parseSearchPeople(tvSeriesNode[3], 'directors');
expect(movie).toEqual<CSFDMovieCreator[]>([]);
const creators = parseSearchCreators(tvSeriesNode[3]);
expect(creators.directors).toEqual<CSFDMovieCreator[]>([]);
});
test('Empty directors + some actors', () => {
const movie = parseSearchPeople(tvSeriesNode[3], 'actors');
const movieDirectors = parseSearchPeople(tvSeriesNode[3], 'directors');
expect(movie).toEqual<CSFDMovieCreator[]>([
const creators = parseSearchCreators(tvSeriesNode[3]);
expect(creators.actors).toEqual<CSFDMovieCreator[]>([
{
id: 61834,
name: 'David Icke',
url: 'https://www.csfd.cz/tvurce/61834-david-icke/prehled/'
}
]);
expect(movieDirectors).toEqual<CSFDMovieCreator[]>([]);
expect(creators.directors).toEqual<CSFDMovieCreator[]>([]);
});
});

Expand Down
Loading