diff --git a/src/api/api.js b/src/api/api.js index d3995d3..fef6b1f 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -242,7 +242,7 @@ const getMoreInfo = async (title) =>{ try { let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); - let result = data.filter(anime => anime.title === title)[0]; + let result = data.filter(anime => anime.title === title || anime.mal_title === title)[0]; return { title: result.title || null, @@ -265,9 +265,7 @@ const getMoreInfo = async (title) =>{ }; const getEpisodes = async (title) =>{ - try { - let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); const result = data.filter(x => x.title === title || x.mal_title === title)[0]; @@ -371,13 +369,13 @@ const getRadioStations = async () => require('../assets/radiostations.json'); const getOpAndEd = async (title) => await structureThemes(await parserThemes.serie(title), true); const getThemesYear = async (year) => { - let data = []; + if (year === undefined) { return await parserThemes.allYears(); } else { - data = await parserThemes.year(year) - return await structureThemes(data, false) + data = await parserThemes.year(year); + return await structureThemes(data, false); } }; @@ -534,6 +532,13 @@ const getProfilePlatform = async (id) => { }; +function getRandomAnime() { + let directory = JSON.parse(JSON.stringify(require('../assets/directory.json'))); + const randomNumber = Math.floor(Math.random() * directory.length); + + return directory[randomNumber]; +} + module.exports = { schedule, top, @@ -563,5 +568,6 @@ module.exports = { getPlatforms, getSectionYoutubeVideos, getProfilePlatform, - getRelatedAnimes + getRelatedAnimes, + getRandomAnime }; diff --git a/src/api/routes/index.js b/src/api/routes/index.js index 10ba89b..ebecfb5 100644 --- a/src/api/routes/index.js +++ b/src/api/routes/index.js @@ -320,7 +320,6 @@ router.get('/getAnimeServers/:id([^/]+/[^/]+)' , (req, res) =>{ }); router.get('/search/:title' , (req, res) =>{ - let title = req.params.title; api.search(title) @@ -339,7 +338,6 @@ router.get('/search/:title' , (req, res) =>{ }); router.get('/images/:query' , (req, res) =>{ - let query = { title: req.params.query, count: '51', type: 'images', safesearch: '1', country: 'es_ES', uiv: '4' }; api.getImages(query) @@ -358,7 +356,6 @@ router.get('/images/:query' , (req, res) =>{ }); router.get('/videos/:channelId' , (req, res) =>{ - let channelId = { id: req.params.channelId, part: 'snippet,id', order: 'date', maxResults: '50', prop: 'items' }; api.getYoutubeVideos(channelId) @@ -377,7 +374,6 @@ router.get('/videos/:channelId' , (req, res) =>{ }); router.get('/sectionedVideos/:type' , (req, res) =>{ - let type = req.params.type; api.getSectionYoutubeVideos(type) @@ -396,7 +392,6 @@ router.get('/sectionedVideos/:type' , (req, res) =>{ }); router.get('/radio' , (req, res) =>{ - api.getRadioStations() .then(stations =>{ if (stations.length > 0) { @@ -431,9 +426,7 @@ router.get('/allThemes', (req, res) =>{ router.get('/themes/:title' , (req, res) =>{ - let title = req.params.title; - api.getOpAndEd(title) .then(themes => { @@ -451,19 +444,17 @@ router.get('/themes/:title' , (req, res) =>{ }); router.get('/themesYear/:year?', (req, res) =>{ - let year = req.params.year; - let season = req.params.season - api.getThemesYear(year, season) + api.getThemesYear(year) .then(themes =>{ if (themes.length > 0) { res.status(200).json({ themes }); - } else ( - res.status(500).json({ message: 'Aruppi lost in the shell'}) - ) + } else { + res.status(500).json({ message: 'Aruppi lost in the shell'}); + } }).catch((err) =>{ console.error(err); }); @@ -512,19 +503,29 @@ router.get('/getByGenres/:genre?/:order?/:page?' , (req , res) =>{ let genres = { genre: req.params.genre, order: req.params.order, page: req.params.page }; api.getAnimeGenres(genres) - .then(animes =>{ + .then(animes => { if (animes.length > 0) { res.status(200).json({ animes }); - } else ( - res.status(500).json({ message: 'Aruppi lost in the shell'}) - ) + } else { + res.status(500).json({ message: 'Aruppi lost in the shell'}); + } }).catch((err) =>{ console.error(err); }); }); +router.get('/randomAnime', (req, res) => { + let randomAnime = api.getRandomAnime(); + + if (randomAnime) { + res.status(200).json({randomAnime}); + }else { + res.status(500).json({ message: 'Aruppi lost in the shell'}); + } +}); + router.get('/destAnimePlatforms' , (req , res) =>{ api.getDestAnimePlatforms() diff --git a/src/utils/animetheme.js b/src/utils/animetheme.js index 3a29ab1..eae96a4 100644 --- a/src/utils/animetheme.js +++ b/src/utils/animetheme.js @@ -82,11 +82,11 @@ class ThemeParser { let animes = []; this.$ = await redditocall(date); - this.$('h3').each((i, el) => { - let parsed = this.parseAnime(el); + this.$('h3').each((index, element) => { + let parsed = this.parseAnime(this.$(element)); parsed.year = date; animes.push(parsed); - }) + }); return animes; } @@ -106,25 +106,25 @@ class ThemeParser { }); } + /* -ParseYears + Get the data from the year + get the name and the id to do the respective + scrapping. + */ parseYears() { return new Promise(async resolve => { + let years = []; - let promises = []; - let data = this.$('h3 a'); - - for (let i = 0; i < data.length; i++) { - - promises.push({ - id: data[i].children[0].parent.attribs.href.split('/')[4], - name: data[i].children[0].data - }) - - if (i === data.length - 1) { - resolve(promises); - } - - } + this.$('h3 a').each((index, element) => { + years.push( + { + id: this.$(element).attr('href').split('/')[4], + name: this.$(element).text() + } + ); + }); + resolve(years); }); } @@ -226,10 +226,16 @@ class ThemeParser { }) } + /* - ParseAnime + Parse the h3 tag and get the table + for the next function to parse the table + and get the information about the ending and + openings. + */ parseAnime(element) { - let el = this.$(element).children('a'); - let title = el.text(); - let mal_id = el.attr('href').split('/')[4]; + let el = this.$(element).find('a'); + let title = this.$(el).text(); + let mal_id = this.$(el).attr('href').split('/')[4]; let next = this.$(element).next(); let theme = { @@ -237,18 +243,12 @@ class ThemeParser { title }; - - if (next.prop("tagName") === "TABLE") { - - theme.themes = this.parseTable(next); - - }else if (next.prop("tagName") === "P") { - - theme.themes = this.parseTable(next.next()); - + if (this.$(next).prop("tagName") === "TABLE") { + theme.themes = this.parseTable(this.$(next)); + }else if (this.$(next).prop("tagName") === "P") { + theme.themes = this.parseTable(this.$(next).next()); } - return theme; } @@ -257,25 +257,22 @@ class ThemeParser { and returns a object with all the information. */ - parseTable(element) { - if (element.prop('tagName') !== "TABLE") { - return this.parseTable(element.next()); + if (this.$(element).prop('tagName') !== "TABLE") { + return this.parseTable(this.$(element).next()); } let themes = []; - element.find('tbody').find('tr').each((i, elem) => { - - let name = replaceAll(elem.children[1].children[0].data, """, "\""); - let link = elem.children[3].children[0].attribs.href; - let linkDesc = elem.children[3].children[0].children[0].data; - let episodes = elem.children[5].children.length > 0 ? elem.children[5].children[0].data : ""; - let notes = elem.children[7].children.length > 0 ? elem.children[7].children[0].data : ""; - + this.$(element).find('tbody').find('tr').each((i, elem) => { + let name = replaceAll(this.$(elem).find('td').eq(0).text(), '"', '"'); + let link = this.$(elem).find('td').eq(1).find('a').attr('href'); + let linkDesc = this.$(elem).find('td').eq(1).find('a').text(); + let episodes = this.$(elem).find('td').eq(2).text().length > 0 ? this.$(elem).find('td').eq(2).text() : ""; + let notes = this.$(elem).find('td').eq(3).text().length > 0 ? this.$(elem).find('td').eq(3).text() : ""; themes.push({ name, @@ -286,8 +283,8 @@ class ThemeParser { notes }); }); - return themes; + return themes; } } diff --git a/src/utils/index.js b/src/utils/index.js index 6263124..7f3f2d5 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -138,14 +138,16 @@ const jkanimeInfo = async (id) => { }; -function getPoster(id) { - +function getPosterAndType(id) { let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); - //@CHECK + for (let anime of data) { if (anime.id === id) { - return [anime.poster, anime.type]; + return [ + anime.poster, + anime.type + ]; } } @@ -153,7 +155,6 @@ function getPoster(id) { }; async function getRelatedAnimes(id) { - const $ = await homgot(`${BASE_ANIMEFLV}/anime/${id}`, { scrapy: true }); let listRelated = {}; let relatedAnimes = []; @@ -164,9 +165,8 @@ async function getRelatedAnimes(id) { }); for (related in listRelated) { - let posterUrl = getPoster(listRelated[related].split('/')[2]); + let posterUrl = getPosterAndType(listRelated[related].split('/')[2]); - //@CHECK relatedAnimes.push( { id: listRelated[related].split('/')[2], @@ -193,7 +193,6 @@ const animeflvGenres = async (id) => { } const animeflvInfo = async (id) => { - let $ = await homgot(`${BASE_ANIMEFLV}/anime/${id}`, { scrapy: true }); let scripts = $('script').toArray(); @@ -459,6 +458,7 @@ const obtainPreviewNews = (encoded) => { then returns a array with the themes selected. */ const structureThemes = async (body, indv) => { + let themes = []; if (indv === true) { @@ -471,8 +471,6 @@ const structureThemes = async (body, indv) => { } else { for (let i = 0; i <= body.length - 1; i++) { - const themes = []; - themes.push({ title: body[i].title, year: body[i].year,