From 04cdccc8f6fbd75fe1ebd41675b165fbc46f65d6 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Fri, 22 Jan 2021 18:49:14 +0000 Subject: [PATCH 01/19] Commiting Branch for improving the existing API --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index e57e57b..2187fe4 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,8 @@ function shutdown() { process.exit(); } +// @TESTING + process.on('SIGINT', shutdown); process.on('SIGQUIT', shutdown); process.on('SIGTERM', shutdown); From 3c505cf54e49174f8799ea0b0c817fb53b14d622 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Fri, 22 Jan 2021 19:13:23 +0000 Subject: [PATCH 02/19] Branch for improving and refactoring --- src/api/api.js | 16 ++--- src/api/apiCall.js | 19 +++--- src/api/routes/index.js | 29 +++++---- src/utils/animetheme.js | 128 ++++++++++++++++++++++++---------------- 4 files changed, 113 insertions(+), 79 deletions(-) diff --git a/src/api/api.js b/src/api/api.js index b62ce86..922a023 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -20,7 +20,7 @@ const { } = require('../utils/index'); const ThemeParser = require('../utils/animetheme'); -const parserThemes = new ThemeParser() +const parserThemes = new ThemeParser(); const { BASE_ANIMEFLV_JELU, BASE_JIKAN, BASE_IVOOX, BASE_QWANT, BASE_YOUTUBE, BASE_THEMEMOE, BASE_ANIMEFLV, BASE_ARUPPI @@ -75,17 +75,19 @@ const getAllAnimes = async () =>{ }; -const getAllDirectory = async (genres) =>{ +const getAllDirectory = async (genres) => { - let data - if (genres === "sfw") { + let data; + + if (genres === 'sfw') { data = JSON.parse(JSON.stringify(require('../assets/directory.json'))).filter(function (item) { return !item.genres.includes("Ecchi") && !item.genres.includes("ecchi"); - }) + }); } else { data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); } + return data.map(doc => ({ id: doc.id, title: doc.title, @@ -101,7 +103,7 @@ const getAllDirectory = async (genres) =>{ }; -const getAnitakume = async () =>{ +const getAnitakume = async () => { const promises = [] @@ -363,7 +365,7 @@ const getOpAndEd = async (title) => await structureThemes(await parserThemes.ser const getThemesYear = async (year) => { - let data = [] + let data = []; if (year === undefined) { return await parserThemes.allYears(); } else { diff --git a/src/api/apiCall.js b/src/api/apiCall.js index 95fc0cd..47c3935 100644 --- a/src/api/apiCall.js +++ b/src/api/apiCall.js @@ -1,27 +1,30 @@ -const got = require('got'); +const got = require('got'); // This is to make a HTTP request without doing AJAX const cheerio = require('cheerio'); const { CookieJar} = require('tough-cookie'); const cookieJar = new CookieJar(); -let response -let data +let response; +let data; const homgot = async (url, options) => { if (options !== undefined) { + if (options.scrapy) { - response = await got(url, { cookieJar }) - data = await cheerio.load(response.body) + response = await got(url, { cookieJar }); + data = await cheerio.load(response.body); } + if (options.parse) { - data = await got(url, { cookieJar }).json() + data = await got(url, { cookieJar }).json(); } + } else { data = await got.get(url, { cookieJar }); } - return data + return data; } -module.exports = {homgot} +module.exports = { homgot }; diff --git a/src/api/routes/index.js b/src/api/routes/index.js index efb4035..b5d7b16 100644 --- a/src/api/routes/index.js +++ b/src/api/routes/index.js @@ -269,12 +269,12 @@ router.get('/moreInfo/:title' , (req, res) =>{ let title = req.params.title; api.getMoreInfo(title) - .then(info =>{ + .then(info => { if (info !== undefined) { res.status(200).json(info); - } 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); }); @@ -286,13 +286,15 @@ router.get('/getEpisodes/:title' , (req, res) =>{ let title = req.params.title; api.getEpisodes(title) - .then(episodes =>{ + .then(episodes => { + if (episodes.length > 0) { res.status(200).json({episodes}); - } 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); }); @@ -431,16 +433,17 @@ router.get('/allThemes', (req, res) =>{ router.get('/themes/:title' , (req, res) =>{ let title = req.params.title; + api.getOpAndEd(title) - .then(themes =>{ - if (themes.length > 0) { + .then(themes => { + if (themes) { 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); }); diff --git a/src/utils/animetheme.js b/src/utils/animetheme.js index 8f487a0..81aa56e 100644 --- a/src/utils/animetheme.js +++ b/src/utils/animetheme.js @@ -19,7 +19,7 @@ class ThemeParser { return await this.parseLinks(); } catch(err) { - throw err; + console.log(err); } } @@ -30,18 +30,18 @@ class ThemeParser { return await this.parseYears(); } catch(err) { - throw err; + console.log(err); } } - async serie(query) { + async serie(title) { try { this.animes = []; this.$ = await redditocall('anime_index'); - return await this.parseSerie(query); + return await this.parseSerie(title); } catch(err) { - throw err; + console.log(err); } } @@ -52,7 +52,7 @@ class ThemeParser { return await this.parseArtists(); } catch(err) { - throw err; + console.log(err); } } @@ -63,7 +63,7 @@ class ThemeParser { return await this.parseArtist(); } catch(err) { - throw err; + console.log(err); } } @@ -74,13 +74,14 @@ class ThemeParser { return await this.parseRandom(query); } catch(err) { - throw err; + console.log(err); } } async year(date) { let animes = []; - this.$ = await redditocall(date) + + this.$ = await redditocall(date); this.$('h3').each((i, el) => { let parsed = this.parseAnime(el); parsed.year = date; @@ -102,13 +103,13 @@ class ThemeParser { let parsed = this.parseAnime(this.$('h3')[rand]); resolve(parsed); - }) + }); } - parseYears(){ + parseYears() { return new Promise(async resolve => { - let promises = [] + let promises = []; let data = this.$('h3 a'); for (let i = 0; i < data.length; i++) { @@ -119,15 +120,15 @@ class ThemeParser { }) if (i === data.length - 1) { - resolve(promises) + resolve(promises); } } - }) + }); } - parseArtists(){ + parseArtists() { return new Promise(async resolve => { let promises = [] @@ -169,22 +170,31 @@ class ThemeParser { }) } - parseSerie(query){ + /* - ParseSerie + Parse the HTML from the redditocall + and search for the h3 tag to be the + same of the title and resolve a object. + */ + parseSerie(title) { return new Promise(async resolve => { let data = this.$('p a'); for (let i = 0; i < data.length; i++) { - let serieElement = data[i].children[0].data - - if (serieElement.split(" (")[0] === query) { + let serieElement = data[i].children[0].data; + + if (serieElement.split(" (")[0] === title) { + let year = this.$('p a')[i].attribs.href.split('/r/AnimeThemes/wiki/')[1].split('#wiki')[0]; this.$ = await redditocall(this.$('p a')[i].attribs.href.split('/r/AnimeThemes/wiki/')[1].split('#wiki')[0]); - + for (let i = 0; i < this.$('h3').length; i++) { - if (this.$('h3')[i].children[0].children[0].data === query) { + + if (this.$('h3')[i].children[0].children[0].data === title) { + let parsed = this.parseAnime(this.$('h3')[i]); + parsed.year = year; resolve(parsed); } } @@ -192,7 +202,7 @@ class ThemeParser { } } - }) + }); } parseLinks() { @@ -215,65 +225,80 @@ class ThemeParser { }) } - parseAnime(dat) { - let el = this.$(dat).children('a'); + parseAnime(element) { + let el = this.$(element).children('a'); let title = el.text(); - let malId = el.attr('href').split('/')[4]; - let next = this.$(dat).next(); + let mal_id = el.attr('href').split('/')[4]; + let next = this.$(element).next(); let theme = { - id: malId, + id: mal_id, title - } + }; + + + if (next.prop("tagName") === "TABLE") { - if (next.prop("tagName") === "P") { - theme.themes = this.parseTable(next.next()); - } else if (next.prop("tagName") === "TABLE") { theme.themes = this.parseTable(next); - } + + }else if (next.prop("tagName") === "P") { + + theme.themes = this.parseTable(next.next()); + + } + return theme; } - parseTable(table) { - if (table.prop('tagName') !== "TABLE") { - return this.parseTable(table.next()); + /* - ParseTable + Parse the table tag from the HTML + and returns a object with all the + information. + */ + + parseTable(element) { + + if (element.prop('tagName') !== "TABLE") { + return this.parseTable(element.next()); } let themes = []; - table.children('tbody').children('tr').each(function () { - const $ = cheerio.load(this); - const td = $('td'); // Theme row - let name = replaceAll(td.first().text(), """, "\"") - let linkEl = td.eq(1).children().first(); - let link = linkEl.attr('href'); - let linkDesc = linkEl.text(); - let episodes = td.eq(2).text(); - let notes = td.eq(3).text(); + + 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 : ""; + + themes.push({ name, link, desc: linkDesc, - type: (name.startsWith('OP') ? 'opening' : 'ending'), + type: name.startsWith('OP') ? 'Opening' : name.startsWith('ED') ? 'Ending' : 'OP/ED', episodes, notes - }) - }) - + }); + }); return themes; + } } async function redditocall(href) { - let resp = await homgot(REDDIT_ANIMETHEMES + href + ".json", { parse: true }) + let resp = await homgot(REDDIT_ANIMETHEMES + href + ".json", { parse: true }); return cheerio.load(getHTML(resp.data.content_html)); } + function getHTML(str) { - let html = replaceAll(str, "<", "<") - html = replaceAll(html, ">", ">") + let html = replaceAll(str, "<", "<"); + html = replaceAll(html, ">", ">"); return html; } @@ -281,4 +306,5 @@ function replaceAll(str, find, replace) { return str.replace(new RegExp(find, 'g'), replace); } + module.exports = ThemeParser; From b8374812e744c86bd436d5a5ead56e4e380bc315 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sat, 23 Jan 2021 13:33:56 +0000 Subject: [PATCH 03/19] Fixing the animeTheme.js and refactoring in some files --- src/api/api.js | 9 ++- src/utils/animetheme.js | 3 +- src/utils/index.js | 161 +++++++++++++++++++++------------------- 3 files changed, 93 insertions(+), 80 deletions(-) diff --git a/src/api/api.js b/src/api/api.js index 922a023..dd0546d 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -284,10 +284,15 @@ const getEpisodes = async (title) =>{ const getAnimeServers = async (id) => { if (isNaN(id.split('/')[0])) { - return await videoServersJK(id) + + return await videoServersJK(id); + } else { + const data = await homgot(`${BASE_ANIMEFLV_JELU}GetAnimeServers/${id}`, { parse: true }); + return await transformUrlServer(data.servers); + } }; @@ -483,7 +488,7 @@ const getPlatforms = async (id) => { const getProfilePlatform = async (id) => { - let data = await homgot(`${BASE_ARUPPI}res/documents/animelegal/platforms/${id}.json`, { parse: true }) + let data = await homgot(`${BASE_ARUPPI}res/documents/animelegal/platforms/${id}.json`, { parse: true }); let channelId = { id: data[0].youtubeId, part: 'snippet,id', order: 'date', maxResults: '50', prop: 'items' }; let videos = await getYoutubeVideos(channelId) diff --git a/src/utils/animetheme.js b/src/utils/animetheme.js index 81aa56e..3a29ab1 100644 --- a/src/utils/animetheme.js +++ b/src/utils/animetheme.js @@ -209,10 +209,11 @@ class ThemeParser { return new Promise(async resolve => { let years = this.$('h3 a'); - this.$('h3 a')[0].children[0].data + for (let i = 0; i < years.length; i++) { let yearElement = years[i]; + await this.year(this.$(yearElement).attr('href').split('/')[4]) .then(async animes => { this.animes = this.animes.concat(animes); diff --git a/src/utils/index.js b/src/utils/index.js index 3d09fc2..56f613e 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -22,17 +22,18 @@ global.btoa = btoa; async function videoServersJK(id) { const $ = await homgot(`${BASE_JKANIME}${id}`, { scrapy: true }); - + const scripts = $('script'); const episodes = $('div#reproductor-box li'); const serverNames = []; let servers = []; - episodes.each((index, element) => serverNames.push($(element).find('a').text())) + episodes.each((index, element) => serverNames.push($(element).find('a').text())); for (let i = 0; i < scripts.length; i++) { try { const contents = $(scripts[i]).html(); + if ((contents || '').includes('var video = [];')) { Array.from({ length: episodes.length }, (v, k) => { let index = Number(k + 1); @@ -123,7 +124,8 @@ const jkanimeInfo = async (id) => { }; const animeflvGenres = async (id) => { - let $ = await homgot(`${BASE_ANIMEFLV}${id}`, { scrapy: true }); + let $ = await homgot(`${BASE_ANIMEFLV}/${id}`, { scrapy: true }); + $('main.Main section.WdgtCn nav.Nvgnrs a').each((index, element) => { return $(element).attr('href').split('=')[1] || null; }); @@ -131,44 +133,50 @@ const animeflvGenres = async (id) => { const animeflvInfo = async (id) => { - let $ = await homgot(`${BASE_ANIMEFLV}anime/${id}`, { scrapy: true }); + let $ = await homgot(`${BASE_ANIMEFLV}/anime/${id}`, { scrapy: true }); let scripts = $('script').toArray(); const anime_info_ids = []; const anime_eps_data = []; - Array.from({ length: scripts.length }, (v, k) => { - const contents = $(scripts[k]).html(); + for (let script of scripts) { + const contents = $(script).html(); + if ((contents || '').includes('var anime_info = [')) { let anime_info = contents.split('var anime_info = ')[1].split(';\n')[0]; let dat_anime_info = JSON.parse(anime_info); anime_info_ids.push(dat_anime_info); } + if ((contents || '').includes('var episodes = [')) { let episodes = contents.split('var episodes = ')[1].split(';')[0]; - let eps_data = JSON.parse(episodes) + let eps_data = JSON.parse(episodes); anime_eps_data.push(eps_data); } - }); + } + const animeId = id; - let nextEpisodeDate + let nextEpisodeDate; if (anime_info_ids.length > 0) { if (anime_info_ids[0].length === 4) { - nextEpisodeDate = anime_info_ids[0][3] + nextEpisodeDate = anime_info_ids[0][3]; } else { - nextEpisodeDate = null + nextEpisodeDate = null; } } const amimeTempList = []; + for (const [key] of Object.entries(anime_eps_data)) { let episode = anime_eps_data[key].map(x => x[0]); let episodeId = anime_eps_data[key].map(x => x[1]); amimeTempList.push(episode, episodeId); } + const animeListEps = [{ nextEpisodeDate: nextEpisodeDate }]; - for (let i = 0; i < amimeTempList[1].length; i++){ + + for (let i = 0; i < amimeTempList[1].length; i++) { let data = amimeTempList.map(x => x[i]); let episode = data[0]; let id = data[1]; @@ -177,65 +185,68 @@ const animeflvInfo = async (id) => { animeListEps.push({ episode: episode, id: link, - }) + }); } - return animeListEps - + return animeListEps; }; -const getAnimeCharacters = async(title) =>{ - const matchAnime = await getMALid(title) +const getAnimeCharacters = async (mal_id) =>{ + + const dataAnime = await homgot(`${BASE_JIKAN}/anime/${mal_id}/characters_staff`, { parse: true }); try { if(matchAnime !== null) { - const data = await homgot(`${BASE_JIKAN}anime/${matchAnime.mal_id}/characters_staff`, { parse: true }); - return data.characters.map(doc => ({ - id: doc.mal_id, - name: doc.name, - image: doc.image_url, - role: doc.role + + return dataAnime.characters.map(item=> ({ + id: item.mal_id, + name: item.name, + image: item.image_url, + role: item.role })); } } catch (err) { - console.log(err) + console.log(err); } }; -const getAnimeVideoPromo = async(title) =>{ +const getAnimeVideoPromo = async(mal_id) =>{ - const matchAnime = await getMALid(title) + const dataAnime = await homgot(`${BASE_JIKAN}/anime/${mal_id}`); try { - if(matchAnime !== null) { - const data = await homgot(`${BASE_JIKAN}anime/${matchAnime.mal_id}/videos`, {parse: true}) - return data.promo.map(doc => ({ + + if(dataAnime !== null) { + + return dataAnime.promo.map(doc => ({ title: doc.title, previewImage: doc.image_url, videoURL: doc.video_url })); + } + } catch (err) { console.log(err) } - }; -const animeExtraInfo = async (title) => { - - const matchAnime = await getMALid(title) +const animeExtraInfo = async (mal_id) => { + //@TESTING + const dataAnime = await homgot(`${BASE_JIKAN}/anime/${mal_id}`, { parse: true }); + console.log(dataAnime); try { - if(matchAnime !== null) { + if(dataAnime !== null) { + - const data = await homgot(`${BASE_JIKAN}anime/${matchAnime.mal_id}`, {parse: true}) const promises = []; - let broadcast = '' + let broadcast = ''; - Array(data).map(doc => { + Array(dataAnime).map(doc => { let airDay = { 'mondays': 'Lunes', @@ -256,11 +267,11 @@ const animeExtraInfo = async (title) => { }; if (doc.broadcast === null) { - broadcast = null + broadcast = null; } else { - broadcast = airDay[doc.broadcast.split('at')[0].replace(" ", "").toLowerCase()] + broadcast = airDay[doc.broadcast.split('at')[0].replace(" ", "").toLowerCase()]; } - + promises.push({ titleJapanese: doc.title_japanese, source: doc.source, @@ -279,7 +290,7 @@ const animeExtraInfo = async (title) => { endingThemes: doc.ending_themes || null }); }); - return Promise.all(promises); + return data; } } catch (err) { @@ -288,28 +299,6 @@ const animeExtraInfo = async (title) => { }; -const getMALid = async(title) =>{ - - - if (title === undefined || title === null) { - - return 1 - - } else { - - const res = await homgot(`${BASE_JIKAN}search/anime?q=${title}`,{ parse: true }) - const matchAnime = res.results.find(x => x.title === title); - - if(typeof matchAnime === 'undefined') { - return null; - } else { - return matchAnime; - } - - - } - -}; const imageUrlToBase64 = async (url) => { let img = await homgot(url) @@ -384,44 +373,63 @@ const obtainPreviewNews = (encoded) => { return image; } -const structureThemes = async (body, indv) => { - const promises = [] + +/* - StructureThemes + This function only parses the theme/themes + if indv is true, then only return a object, if it's false + then returns a array with the themes selected. +*/ +const structureThemes = async (body, indv) => { + if (indv === true) { - promises.push({ + + return { title: body.title, year: body.year, - themes: await getThemesData(body.themes), - }); + themes: await getThemesData(body.themes) + }; + } else { for (let i = 0; i <= body.length - 1; i++) { - promises.push({ + + const themes = []; + + themes.push({ title: body[i].title, year: body[i].year, themes: await getThemesData(body[i].themes), }); } + + return themes; } - return promises; + }; +/* - GetThemesData + Get the themes from the object and + format to a new array of items where + these items are formatted better. +*/ const getThemesData = async (themes) => { - let promises = [] + let items = []; for (let i = 0; i <= themes.length - 1; i++) { - promises.push({ + items.push({ title: themes[i].name.split('"')[1] || 'Remasterización', - type: themes[i].name.split('"')[0] || 'OP/ED', - episodes: themes[i].episodes || null, + type: themes[i].type, + episodes: themes[i].episodes !== "" ? themes[i].episodes : null, + notes: themes[i].notes !== "" ? themes[i].notes : null, video: themes[i].link }); } - - return promises.filter(x => x.title !== 'Remasterización'); + + return items.filter(x => x.title !== 'Remasterización'); }; @@ -442,7 +450,6 @@ module.exports = { getAnimeCharacters, getAnimeVideoPromo, animeExtraInfo, - getMALid, imageUrlToBase64, searchAnime, transformUrlServer, From 1532e15bbf658de3ab9c0a93a158b2cd4ec5a850 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sat, 23 Jan 2021 17:56:54 +0000 Subject: [PATCH 04/19] Missing a function to the funcionality of the app --- src/api/api.js | 13 +++---- src/api/routes/index.js | 6 ++-- src/utils/index.js | 79 +++++++++++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/api/api.js b/src/api/api.js index dd0546d..6848fa0 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -242,6 +242,7 @@ const getMoreInfo = async (title) =>{ let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); let result = data.filter(anime => anime.title === title)[0]; + console.log(result); return { title: result.title || null, @@ -257,7 +258,7 @@ const getMoreInfo = async (title) =>{ } } catch (e) { - console.log(e) + console.log(e); } }; @@ -267,16 +268,16 @@ const getEpisodes = async (title) =>{ try { let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); - const res = data.filter(x => x.title === title || x.mal_title === title)[0]; + const result = data.filter(x => x.title === title || x.mal_title === title)[0]; - if (!res.jkanime) { - return await animeflvInfo(res.id).then(episodes => episodes || null) + if (!result.jkanime) { + return await animeflvInfo(result.id).then(episodes => episodes || null); } else { - return await jkanimeInfo(res.id).then(episodes => episodes || null) + return await jkanimeInfo(result.id).then(episodes => episodes || null); } } catch (e) { - console.log(e) + console.log(e); } }; diff --git a/src/api/routes/index.js b/src/api/routes/index.js index b5d7b16..9007695 100644 --- a/src/api/routes/index.js +++ b/src/api/routes/index.js @@ -310,9 +310,9 @@ router.get('/getAnimeServers/:id([^/]+/[^/]+)' , (req, res) =>{ res.status(200).json({ servers }); - } 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); }); diff --git a/src/utils/index.js b/src/utils/index.js index 56f613e..0a5dd93 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -48,6 +48,7 @@ async function videoServersJK(id) { } let serverList = []; + for (let server in servers) { serverList.push({ id: serverNames[server].toLowerCase(), @@ -61,6 +62,7 @@ async function videoServersJK(id) { return await Promise.all(serverList); } + async function getVideoURL(url) { const $ = await homgot(url, { scrapy: true }); @@ -192,18 +194,18 @@ const animeflvInfo = async (id) => { }; -const getAnimeCharacters = async (mal_id) =>{ +const getAnimeCharacters = async(title) =>{ - const dataAnime = await homgot(`${BASE_JIKAN}/anime/${mal_id}/characters_staff`, { parse: true }); + const matchAnime = await getMALid(title); try { if(matchAnime !== null) { - - return dataAnime.characters.map(item=> ({ - id: item.mal_id, - name: item.name, - image: item.image_url, - role: item.role + const data = await homgot(`${BASE_JIKAN}anime/${matchAnime.mal_id}/characters_staff`, { parse: true }); + return data.characters.map(doc => ({ + id: doc.mal_id, + name: doc.name, + image: doc.image_url, + role: doc.role })); } } catch (err) { @@ -212,41 +214,40 @@ const getAnimeCharacters = async (mal_id) =>{ }; -const getAnimeVideoPromo = async(mal_id) =>{ +const getAnimeVideoPromo = async(title) =>{ - const dataAnime = await homgot(`${BASE_JIKAN}/anime/${mal_id}`); + const matchAnime = await getMALid(title); try { + if(matchAnime !== null) { - if(dataAnime !== null) { + const data = await homgot(`${BASE_JIKAN}anime/${matchAnime.mal_id}/videos`, {parse: true}); - return dataAnime.promo.map(doc => ({ + return data.promo.map(doc => ({ title: doc.title, previewImage: doc.image_url, videoURL: doc.video_url })); - } - } catch (err) { - console.log(err) + console.log(err); } + }; -const animeExtraInfo = async (mal_id) => { +const animeExtraInfo = async (title) => { - //@TESTING - const dataAnime = await homgot(`${BASE_JIKAN}/anime/${mal_id}`, { parse: true }); - console.log(dataAnime); - try { + const matchAnime = await getMALid(title) - if(dataAnime !== null) { + try { + if(matchAnime !== null) { + const data = await homgot(`${BASE_JIKAN}anime/${matchAnime.mal_id}`, {parse: true}) const promises = []; - let broadcast = ''; + let broadcast = '' - Array(dataAnime).map(doc => { + Array(data).map(doc => { let airDay = { 'mondays': 'Lunes', @@ -267,11 +268,11 @@ const animeExtraInfo = async (mal_id) => { }; if (doc.broadcast === null) { - broadcast = null; + broadcast = null } else { - broadcast = airDay[doc.broadcast.split('at')[0].replace(" ", "").toLowerCase()]; + broadcast = airDay[doc.broadcast.split('at')[0].replace(" ", "").toLowerCase()] } - + promises.push({ titleJapanese: doc.title_japanese, source: doc.source, @@ -290,7 +291,7 @@ const animeExtraInfo = async (mal_id) => { endingThemes: doc.ending_themes || null }); }); - return data; + return Promise.all(promises); } } catch (err) { @@ -299,6 +300,29 @@ const animeExtraInfo = async (mal_id) => { }; +const getMALid = async(title) =>{ + + + if (title === undefined || title === null) { + + return 1 + + } else { + + const res = await homgot(`${BASE_JIKAN}search/anime?q=${title}`,{ parse: true }) + const matchAnime = res.results.find(x => x.title === title); + + if(typeof matchAnime === 'undefined') { + return null; + } else { + return matchAnime; + } + + + } + +}; + const imageUrlToBase64 = async (url) => { let img = await homgot(url) @@ -456,5 +480,6 @@ module.exports = { obtainPreviewNews, structureThemes, getThemes, + getMALid, videoServersJK } From 2746613f9fe37f235394b75874dc988d275850b5 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sat, 23 Jan 2021 21:00:01 +0000 Subject: [PATCH 05/19] Fixing issue with videoServersJK not getting the url from a episode --- src/utils/index.js | 86 ++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 52 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 0a5dd93..30b640c 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -22,37 +22,37 @@ global.btoa = btoa; async function videoServersJK(id) { const $ = await homgot(`${BASE_JKANIME}${id}`, { scrapy: true }); + let servers = {}; + let script; + const serverNames = $('div#reproductor-box li').map((index, element) => { + return $(element).find('a').text(); + }).get(); + + $('script').each((index, element) => { + if ($(element).html().includes('var video = [];')) { + script = $(element).html(); + } + }); - const scripts = $('script'); - const episodes = $('div#reproductor-box li'); - const serverNames = []; - let servers = []; - - episodes.each((index, element) => serverNames.push($(element).find('a').text())); - - for (let i = 0; i < scripts.length; i++) { - try { - const contents = $(scripts[i]).html(); - - if ((contents || '').includes('var video = [];')) { - Array.from({ length: episodes.length }, (v, k) => { - let index = Number(k + 1); - let videoPageURL = contents.split(`video[${index}] = \'