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
41 changes: 0 additions & 41 deletions demo.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const parseIdFromUrl = (url: string): number => {
if (/^\d+-/.test(p)) {
return +p.split('-')[0] || null;
}
// Also support pure number segments in URLs (e.g. /uzivatel/1000/)
if (/^\d+$/.test(p)) {
return +p || null;
}
}

// Fallback
Expand Down
7 changes: 4 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 { extractId, sleep } from '../helpers/global.helper';
import {
getUserRating,
getUserRatingColorRating,
Expand All @@ -23,8 +23,9 @@ export class UserRatingsScraper {
options?: CSFDOptions
): Promise<CSFDUserRatings[]> {
let allMovies: CSFDUserRatings[] = [];
const id = extractId(user) || user;
const pageToFetch = config?.page || 1;
const url = userRatingsUrl(user, pageToFetch > 1 ? pageToFetch : undefined, {
const url = userRatingsUrl(id, pageToFetch > 1 ? pageToFetch : undefined, {
language: options?.language
});
const response = await fetchPage(url, { ...options?.request });
Expand All @@ -40,7 +41,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(id, i, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });

const items = parse(response);
Expand Down
7 changes: 4 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 { extractId, sleep } from '../helpers/global.helper';
import {
getUserReviewColorRating,
getUserReviewDate,
Expand All @@ -25,8 +25,9 @@ export class UserReviewsScraper {
options?: CSFDOptions
): Promise<CSFDUserReviews[]> {
let allReviews: CSFDUserReviews[] = [];
const id = extractId(user) || user;
const pageToFetch = config?.page || 1;
const url = userReviewsUrl(user, pageToFetch > 1 ? pageToFetch : undefined, {
const url = userReviewsUrl(id, pageToFetch > 1 ? pageToFetch : undefined, {
language: options?.language
});
const response = await fetchPage(url, { ...options?.request });
Expand All @@ -42,7 +43,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(id, i, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });

const items = parse(response);
Expand Down
19 changes: 13 additions & 6 deletions src/vars.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CSFDCinemaPeriod } from './dto/cinema';
import { extractId } from './helpers/global.helper';
import { CSFDLanguage } from './types';

export const LIB_PREFIX = '[node-csfd-api]';
Expand Down Expand Up @@ -29,20 +30,26 @@ export const getUrlByLanguage = (language?: CSFDLanguage): string => {
};

// User URLs
export const userUrl = (user: string | number, options: Options): string =>
`${getUrlByLanguage(options?.language)}/uzivatel/${encodeURIComponent(user)}`;
export const userUrl = (user: string | number, options: Options): string => {
const id = extractId(user) || user;
return `${getUrlByLanguage(options?.language)}/uzivatel/${encodeURIComponent(id)}`;
};

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 =>
`${getUrlByLanguage(options?.language)}/film/${encodeURIComponent(movie)}/prehled/`;
export const movieUrl = (movie: number | string, options: Options): string => {
const id = extractId(movie) || movie;
return `${getUrlByLanguage(options?.language)}/film/${encodeURIComponent(id)}/prehled/`;
};
// Creator URLs
export const creatorUrl = (creator: number | string, options: Options): string =>
`${getUrlByLanguage(options?.language)}/tvurce/${encodeURIComponent(creator)}`;
export const creatorUrl = (creator: number | string, options: Options): string => {
const id = extractId(creator) || creator;
return `${getUrlByLanguage(options?.language)}/tvurce/${encodeURIComponent(id)}`;
};

// Cinema URLs
export const cinemasUrl = (district: number | string, period: CSFDCinemaPeriod, options: Options): string =>
Expand Down
16 changes: 14 additions & 2 deletions tests/vars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { creatorUrl, movieUrl, searchUrl, userRatingsUrl } from '../src/vars';
describe('Vars User Ratings', () => {
test('Assemble User rating url', () => {
const url = userRatingsUrl('912-bart');
expect(url).toBe('https://www.csfd.cz/uzivatel/912-bart/hodnoceni/');
expect(url).toBe('https://www.csfd.cz/uzivatel/912/hodnoceni/');
});
test('Assemble User rating. Page 2', () => {
const url = userRatingsUrl('912-bart', 2);
expect(url).toBe('https://www.csfd.cz/uzivatel/912-bart/hodnoceni/?page=2');
expect(url).toBe('https://www.csfd.cz/uzivatel/912/hodnoceni/?page=2');
});
test('Assemble User rating with full URL', () => {
const url = userRatingsUrl('https://www.csfd.cz/uzivatel/912-bart');
expect(url).toBe('https://www.csfd.cz/uzivatel/912/hodnoceni/');
});
});

Expand All @@ -17,6 +21,10 @@ describe('Vars Movies', () => {
const url = movieUrl(535121, {});
expect(url).toBe('https://www.csfd.cz/film/535121/prehled/');
});
test('Assemble movieUrl with full URL', () => {
const url = movieUrl('https://www.csfd.cz/film/535121-zlatokopky/', {});
expect(url).toBe('https://www.csfd.cz/film/535121/prehled/');
});
});

describe('Vars Search', () => {
Expand All @@ -31,4 +39,8 @@ describe('Vars Creator', () => {
const url = creatorUrl('1', {});
expect(url).toBe('https://www.csfd.cz/tvurce/1');
});
test('Assemble creatorUrl with full URL', () => {
const url = creatorUrl('https://www.csfd.cz/tvurce/1-mel-gibson', {});
expect(url).toBe('https://www.csfd.cz/tvurce/1');
});
});