Skip to content

Commit

Permalink
feat(zoro): Add search suggestions (#536)
Browse files Browse the repository at this point in the history
* feat(zoro): added search suggestions

* [CodeFactor] Apply fixes to commit 7861ea6

* fix(zoro): added docs for fetchSearchSuggestions
  • Loading branch information
jasanpreetn9 authored May 21, 2024
1 parent acb1747 commit 2bcd928
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
1 change: 1 addition & 0 deletions dist/providers/anime/zoro.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare class Zoro extends AnimeParser {
*/
fetchSchedule(date?: string): Promise<ISearch<IAnimeResult>>;
fetchSpotlight(): Promise<ISearch<IAnimeResult>>;
fetchSearchSuggestions(query: string): Promise<ISearch<IAnimeResult>>;
/**
* @param id Anime id
*/
Expand Down
38 changes: 38 additions & 0 deletions dist/providers/anime/zoro.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/providers/anime/zoro.js.map

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions docs/providers/zoro.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const zoro = new ANIME.Zoro();
- [fetchSchedule](#fetchSchedule)
- [fetchStudio](#fetchStudio)
- [fetchSpotlight](#fetchSpotlight)
- [fetchSearchSuggestions] (#fetchSearchSuggestions)

### search
> Note: This method is a subclass of the [`BaseParser`](https://github.com/consumet/extensions/blob/master/src/models/base-parser.ts) class. meaning it is available across most categories.
Expand Down Expand Up @@ -723,6 +724,77 @@ output:
]
}
```
### fetchSearchSuggestions

```ts
zoro.fetchSearchSuggestions("One Piece").then(data => {
console.log(data);
})
```

returns a promise which resolves into an array of anime. (*[`Promise<ISearch<IAnimeResult[]>>`](https://github.com/consumet/extensions/blob/master/src/models/types.ts#L13-L26)*)\
output:
```js
{
results: [
{
image: 'https://cdn.noitatnemucod.net/thumbnail/300x400/100/ff736656ba002e0dd51363c3d889d9ff.jpg',
id: 'one-piece-movie-1-3096',
title: 'One Piece Movie 1',
japaneseTitle: 'One Piece Movie 1',
aliasTitle: 'One Piece Movie 1',
releaseDate: 'Mar 4, 2000',
type: 'Movie',
duration: '50m',
url: 'https://hianime.to/one-piece-movie-1-3096'
},
{
image: 'https://cdn.noitatnemucod.net/thumbnail/300x400/100/bcd84731a3eda4f4a306250769675065.jpg',
id: 'one-piece-100',
title: 'One Piece',
japaneseTitle: 'One Piece',
aliasTitle: 'One Piece',
releaseDate: 'Oct 20, 1999',
type: 'TV',
duration: '24m',
url: 'https://hianime.to/one-piece-100'
},
{
image: 'https://cdn.noitatnemucod.net/thumbnail/300x400/100/a1e98b07e290cd9653b41a895342a377.jpg',
id: 'one-piece-film-red-18236',
title: 'One Piece Film: Red',
japaneseTitle: 'One Piece Film: Red',
aliasTitle: 'One Piece Film: Red',
releaseDate: 'Aug 6, 2022',
type: 'Movie',
duration: '1h 55m',
url: 'https://hianime.to/one-piece-film-red-18236'
},
{
image: 'https://cdn.noitatnemucod.net/thumbnail/300x400/100/7156c377053c230cc42b66bbf7260325.jpg',
id: 'one-piece-the-movie-13-film-gold-550',
title: 'One Piece: The Movie 13 - Film: Gold',
japaneseTitle: 'One Piece Film: Gold',
aliasTitle: 'One Piece Film: Gold',
releaseDate: 'Jul 23, 2016',
type: 'Movie',
duration: '1h 30m',
url: 'https://hianime.to/one-piece-the-movie-13-film-gold-550'
},
{
image: 'https://cdn.noitatnemucod.net/thumbnail/300x400/100/14f2be76eee4a497ad81a5039425ff06.jpg',
id: 'one-room-third-season-6959',
title: 'One Room Third Season',
japaneseTitle: 'One Room Third Season',
aliasTitle: 'One Room Third Season',
releaseDate: 'Oct 6, 2020',
type: 'TV',
duration: '4m',
url: 'https://hianime.to/one-room-third-season-6959'
}
]
}
```


Make sure to check the `headers` property of the returned object. It contains the referer header, which might be needed to bypass the 403 error and allow you to stream the video without any issues.
Expand Down
39 changes: 39 additions & 0 deletions src/providers/anime/zoro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,45 @@ class Zoro extends AnimeParser {
}
}

async fetchSearchSuggestions(query: string): Promise<ISearch<IAnimeResult>> {
try {
const encodedQuery = encodeURIComponent(query);
const { data } = await this.client.get(`${this.baseUrl}/ajax/search/suggest?keyword=${encodedQuery}`);
const $ = load(data.html);
const res: ISearch<IAnimeResult> = {
results: [],
};

$('.nav-item').each((i, el) => {
const card = $(el);
if (!card.hasClass("nav-bottom")) {
const image = card.find('.film-poster img').attr('data-src');
const title = card.find('.film-name');
const id = card.attr('href')?.split('/')[1].split('?')[0];

const duration = card.find(".film-infor span").last().text().trim();
const releaseDate = card.find(".film-infor span:nth-child(1)").text().trim();
const type = card.find(".film-infor").find("span, i").remove().end().text().trim();
res.results.push({
image: image,
id: id!,
title: title.text(),
japaneseTitle: title.attr('data-jname'),
aliasTitle: card.find(".alias-name").text(),
releaseDate: releaseDate,
type: type as MediaFormat,
duration: duration,
url: `${this.baseUrl}/${id}`,
});
}
});

return res;
} catch (error) {
throw new Error('Something went wrong. Please try again later.');
}
}

/**
* @param id Anime id
*/
Expand Down
6 changes: 6 additions & 0 deletions test/anime/zoro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ test('returns a filled array of anime list', async () => {
expect(data.results).not.toEqual([]);
});

test('returns a filled array of anime list', async () => {
const data = await zoro.fetchSearchSuggestions("one piece");
console.log(data);
expect(data.results).not.toEqual([]);
})

test('returns a filled object of anime data', async () => {
const res = await zoro.search('Overlord IV');
const data = await zoro.fetchAnimeInfo("one-piece-100"); // Overlord IV id
Expand Down

0 comments on commit 2bcd928

Please # to comment.