Skip to content

Commit

Permalink
add route at flixHQ to get movies/tv by genre. Closes #475
Browse files Browse the repository at this point in the history
  • Loading branch information
Adailton Nascimento committed Feb 27, 2024
1 parent 363d3ea commit 145f172
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/providers/movies/flixhq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,55 @@ class FlixHQ extends MovieParser {
throw new Error((err as Error).message);
}
};

fetchByGenre = async (genre: string, page: number = 1): Promise<ISearch<IMovieResult>> => {
const result: ISearch<IMovieResult> = {
currentPage: page,
hasNextPage: false,
results: [],
};
try {
const { data } = await this.client.get(
`${this.baseUrl}/genre/${genre}?page=${page}`
);

const $ = load(data);

const navSelector = 'div.pre-pagination:nth-child(3) > nav:nth-child(1) > ul:nth-child(1)';

result.hasNextPage =
$(navSelector).length > 0 ? !$(navSelector).children().last().hasClass('active') : false;

$('.film_list-wrap > div.flw-item').each((i, el) => {
const releaseDate = $(el).find('div.film-detail > div.fd-infor > span:nth-child(1)').text();
result.results.push({
id: $(el).find('div.film-poster > a').attr('href')?.slice(1)!,

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

View check run for this annotation

codefactor.io / CodeFactor

src/providers/movies/flixhq.ts#L445

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)
title: $(el).find('div.film-detail > h2 > a').attr('title')!,
url: `${this.baseUrl}${$(el).find('div.film-poster > a').attr('href')}`,
image: $(el).find('div.film-poster > img').attr('data-src'),
releaseDate: isNaN(parseInt(releaseDate)) ? undefined : releaseDate,
seasons: releaseDate.includes('SS') ? parseInt(releaseDate.split('SS')[1]) : undefined,
type:
$(el).find('div.film-detail > div.fd-infor > span.float-right').text() === 'Movie'
? TvType.MOVIE
: TvType.TVSERIES,
});
});

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();
// // const search = await movie.fetchByCountry('KR')
// // console.log(search);
//})();
// // const genre = await movie.fetchByGenre('drama')
// // console.log(genre)
//})();

export default FlixHQ;
5 changes: 5 additions & 0 deletions test/movies/flixhq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ test('returns a filled object of movies/tv data by country', async () => {
const data = await flixhq.fetchByCountry('KR');
expect(data.results).not.toEqual([]);
});

test('returns a filled object of movies/tv data by genre', async () => {
const data = await flixhq.fetchByGenre('drama');
expect(data.results).not.toEqual([]);
});

0 comments on commit 145f172

Please sign in to comment.