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
28 changes: 28 additions & 0 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ export const extractId = (idOrUrl: number | string): number | null => {
return null;
};

/**
* Extracts a user ID or slug from a number, string, slug, or full URL.
* Designed for Developer Experience (DX) to allow flexible inputs.
*/
export const extractUser = (userOrUrl: number | string): string | number | null => {
if (typeof userOrUrl === 'number') {
return isNaN(userOrUrl) ? null : userOrUrl;
}

if (typeof userOrUrl === 'string') {
const trimmed = userOrUrl.trim();
if (!trimmed) return null;

if (trimmed.includes('/') || trimmed.includes('csfd.')) {
const parts = trimmed.split('/');
const uzivatelIndex = parts.indexOf('uzivatel');
if (uzivatelIndex !== -1 && parts[uzivatelIndex + 1]) {
return parts[uzivatelIndex + 1];
Comment on lines +66 to +70

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Parse only the URL path.

Line 67 splits the complete URL. A query string or fragment can contain /uzivatel/ and be accepted as a user URL. For example, a film URL with ?next=/uzivatel/912-bart returns 912-bart.

A user URL with query parameters also returns an identifier that contains ?, which creates an invalid request URL. Strip the query and fragment before splitting. Add a regression test for both cases.

Proposed fix
-    if (trimmed.includes('/') || trimmed.includes('csfd.')) {
-      const parts = trimmed.split('/');
+    const path = trimmed.replace(/[?#].*$/, '');
+    if (path.includes('/') || path.includes('csfd.')) {
+      const parts = path.split('/');
📝 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
if (trimmed.includes('/') || trimmed.includes('csfd.')) {
const parts = trimmed.split('/');
const uzivatelIndex = parts.indexOf('uzivatel');
if (uzivatelIndex !== -1 && parts[uzivatelIndex + 1]) {
return parts[uzivatelIndex + 1];
const path = trimmed.replace(/[?#].*$/, '');
if (path.includes('/') || path.includes('csfd.')) {
const parts = path.split('/');
const uzivatelIndex = parts.indexOf('uzivatel');
if (uzivatelIndex !== -1 && parts[uzivatelIndex + 1]) {
return parts[uzivatelIndex + 1];
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/helpers/global.helper.ts` around lines 66 - 70, Update the URL parsing
logic around the helper’s trimmed URL handling to remove the query string and
fragment before splitting or searching for the uzivatel path segment. Ensure
identifiers never include query or fragment data and URLs whose query or
fragment merely contains /uzivatel/ are not accepted; add regression tests
covering both cases.

}
return null;
}

return trimmed;
}

return null;
};

export const parseLastIdFromUrl = (url: string): number => {
if (url) {
const idSlug = url?.split('/')[3];
Expand Down
11 changes: 8 additions & 3 deletions src/services/user-ratings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement, parse } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDUserRatingConfig, CSFDUserRatings } from '../dto/user-ratings';
import { fetchPage } from '../fetchers';
import { sleep } from '../helpers/global.helper';
import { sleep, extractUser } from '../helpers/global.helper';
import {
getUserRating,
getUserRatingColorRating,
Expand All @@ -22,9 +22,14 @@ export class UserRatingsScraper {
config?: CSFDUserRatingConfig,
options?: CSFDOptions
): Promise<CSFDUserRatings[]> {
const extractedUser = extractUser(user);
if (!extractedUser) {
throw new Error('node-csfd-api: user must be a valid user ID or slug');
}

let allMovies: CSFDUserRatings[] = [];
const pageToFetch = config?.page || 1;
const url = userRatingsUrl(user, pageToFetch > 1 ? pageToFetch : undefined, {
const url = userRatingsUrl(extractedUser, pageToFetch > 1 ? pageToFetch : undefined, {
language: options?.language
});
const response = await fetchPage(url, { ...options?.request });
Expand All @@ -40,7 +45,7 @@ export class UserRatingsScraper {
if (config?.allPages) {
for (let i = 2; i <= pages; i++) {
config.onProgress?.(i, pages);
const url = userRatingsUrl(user, i, { language: options?.language });
const url = userRatingsUrl(extractedUser, i, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });

const items = parse(response);
Expand Down
11 changes: 8 additions & 3 deletions src/services/user-reviews.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement, parse } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDUserReviews, CSFDUserReviewsConfig } from '../dto/user-reviews';
import { fetchPage } from '../fetchers';
import { sleep } from '../helpers/global.helper';
import { sleep, extractUser } from '../helpers/global.helper';
import {
getUserReviewColorRating,
getUserReviewDate,
Expand All @@ -24,9 +24,14 @@ export class UserReviewsScraper {
config?: CSFDUserReviewsConfig,
options?: CSFDOptions
): Promise<CSFDUserReviews[]> {
const extractedUser = extractUser(user);
if (!extractedUser) {
throw new Error('node-csfd-api: user must be a valid user ID or slug');
}

let allReviews: CSFDUserReviews[] = [];
const pageToFetch = config?.page || 1;
const url = userReviewsUrl(user, pageToFetch > 1 ? pageToFetch : undefined, {
const url = userReviewsUrl(extractedUser, pageToFetch > 1 ? pageToFetch : undefined, {
language: options?.language
});
const response = await fetchPage(url, { ...options?.request });
Expand All @@ -42,7 +47,7 @@ export class UserReviewsScraper {
if (config?.allPages) {
for (let i = 2; i <= pages; i++) {
config.onProgress?.(i, pages);
const url = userReviewsUrl(user, i, { language: options?.language });
const url = userReviewsUrl(extractedUser, i, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });

const items = parse(response);
Expand Down
35 changes: 34 additions & 1 deletion tests/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, test } from 'vitest';
import { addProtocol, extractId, parseColor, parseIdFromUrl } from '../src/helpers/global.helper';
import {
addProtocol,
extractId,
extractUser,
parseColor,
parseIdFromUrl
} from '../src/helpers/global.helper';

describe('Add protocol', () => {
test('Handle without protocol', () => {
Expand Down Expand Up @@ -49,6 +55,33 @@ describe('Parse Id', () => {
});
});

describe('extractUser', () => {
test('Handle numeric ID', () => {
expect(extractUser(912)).toBe(912);
});
test('Handle numeric string', () => {
expect(extractUser('912')).toBe('912');
});
test('Handle slug', () => {
expect(extractUser('912-bart')).toBe('912-bart');
});
test('Handle text slug', () => {
expect(extractUser('admin')).toBe('admin');
});
test('Handle full URL', () => {
expect(extractUser('https://www.csfd.cz/uzivatel/912-bart/')).toBe('912-bart');
});
test('Handle full URL with language prefix', () => {
expect(extractUser('https://www.csfd.cz/en/uzivatel/912-bart/hodnoceni/')).toBe('912-bart');
});
test('Handle non-user URL', () => {
expect(extractUser('https://www.csfd.cz/film/123/')).toBe(null);
});
test('Handle invalid strings', () => {
expect(extractUser(' ')).toBe(null);
});
});

describe('extractId', () => {
test('Handle numeric ID', () => {
expect(extractId(228329)).toBe(228329);
Expand Down