Skip to content

Commit

Permalink
feat(flixhq): Add fetch by genre (#476)
Browse files Browse the repository at this point in the history
* add route at flixHQ to get movies/tv by genre.  Closes #475

* feat: update docs by adding fetchByGenre and fetchByCountry

* revolve build error no-non-null-asserted-optional-chain

---------

Co-authored-by: Adailton Nascimento <[email protected]>
  • Loading branch information
dhelbegor and Adailton Nascimento authored Feb 27, 2024
1 parent 97bca7d commit 443baa6
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 7 deletions.
90 changes: 90 additions & 0 deletions docs/providers/flixhq.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,93 @@ output:
{...},
]
```

### fetchByCountry

<h4>Parameters</h4>

| Parameter | Type | Description |
| --------------- | -------- | ----------------------------------------------------------------------- |
| country | `string` | param to filter by country. (*In this case, We're filtering by `KR`*) |
| page (optional) | `number` | page number (default: 1) |

```ts
flixhq.fetchByCountry('KR').then(data => {
console.log(data);
})
```

returns a promise which resolves into an array of movies/tv series. (*[`Promise<ISearch<IMovieResult[]>>`](https://github.com/consumet/extensions/blob/master/src/models/types.ts#L233-L241)*)\
output:
```js
{
currentPage: 1,
hasNextPage: true,
results: [
{
id: 'tv/watch-wedding-impossible-106609',
title: 'Wedding Impossible',
url: 'https://flixhq.to/tv/watch-wedding-impossible-106609',
image: 'https://img.flixhq.to/xxrz/250x400/379/d1/8c/d18c569318ce319a57ba681c69b01d73/d18c569318ce319a57ba681c69b01d73.jpg',
season: 'SS 1',
latestEpisode: 'EPS 1',
type: 'TV Series'
},
{
id: 'tv/watch-a-killer-paradox-106036',
title: 'A Killer Paradox',
url: 'https://flixhq.to/tv/watch-a-killer-paradox-106036',
image: 'https://img.flixhq.to/xxrz/250x400/379/89/a0/89a0eede251cff2c9acf24fe64e2fe01/89a0eede251cff2c9acf24fe64e2fe01.jpg',
season: 'SS 1',
latestEpisode: 'EPS 8',
type: 'TV Series'
},
{...}
]
}
```

### fetchByGenre

<h4>Parameters</h4>

| Parameter | Type | Description |
| --------------- | -------- | ---------------------------------------------------------------------- |
| genre | `string` | param to filter by genre. (*In this case, We're filtering by `drama`*) |
| page (optional) | `number` | page number (default: 1) |

```ts
flixhq.fetchByGenre('drama').then(data => {
console.log(data);
})
```

returns a promise which resolves into an array of movies/tv series. (*[`Promise<ISearch<IMovieResult[]>>`](https://github.com/consumet/extensions/blob/master/src/models/types.ts#L233-L241)*)\
output:
```js
{
currentPage: 1,
hasNextPage: true,
results: [
{
id: 'movie/watch-no-new-friends-105202',
title: 'No New Friends',
url: 'https://flixhq.to/movie/watch-no-new-friends-105202',
image: 'https://img.flixhq.to/xxrz/250x400/379/16/30/16304d1c6302e6b078f6b74d5ff58347/16304d1c6302e6b078f6b74d5ff58347.jpg',
releaseDate: '2024',
seasons: undefined,
type: 'Movie'
},
{
id: 'tv/watch-shogun-106618',
title: 'Shōgun',
url: 'https://flixhq.to/tv/watch-shogun-106618',
image: 'https://img.flixhq.to/xxrz/250x400/379/a7/fc/a7fca6a36c98856de5e71d120a16e521/a7fca6a36c98856de5e71d120a16e521.jpg',
releaseDate: undefined,
seasons: 1,
type: 'TV Series'
},
{...}
]
}
```
54 changes: 47 additions & 7 deletions src/providers/movies/flixhq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ class FlixHQ extends MovieParser {
$('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')!,
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')}`,
image: $(el).find('div.film-poster > img').attr('data-src'),
season: $(el).find('div.film-detail > div.fd-infor > span:nth-child(1)').text(),
latestEpisode: $(el).find('div.film-detail > div.fd-infor > span:nth-child(3)').text() || null,
latestEpisode: $(el).find('div.film-detail > div.fd-infor > span:nth-child(3)').text() ?? null,
type:
$(el).find('div.film-detail > div.fd-infor > span.float-right').text() === 'Movie'
? TvType.MOVIE
Expand All @@ -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) ?? '',
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.fetchByCountry('KR')
// // 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 443baa6

Please sign in to comment.