diff --git a/src/controllers/controller.ts b/src/controllers/controller.ts index 6b46446..59833f3 100644 --- a/src/controllers/controller.ts +++ b/src/controllers/controller.ts @@ -1,7 +1,7 @@ import cheerio from 'cheerio'; import axios from 'axios'; import { urls } from '../config'; -import { LastestAnimeI, AnimeI, SuggestionI, GenderI } from "../models/interfaces"; +import { LastestAnimeI, AnimeI, SuggestionI, GenderI, AnimeSearchI } from "../models/interfaces"; async function getLastest(req: any, res: any) { @@ -202,8 +202,48 @@ async function getAnime(req:any, res:any) { }); }; }; + +async function searchAnime(req:any, res:any) { + try { + let { id } = req.params; + const response = await axios.get(`${urls.search}${id}`); + const $ = cheerio.load(response.data); + let animes = []; + $('.animes .row article').each((i, e) => { + let el = $(e); + let title = el.find('h3.Title').text() + let cover = el.find('div.Image .cover .img-fluid').attr('src') + let id1 = el.find('a.link-anime').attr('href'); + let id = id1.split('/')[4]; + let category = el.find('.category').text(); + category = category.substring(1, category.length) + let year = parseInt(el.find('.fecha').text()); + + let anime: AnimeSearchI = { + id, + title, + cover, + category, + year + } + animes.push(anime); + }) + + res.json( + animes, + ) + + } catch (err) { + res.json({ + message: err.message, + success: false + }) + } +} + export { getLastest, getEmision, getAnime, + searchAnime, } \ No newline at end of file diff --git a/src/models/interfaces.ts b/src/models/interfaces.ts index 6beccbf..9ac27e8 100644 --- a/src/models/interfaces.ts +++ b/src/models/interfaces.ts @@ -46,4 +46,12 @@ export interface SuggestionI{ export interface EpisodeI{ id?:string; number?:string; +} + +export interface AnimeSearchI { + id: string; + title: string; + cover: string + category: string; + year: number; } \ No newline at end of file diff --git a/src/routes/api.routes.ts b/src/routes/api.routes.ts index 7e8e2ab..74ece8a 100644 --- a/src/routes/api.routes.ts +++ b/src/routes/api.routes.ts @@ -3,7 +3,7 @@ import { Router } from 'express'; const routes = Router(); -import { getEmision, getLastest, getAnime } from '../controllers/controller' +import { getEmision, getLastest, getAnime, searchAnime } from '../controllers/controller' @@ -12,9 +12,12 @@ routes.get('/lastest', (req, res) => { }) routes.get('/emision', (req, res) => { getEmision(req, res); -}) +}); routes.get('/anime/:id', (req, res) => { getAnime(req, res); +}); +routes.get('/search/:id', (req, res) => { + searchAnime(req, res); }) export default routes \ No newline at end of file