Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/dto/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ export interface CSFDPremiere {
}

export type CSFDBoxContent = 'Související' | 'Podobné';

export interface MovieJsonLd {
dateCreated: string;
duration: string;
[key: string]: any;
}
31 changes: 17 additions & 14 deletions src/helpers/movie.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
CSFDPremiere,
CSFDTitlesOther,
CSFDVod,
CSFDVodService
CSFDVodService,
MovieJsonLd
} from '../dto/movie';
import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl } from './global.helper';

Expand Down Expand Up @@ -123,23 +124,23 @@ export const getMovieRatingCount = (el: HTMLElement): number => {
}
};

export const getMovieYear = (el: string): number => {
try {
const jsonLd = JSON.parse(el);
export const getMovieYear = (jsonLd: MovieJsonLd | null): number => {
if (jsonLd) {
return +jsonLd.dateCreated;
} catch (error) {
console.error('node-csfd-api: Error parsing JSON-LD', error);
return null;
}
return null;
};

export const getMovieDuration = (jsonLdRaw: string, el: HTMLElement): number => {
let duration = null;
export const getMovieDuration = (jsonLd: MovieJsonLd | null, el: HTMLElement): number => {
try {
if (jsonLd && jsonLd.duration) {
return parseISO8601Duration(jsonLd.duration);
}
} catch (e) {
// Ignore error
}

try {
const jsonLd = JSON.parse(jsonLdRaw);
duration = jsonLd.duration;
return parseISO8601Duration(duration);
} catch (error) {
const origin = el.querySelector('.origin').innerText;
const timeString = origin.split(',');
if (timeString.length > 2) {
Expand All @@ -151,11 +152,13 @@ export const getMovieDuration = (jsonLdRaw: string, el: HTMLElement): number =>
const hoursMinsRaw = timeRaw.split('min')[0];
const hoursMins = hoursMinsRaw.split('h');
// Resolve hours + minutes format
duration = hoursMins.length > 1 ? +hoursMins[0] * 60 + +hoursMins[1] : +hoursMins[0];
const duration = hoursMins.length > 1 ? +hoursMins[0] * 60 + +hoursMins[1] : +hoursMins[0];
return duration;
} else {
return null;
}
} catch (e) {
return null;
}
};

Expand Down
12 changes: 9 additions & 3 deletions src/services/movie.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HTMLElement, parse } from 'node-html-parser';
import { CSFDFilmTypes } from '../dto/global';
import { CSFDMovie } from '../dto/movie';
import { CSFDMovie, MovieJsonLd } from '../dto/movie';
import { fetchPage } from '../fetchers';
import {
getLocalizedCreatorLabel,
Expand Down Expand Up @@ -41,7 +41,13 @@ export class MovieScraper {
const pageClasses = movieHtml.querySelector('.page-content').classNames.split(' ');
const asideNode = movieHtml.querySelector('.aside-movie-profile');
const movieNode = movieHtml.querySelector('.main-movie-profile');
const jsonLd = movieHtml.querySelector('script[type="application/ld+json"]').innerText;
const jsonLdString = movieHtml.querySelector('script[type="application/ld+json"]').innerText;
let jsonLd: MovieJsonLd | null = null;
try {
jsonLd = JSON.parse(jsonLdString);
} catch (e) {
console.error('node-csfd-api: Error parsing JSON-LD', e);
}
return this.buildMovie(+movieId, movieNode, asideNode, pageClasses, jsonLd, options);
}

Expand All @@ -50,7 +56,7 @@ export class MovieScraper {
el: HTMLElement,
asideEl: HTMLElement,
pageClasses: string[],
jsonLd: string,
jsonLd: MovieJsonLd | null,
options: CSFDOptions
): CSFDMovie {
return {
Expand Down
19 changes: 15 additions & 4 deletions tests/movie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
CSFDMovieListItem,
CSFDPremiere,
CSFDTitlesOther,
CSFDVod
CSFDVod,
MovieJsonLd
} from '../src/dto/movie';
import { getColor } from '../src/helpers/global.helper';
import {
Expand Down Expand Up @@ -48,13 +49,23 @@ const getNode = (node: HTMLElement): HTMLElement => {
return node.querySelector('.main-movie-profile') as HTMLElement;
};

const getJsonLd = (node: HTMLElement): string => {
return node.querySelector('script[type="application/ld+json"]')?.innerText ?? '{}';
const getJsonLd = (node: HTMLElement): MovieJsonLd | null => {
const text = node.querySelector('script[type="application/ld+json"]')?.innerText ?? '{}';
try {
return JSON.parse(text);
} catch (e) {
return null;
}
};
Comment on lines +52 to 59
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 | 🟡 Minor

?? '{}' fallback returns a truthy empty object, breaking getMovieYear for pages without a JSON-LD tag.

When script[type="application/ld+json"] is absent, innerText is undefined, text becomes '{}', and JSON.parse('{}') returns {} — a truthy object with no dateCreated or duration. Any subsequent call to getMovieYear({}) will execute the if (jsonLd) branch and return +undefined (i.e., NaN) instead of null.

Return null directly when the tag is missing to stay consistent with the MovieJsonLd | null contract:

✏️ Proposed fix
 const getJsonLd = (node: HTMLElement): MovieJsonLd | null => {
-  const text = node.querySelector('script[type="application/ld+json"]')?.innerText ?? '{}';
-  try {
-    return JSON.parse(text);
-  } catch (e) {
-    return null;
-  }
+  const text = node.querySelector('script[type="application/ld+json"]')?.innerText;
+  if (!text) return null;
+  try {
+    return JSON.parse(text) as MovieJsonLd;
+  } catch (e) {
+    return null;
+  }
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const getJsonLd = (node: HTMLElement): MovieJsonLd | null => {
const text = node.querySelector('script[type="application/ld+json"]')?.innerText ?? '{}';
try {
return JSON.parse(text);
} catch (e) {
return null;
}
};
const getJsonLd = (node: HTMLElement): MovieJsonLd | null => {
const text = node.querySelector('script[type="application/ld+json"]')?.innerText;
if (!text) return null;
try {
return JSON.parse(text) as MovieJsonLd;
} catch (e) {
return null;
}
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/movie.test.ts` around lines 52 - 59, getJsonLd currently falls back to
parsing the string '{}' which produces a truthy empty object and breaks
consumers like getMovieYear; change getJsonLd to detect absence of the script
element and return null immediately instead of using '?? "{}"', i.e., capture
the result of node.querySelector('script[type="application/ld+json"]') into a
variable, if it's null return null, otherwise read its innerText and JSON.parse
inside the try/catch to return MovieJsonLd or null; update references to
getJsonLd and ensure getMovieYear receives null when no JSON‑LD is present.


const getMovie = (
node: HTMLElement
): { pClasses: string[]; aside: HTMLElement; pNode: HTMLElement; jsonLd: string } => {
): {
pClasses: string[];
aside: HTMLElement;
pNode: HTMLElement;
jsonLd: MovieJsonLd | null;
} => {
return {
pClasses: getPageClasses(node),
aside: getAsideNode(node),
Expand Down