diff --git a/demo.ts b/demo.ts deleted file mode 100644 index a901738..0000000 --- a/demo.ts +++ /dev/null @@ -1,41 +0,0 @@ -// import { writeFile } from 'fs'; -import { csfd } from './src'; - -// csfd.setOptions({ optionsRequest: { credentials: 'include' } }); - -// Parse movie -csfd.movie(621073).then((movie) => console.log(movie)); - -// csfd.search('matrix').then((search) => console.log(search)); -// csfd.cinema(1, 'today').then((cinema) => console.log(cinema)); - -// Parse creator -// csfd.creator(2120).then((creator) => console.log(creator)); - -/** - * USER RATINGS - */ - -// // Save all pages in json file -// const userId = 912; - -// csfd -// .userRatings(userId, { -// excludes: ['episode', 'series', 'season'], -// allPages: true, -// allPagesDelay: 2000, -// onProgress: (page, total) => { -// console.log('Fetching rating page', page, 'out of', total, '...'); -// } -// }) -// .then((ratings) => { -// console.log('Saved in file:', `./${userId}.json`); -// writeFile(`${userId}.json`, JSON.stringify(ratings), (err) => { -// if (err) return console.log(err); -// }); -// }); - -// Only TV series -// csfd -// .userRatings('912-bart', { includesOnly: ['series'] }) -// .then((ratings) => console.log(ratings)); diff --git a/src/helpers/global.helper.ts b/src/helpers/global.helper.ts index 6f2acb7..94f5c53 100644 --- a/src/helpers/global.helper.ts +++ b/src/helpers/global.helper.ts @@ -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 diff --git a/src/services/user-ratings.service.ts b/src/services/user-ratings.service.ts index 09244fe..98e8a05 100644 --- a/src/services/user-ratings.service.ts +++ b/src/services/user-ratings.service.ts @@ -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, @@ -23,8 +23,9 @@ export class UserRatingsScraper { options?: CSFDOptions ): Promise { 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 }); @@ -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); diff --git a/src/services/user-reviews.service.ts b/src/services/user-reviews.service.ts index 88f5f49..8d504c8 100644 --- a/src/services/user-reviews.service.ts +++ b/src/services/user-reviews.service.ts @@ -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, @@ -25,8 +25,9 @@ export class UserReviewsScraper { options?: CSFDOptions ): Promise { 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 }); @@ -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); diff --git a/src/vars.ts b/src/vars.ts index 41d5405..191896f 100644 --- a/src/vars.ts +++ b/src/vars.ts @@ -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]'; @@ -29,8 +30,10 @@ 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 : ''}`; @@ -38,11 +41,15 @@ export const userReviewsUrl = (user: string | number, page?: number, options: Op `${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 => diff --git a/tests/vars.test.ts b/tests/vars.test.ts index e6c2e07..22a467f 100644 --- a/tests/vars.test.ts +++ b/tests/vars.test.ts @@ -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/'); }); }); @@ -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', () => { @@ -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'); + }); });