Skip to content

Commit

Permalink
feat: add pagination to fetchByCountry at flixhq
Browse files Browse the repository at this point in the history
  • Loading branch information
Adailton Nascimento committed Feb 22, 2024
1 parent ba9e25b commit f54bc7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface IAnimeResult {
[x: string]: any; // other fields
}

export interface ISearch<T> {
export interface Pagination<T> {
currentPage?: number;
hasNextPage?: boolean;
totalPages?: number;
Expand All @@ -43,6 +43,10 @@ export interface ISearch<T> {
results: T[];
}

export interface ISearch<T> extends Pagination<T> {}

export interface IByCountry<T> extends Pagination<T> {}

export interface Trailer {
id: string;
site?: string;
Expand Down
30 changes: 20 additions & 10 deletions src/providers/movies/flixhq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ISearch,
} from '../../models';
import { MixDrop, VidCloud } from '../../extractors';
import { IByCountry } from '../../models/types';

class FlixHQ extends MovieParser {
override readonly name = 'FlixHQ';
Expand Down Expand Up @@ -384,14 +385,24 @@ class FlixHQ extends MovieParser {
}
};

fetchByCountry = async (country: string): Promise<IMovieResult[]> => {
fetchByCountry = async (country: string, page: number = 1): Promise<IByCountry<IMovieResult>> => {
const result: IByCountry<IMovieResult> = {
currentPage: page,
hasNextPage: false,
results: [],
};
const navSelector = 'div.pre-pagination:nth-child(3) > nav:nth-child(1) > ul:nth-child(1)';

Check warning on line 395 in src/providers/movies/flixhq.ts

View check run for this annotation

codefactor.io / CodeFactor

src/providers/movies/flixhq.ts#L395

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong. (@typescript-eslint/no-non-null-asserted-optional-chain)
try {
const { data } = await this.client.get(`${this.baseUrl}/country/${country}`);
const { data } = await this.client.get(`${this.baseUrl}/country/${country}/?page=${page}`);
const $ = load(data);

const movies = $('div.container > section.block_area > div.block_area-content > div.film_list-wrap > div.flw-item')
.map((i, el) => {
const movie = {
result.hasNextPage =
$(navSelector).length > 0 ? !$(navSelector).children().last().hasClass('active') : false;

$('div.container > section.block_area > div.block_area-content > div.film_list-wrap > div.flw-item')
.each((i, el) => {
result.results.push({
id: $(el).find('div.film-poster > a').attr('href')?.slice(1)!,
title: $(el).find('div.film-detail > h2.film-name > a').attr('title')!,
url: `${this.baseUrl}${$(el).find('div.film-poster > a').attr('href')}`,
Expand All @@ -403,23 +414,22 @@ class FlixHQ extends MovieParser {
? TvType.MOVIE
: TvType.TVSERIES,

}
return movie
})
})
.get();
return movies;
return result;
} catch (err) {
throw new Error((err as Error).message);
}
};
}

// (async () => {
// const movie = new FlixHQ();
// const movie = new FlixHQ();
// const search = await movie.search('the flash');
// // const movieInfo = await movie.fetchEpisodeSources('1168337', 'tv/watch-vincenzo-67955');
// // const recentTv = await movie.fetchTrendingTvShows();
// console.log(search);
// console.log(search);
// })();

export default FlixHQ;

0 comments on commit f54bc7f

Please sign in to comment.