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
12 changes: 11 additions & 1 deletion src/bin/export-reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ export async function runReviewsExport(userId: number, options: ExportReviewsOpt
content = JSON.stringify(reviews, null, 2);
fileName = `${userId}-reviews.json`;
} else {
const headers = ['id', 'title', 'year', 'type', 'colorRating', 'userRating', 'date', 'url', 'text'];
const headers = [
'id',
'title',
'year',
'type',
'colorRating',
'userRating',
'date',
'url',
'text'
];
content = [
headers.join(','),
...reviews.map((r) =>
Expand Down
35 changes: 23 additions & 12 deletions src/bin/lookup-movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ export async function runMovieLookup(movieId: number, json: boolean): Promise<vo

function printMovie(movie: CSFDMovie) {
const ratingColor =
movie.colorRating === 'good' ? c.green :
movie.colorRating === 'average' ? c.yellow :
movie.colorRating === 'bad' ? c.red : c.dim;
movie.colorRating === 'good'
? c.green
: movie.colorRating === 'average'
? c.yellow
: movie.colorRating === 'bad'
? c.red
: c.dim;

const row = (label: string, value: string) =>
value ? ` ${c.dim(label.padEnd(11))} ${value}` : '';

const names = (arr: { name: string }[], max = 5) =>
arr.slice(0, max).map((x) => x.name).join(', ');
arr
.slice(0, max)
.map((x) => x.name)
.join(', ');

const description = movie.descriptions?.[0]
? movie.descriptions[0].length > 160
Expand All @@ -35,18 +42,22 @@ function printMovie(movie: CSFDMovie) {
'',
c.bold(movie.title) + c.dim(` (${movie.year ?? '?'})`) + ' · ' + c.dim(movie.type ?? ''),
c.dim('─'.repeat(52)),
row('Rating', movie.rating != null
? ratingColor(c.bold(movie.rating + '%')) + c.dim(` (${movie.ratingCount?.toLocaleString()} ratings)`)
: c.dim('no rating')),
row('Genres', movie.genres?.join(', ') ?? ''),
row('Origins', movie.origins?.join(', ') ?? ''),
row('Duration', movie.duration ? movie.duration + ' min' : ''),
row(
'Rating',
movie.rating != null
? ratingColor(c.bold(movie.rating + '%')) +
c.dim(` (${movie.ratingCount?.toLocaleString()} ratings)`)
: c.dim('no rating')
),
row('Genres', movie.genres?.join(', ') ?? ''),
row('Origins', movie.origins?.join(', ') ?? ''),
row('Duration', movie.duration ? movie.duration + ' min' : ''),
row('Directors', names(movie.creators?.directors ?? [])),
row('Cast', names(movie.creators?.actors ?? [])),
row('Cast', names(movie.creators?.actors ?? [])),
description ? '\n ' + c.dim(description) : '',
vod ? '\n' + row('VOD', vod) : '',
row('URL', c.dim(movie.url ?? '')),
'',
''
].filter(Boolean);

console.log(lines.join('\n'));
Expand Down
29 changes: 20 additions & 9 deletions src/bin/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ export async function runSearch(query: string, json: boolean): Promise<void> {

function printSearch(query: string, results: CSFDSearch) {
const ratingDot = (colorRating: string | null) =>
colorRating === 'good' ? c.green('●') :
colorRating === 'average' ? c.yellow('●') :
colorRating === 'bad' ? c.red('●') : c.dim('●');
colorRating === 'good'
? c.green('●')
: colorRating === 'average'
? c.yellow('●')
: colorRating === 'bad'
? c.red('●')
: c.dim('●');

const section = (label: string, count: number) =>
count > 0 ? `\n${c.bold(label)} ${c.dim(`(${count})`)}` : null;

const total = results.movies.length + results.tvSeries.length + results.creators.length + results.users.length;
const total =
results.movies.length +
results.tvSeries.length +
results.creators.length +
results.users.length;

console.log('');
console.log(`${c.bold('Search results for')} ${c.cyan(`"${query}"`)} ${c.dim(`— ${total} found`)}`);
console.log(
`${c.bold('Search results for')} ${c.cyan(`"${query}"`)} ${c.dim(`— ${total} found`)}`
);
console.log(c.dim('─'.repeat(52)));

const movieLine = (r: CSFDSearch['movies'][0]) =>
Expand All @@ -43,15 +53,16 @@ function printSearch(query: string, results: CSFDSearch) {

if (results.creators.length > 0) {
console.log(section('Creators', results.creators.length));
results.creators.forEach((r) =>
console.log(` ${c.dim(String(r.id).padEnd(8))} ${r.name}`)
);
results.creators.forEach((r) => console.log(` ${c.dim(String(r.id).padEnd(8))} ${r.name}`));
}

if (results.users.length > 0) {
console.log(section('Users', results.users.length));
results.users.forEach((r) =>
console.log(` ${c.dim(String(r.id).padEnd(8))} ${r.user}` + (r.userRealName ? c.dim(` (${r.userRealName})`) : ''))
console.log(
` ${c.dim(String(r.id).padEnd(8))} ${r.user}` +
(r.userRealName ? c.dim(` (${r.userRealName})`) : '')
)
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
export const useColor = process.stdout.isTTY && !process.env['NO_COLOR'];

export const c = {
bold: (s: string) => useColor ? `\x1b[1m${s}\x1b[22m` : s,
dim: (s: string) => useColor ? `\x1b[2m${s}\x1b[22m` : s,
cyan: (s: string) => useColor ? `\x1b[36m${s}\x1b[39m` : s,
green: (s: string) => useColor ? `\x1b[32m${s}\x1b[39m` : s,
yellow: (s: string) => useColor ? `\x1b[33m${s}\x1b[39m` : s,
red: (s: string) => useColor ? `\x1b[31m${s}\x1b[39m` : s,
bold: (s: string) => (useColor ? `\x1b[1m${s}\x1b[22m` : s),
dim: (s: string) => (useColor ? `\x1b[2m${s}\x1b[22m` : s),
cyan: (s: string) => (useColor ? `\x1b[36m${s}\x1b[39m` : s),
green: (s: string) => (useColor ? `\x1b[32m${s}\x1b[39m` : s),
yellow: (s: string) => (useColor ? `\x1b[33m${s}\x1b[39m` : s),
red: (s: string) => (useColor ? `\x1b[31m${s}\x1b[39m` : s)
};

export const err = (msg: string) => c.red(c.bold('✖ Error:')) + ' ' + msg;
Expand Down
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: 2 additions & 2 deletions src/helpers/creator.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement } from 'node-html-parser';
import { CSFDCreatorScreening } from '../dto/creator';
import { CSFDColorRating } from '../dto/global';
import { CSFDColors } from '../dto/user-ratings';
import { addProtocol, parseColor, parseDate, parseIdFromUrl } from './global.helper';
import { addProtocol, getFirstLine, parseColor, parseDate, parseIdFromUrl } from './global.helper';

const getCreatorColorRating = (el: HTMLElement | null): CSFDColorRating => {
const classes: string[] = el?.classNames.split(' ') ?? [];
Expand Down Expand Up @@ -42,7 +42,7 @@ export const getCreatorBirthdayInfo = (

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

Expand Down
12 changes: 12 additions & 0 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ const LANG_PREFIX_REGEX = /^[a-z]{2,3}$/;
const ISO8601_DURATION_REGEX =
/(-)?P(?:([.,\d]+)Y)?(?:([.,\d]+)M)?(?:([.,\d]+)W)?(?:([.,\d]+)D)?T(?:([.,\d]+)H)?(?:([.,\d]+)M)?(?:([.,\d]+)S)?/;

export const getLastWord = (text: string): string => {
if (!text) return '';
const idx = text.lastIndexOf(' ');
return idx === -1 ? text : text.substring(idx + 1);
};

export const getFirstLine = (text: string): string => {
if (!text) return '';
const idx = text.indexOf('\n');
return idx === -1 ? text : text.substring(0, idx);
};

export const parseIdFromUrl = (url: string): number => {
if (!url) return null;

Expand Down
16 changes: 11 additions & 5 deletions src/helpers/movie.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { CSFDOptions } from '../types';
import {
addProtocol,
getColor,
getFirstLine,
parseDate,
parseFilmType,
parseISO8601Duration,
Expand Down Expand Up @@ -147,7 +148,10 @@ export const getMovieOrigins = (el: HTMLElement): string[] => {
const originNode = el.querySelector('.origin');
if (!originNode) return [];
const text = originNode.childNodes[0]?.text || '';
return text.split('/').map(x => x.trim()).filter(x => x);
return text
.split('/')
.map((x) => x.trim())
.filter((x) => x);
};

export const getMovieColorRating = (bodyClasses: string[]): CSFDColorRating => {
Expand Down Expand Up @@ -219,7 +223,7 @@ export const getMovieTitlesOther = (el: HTMLElement): CSFDTitlesOther[] => {

const titlesOther = namesNode.map((el) => {
const country = el.querySelector('img.flag').attributes.alt;
const title = el.textContent.trim().split('\n')[0];
const title = getFirstLine(el.textContent.trim());

if (country && title) {
return {
Expand Down Expand Up @@ -261,10 +265,12 @@ export const getMovieRandomPhoto = (el: HTMLElement | null): string => {
}
};

const CLEAN_TEXT_REGEX = /(\r\n|\n|\r|\t)/gm;

export const getMovieTrivia = (el: HTMLElement | null): string[] => {
const triviaNodes = el.querySelectorAll('.article-trivia ul li');
if (triviaNodes?.length) {
return triviaNodes.map((node) => node.textContent.trim().replace(/(\r\n|\n|\r|\t)/gm, ''));
return triviaNodes.map((node) => node.textContent.trim().replace(CLEAN_TEXT_REGEX, ''));
} else {
return null;
}
Expand All @@ -273,7 +279,7 @@ export const getMovieTrivia = (el: HTMLElement | null): string[] => {
export const getMovieDescriptions = (el: HTMLElement): string[] => {
return el
.querySelectorAll('.body--plots .plot-full p, .body--plots .plots .plots-item p')
.map((movie) => movie.textContent?.trim().replace(/(\r\n|\n|\r|\t)/gm, ''));
.map((movie) => movie.textContent?.trim().replace(CLEAN_TEXT_REGEX, ''));
};

const parseMoviePeople = (el: HTMLElement): CSFDMovieCreator[] => {
Expand Down Expand Up @@ -434,7 +440,7 @@ export const getMovieGroup = (

export const getMovieType = (el: HTMLElement): CSFDFilmTypes => {
const type = el.querySelector('.film-header-name .type');
const text = type?.innerText?.replace(/[{()}]/g, '').split('\n')[0].trim() || 'film';
const text = getFirstLine(type?.innerText?.replace(/[{()}]/g, ''))?.trim() || 'film';
return parseFilmType(text);
};

Expand Down
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
10 changes: 8 additions & 2 deletions src/helpers/search.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { HTMLElement } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes } from '../dto/global';
import { CSFDMovieCreator } from '../dto/movie';
import { CSFDColors } from '../dto/user-ratings';
import { addProtocol, parseColor, parseFilmType, parseIdFromUrl } from './global.helper';
import {
addProtocol,
getLastWord,
parseColor,
parseFilmType,
parseIdFromUrl
} from './global.helper';

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

Expand All @@ -26,7 +32,7 @@ export const getSearchUrl = (el: HTMLElement): string => {

export const getSearchColorRating = (el: HTMLElement): CSFDColorRating => {
return parseColor(
el.querySelector('.article-header i.icon').classNames.split(' ').pop() as CSFDColors
getLastWord(el.querySelector('.article-header i.icon').classNames) as CSFDColors
);
Comment on lines 33 to 36
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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the unsafe dereference in this file.
rg -n -C2 "querySelector\\('\\.article-header i\\.icon'\\)\\.classNames" src/helpers/search.helper.ts

Repository: bartholomej/node-csfd-api

Length of output: 273


🏁 Script executed:

#!/bin/bash
# Check for other unsafe querySelector patterns in the file
rg -n "querySelector\(" src/helpers/search.helper.ts | head -20

Repository: bartholomej/node-csfd-api

Length of output: 503


🏁 Script executed:

#!/bin/bash
# Check the imports and how getLastWord/parseColor handle edge cases
head -50 src/helpers/search.helper.ts

Repository: bartholomej/node-csfd-api

Length of output: 1748


Add null-safe handling for icon class extraction.

querySelector('.article-header i.icon') can return null; direct .classNames access will throw at runtime.

Suggested fix
 export const getSearchColorRating = (el: HTMLElement): CSFDColorRating => {
-  return parseColor(
-    getLastWord(el.querySelector('.article-header i.icon').classNames) as CSFDColors
-  );
+  const iconClasses = el.querySelector('.article-header i.icon')?.classNames ?? '';
+  return parseColor(getLastWord(iconClasses) as CSFDColors);
 };

Per guideline: "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 33 - 36, getSearchColorRating
currently assumes el.querySelector('.article-header i.icon') exists and directly
reads .classNames; make this null-safe by using optional chaining to get the
icon element and its class string (e.g., const iconClass =
el.querySelector('.article-header i.icon')?.className) and only call
getLastWord/parseColor when iconClass is present; if the icon is missing,
short-circuit and return a safe default CSFDColorRating (or call parseColor with
a known fallback) so parseColor, getLastWord and getSearchColorRating all handle
missing elements without throwing.

};

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/user-ratings.helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { HTMLElement } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDColors } from '../dto/user-ratings';
import { parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';
import { getLastWord, parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';

export const getUserRatingId = (el: HTMLElement): number => {
const url = el.querySelector('td.name .film-title-name').attributes.href;
return parseIdFromUrl(url);
};

export const getUserRating = (el: HTMLElement): CSFDStars => {
const ratingText = el.querySelector('td.star-rating-only .stars').classNames.split(' ').pop();
const ratingText = getLastWord(el.querySelector('td.star-rating-only .stars').classNames);

const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
return rating as CSFDStars;
Comment on lines +12 to 15
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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the unsafe dereference in this file.
rg -n -C2 "querySelector\\('td\\.star-rating-only \\.stars'\\)\\.classNames" src/helpers/user-ratings.helper.ts

Repository: bartholomej/node-csfd-api

Length of output: 324


Guard .stars lookup before reading classNames.

This will throw when the rating node is missing, making the parser brittle against CSFD layout changes. The helper must use optional chaining or try/catch per the robustness requirements for this directory.

Suggested fix
 export const getUserRating = (el: HTMLElement): CSFDStars => {
-  const ratingText = getLastWord(el.querySelector('td.star-rating-only .stars').classNames);
+  const starsClass = el.querySelector('td.star-rating-only .stars')?.classNames ?? '';
+  const ratingText = getLastWord(starsClass);
 
   const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
   return rating as CSFDStars;
 };
📝 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 ratingText = getLastWord(el.querySelector('td.star-rating-only .stars').classNames);
const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
return rating as CSFDStars;
export const getUserRating = (el: HTMLElement): CSFDStars => {
const starsClass = el.querySelector('td.star-rating-only .stars')?.classNames ?? '';
const ratingText = getLastWord(starsClass);
const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
return rating as CSFDStars;
};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/helpers/user-ratings.helper.ts` around lines 12 - 15, The code reads
classNames off el.querySelector('td.star-rating-only .stars') without guarding
for a missing node, causing crashes; update the logic in user-ratings.helper
(the ratingText and rating computation around getLastWord and CSFDStars) to
first check the querySelector result (using optional chaining or a try/catch)
before accessing .classNames, defaulting ratingText to an empty string (or
similar safe value) so rating = ratingText.includes('stars-') ?
+ratingText.split('-').pop() : 0 still returns a valid CSFDStars without
throwing.

Expand Down
9 changes: 6 additions & 3 deletions src/helpers/user-reviews.helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { HTMLElement } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDColors } from '../dto/user-ratings';
import { parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';
import { getLastWord, parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';

export const getUserReviewId = (el: HTMLElement): number => {
const url = el.querySelector('.film-title-name').attributes.href;
return parseIdFromUrl(url);
};

export const getUserReviewRating = (el: HTMLElement): CSFDStars => {
const ratingText = el.querySelector('.star-rating .stars').classNames.split(' ').pop();
const starsNode = el.querySelector('.star-rating .stars');
if (!starsNode) return 0 as CSFDStars;

const ratingText = getLastWord(starsNode.classNames);

const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
return rating as CSFDStars;
Expand All @@ -32,7 +35,7 @@ export const getUserReviewYear = (el: HTMLElement): number | null => {

export const getUserReviewColorRating = (el: HTMLElement): CSFDColorRating => {
const icon = el.querySelector('.film-title-inline i.icon');
const color = parseColor(icon?.classNames.split(' ').pop() as CSFDColors);
const color = parseColor(getLastWord(icon?.classNames) as CSFDColors);
return color;
};

Expand Down
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';

10 changes: 9 additions & 1 deletion src/services/movie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ export class MovieScraper {
} catch (e) {
console.error(LIB_PREFIX + ' Error parsing JSON-LD', e);
}
return this.buildMovie(+movieId, movieHtml, movieNode as HTMLElement, asideNode as HTMLElement, pageClasses, jsonLd, options);
return this.buildMovie(
+movieId,
movieHtml,
movieNode as HTMLElement,
asideNode as HTMLElement,
pageClasses,
jsonLd,
options
);
}

private buildMovie(
Expand Down
5 changes: 4 additions & 1 deletion src/services/user-ratings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export class UserRatingsScraper {
const films: CSFDUserRatings[] = [];
if (config) {
if (config.includesOnly?.length && config.excludes?.length) {
console.warn(`${LIB_PREFIX} Both 'includesOnly' and 'excludes' were provided. 'includesOnly' takes precedence:`, config.includesOnly);
console.warn(
`${LIB_PREFIX} Both 'includesOnly' and 'excludes' were provided. 'includesOnly' takes precedence:`,
config.includesOnly
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/services/user-reviews.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export class UserReviewsScraper {
const films: CSFDUserReviews[] = [];
if (config) {
if (config.includesOnly?.length && config.excludes?.length) {
console.warn(`${LIB_PREFIX} Both 'includesOnly' and 'excludes' were provided. 'includesOnly' takes precedence:`, config.includesOnly);
console.warn(
`${LIB_PREFIX} Both 'includesOnly' and 'excludes' were provided. 'includesOnly' takes precedence:`,
config.includesOnly
);
}
}

Expand Down
Loading
Loading