From 145f172a4734132ed52d49bc15b0c1557ed8d81c Mon Sep 17 00:00:00 2001 From: Adailton Nascimento Date: Mon, 26 Feb 2024 21:51:28 -0300 Subject: [PATCH] add route at flixHQ to get movies/tv by genre. Closes #475 --- src/providers/movies/flixhq.ts | 48 +++++++++++++++++++++++++++++++--- test/movies/flixhq.test.ts | 5 ++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/providers/movies/flixhq.ts b/src/providers/movies/flixhq.ts index 3846b4fed..5d0a8346b 100644 --- a/src/providers/movies/flixhq.ts +++ b/src/providers/movies/flixhq.ts @@ -420,15 +420,55 @@ class FlixHQ extends MovieParser { throw new Error((err as Error).message); } }; + + fetchByGenre = async (genre: string, page: number = 1): Promise> => { + const result: ISearch = { + 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)!, + 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; diff --git a/test/movies/flixhq.test.ts b/test/movies/flixhq.test.ts index 31ee0cc3c..ecdb80e76 100644 --- a/test/movies/flixhq.test.ts +++ b/test/movies/flixhq.test.ts @@ -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([]); +});