Adding the randomAnime route, and fixing some issue with the themeYears function

pull/15/head
capitanwesler 5 years ago
parent 17510dbb5b
commit 720cddb4d5

@ -242,7 +242,7 @@ const getMoreInfo = async (title) =>{
try { try {
let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); 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 { return {
title: result.title || null, title: result.title || null,
@ -265,9 +265,7 @@ const getMoreInfo = async (title) =>{
}; };
const getEpisodes = async (title) =>{ const getEpisodes = async (title) =>{
try { try {
let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); let data = JSON.parse(JSON.stringify(require('../assets/directory.json')));
const result = data.filter(x => x.title === title || x.mal_title === title)[0]; 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 getOpAndEd = async (title) => await structureThemes(await parserThemes.serie(title), true);
const getThemesYear = async (year) => { const getThemesYear = async (year) => {
let data = []; let data = [];
if (year === undefined) { if (year === undefined) {
return await parserThemes.allYears(); return await parserThemes.allYears();
} else { } else {
data = await parserThemes.year(year) data = await parserThemes.year(year);
return await structureThemes(data, false) 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 = { module.exports = {
schedule, schedule,
top, top,
@ -563,5 +568,6 @@ module.exports = {
getPlatforms, getPlatforms,
getSectionYoutubeVideos, getSectionYoutubeVideos,
getProfilePlatform, getProfilePlatform,
getRelatedAnimes getRelatedAnimes,
getRandomAnime
}; };

@ -320,7 +320,6 @@ router.get('/getAnimeServers/:id([^/]+/[^/]+)' , (req, res) =>{
}); });
router.get('/search/:title' , (req, res) =>{ router.get('/search/:title' , (req, res) =>{
let title = req.params.title; let title = req.params.title;
api.search(title) api.search(title)
@ -339,7 +338,6 @@ router.get('/search/:title' , (req, res) =>{
}); });
router.get('/images/:query' , (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' }; let query = { title: req.params.query, count: '51', type: 'images', safesearch: '1', country: 'es_ES', uiv: '4' };
api.getImages(query) api.getImages(query)
@ -358,7 +356,6 @@ router.get('/images/:query' , (req, res) =>{
}); });
router.get('/videos/:channelId' , (req, res) =>{ router.get('/videos/:channelId' , (req, res) =>{
let channelId = { id: req.params.channelId, part: 'snippet,id', order: 'date', maxResults: '50', prop: 'items' }; let channelId = { id: req.params.channelId, part: 'snippet,id', order: 'date', maxResults: '50', prop: 'items' };
api.getYoutubeVideos(channelId) api.getYoutubeVideos(channelId)
@ -377,7 +374,6 @@ router.get('/videos/:channelId' , (req, res) =>{
}); });
router.get('/sectionedVideos/:type' , (req, res) =>{ router.get('/sectionedVideos/:type' , (req, res) =>{
let type = req.params.type; let type = req.params.type;
api.getSectionYoutubeVideos(type) api.getSectionYoutubeVideos(type)
@ -396,7 +392,6 @@ router.get('/sectionedVideos/:type' , (req, res) =>{
}); });
router.get('/radio' , (req, res) =>{ router.get('/radio' , (req, res) =>{
api.getRadioStations() api.getRadioStations()
.then(stations =>{ .then(stations =>{
if (stations.length > 0) { if (stations.length > 0) {
@ -431,10 +426,8 @@ router.get('/allThemes', (req, res) =>{
router.get('/themes/:title' , (req, res) =>{ router.get('/themes/:title' , (req, res) =>{
let title = req.params.title; let title = req.params.title;
api.getOpAndEd(title) api.getOpAndEd(title)
.then(themes => { .then(themes => {
if (themes) { if (themes) {
@ -451,19 +444,17 @@ router.get('/themes/:title' , (req, res) =>{
}); });
router.get('/themesYear/:year?', (req, res) =>{ router.get('/themesYear/:year?', (req, res) =>{
let year = req.params.year; let year = req.params.year;
let season = req.params.season
api.getThemesYear(year, season) api.getThemesYear(year)
.then(themes =>{ .then(themes =>{
if (themes.length > 0) { if (themes.length > 0) {
res.status(200).json({ res.status(200).json({
themes themes
}); });
} else ( } else {
res.status(500).json({ message: 'Aruppi lost in the shell'}) res.status(500).json({ message: 'Aruppi lost in the shell'});
) }
}).catch((err) =>{ }).catch((err) =>{
console.error(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 }; let genres = { genre: req.params.genre, order: req.params.order, page: req.params.page };
api.getAnimeGenres(genres) api.getAnimeGenres(genres)
.then(animes =>{ .then(animes => {
if (animes.length > 0) { if (animes.length > 0) {
res.status(200).json({ res.status(200).json({
animes animes
}); });
} else ( } else {
res.status(500).json({ message: 'Aruppi lost in the shell'}) res.status(500).json({ message: 'Aruppi lost in the shell'});
) }
}).catch((err) =>{ }).catch((err) =>{
console.error(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) =>{ router.get('/destAnimePlatforms' , (req , res) =>{
api.getDestAnimePlatforms() api.getDestAnimePlatforms()

@ -82,11 +82,11 @@ class ThemeParser {
let animes = []; let animes = [];
this.$ = await redditocall(date); this.$ = await redditocall(date);
this.$('h3').each((i, el) => { this.$('h3').each((index, element) => {
let parsed = this.parseAnime(el); let parsed = this.parseAnime(this.$(element));
parsed.year = date; parsed.year = date;
animes.push(parsed); animes.push(parsed);
}) });
return animes; 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() { parseYears() {
return new Promise(async resolve => { return new Promise(async resolve => {
let years = [];
let promises = []; this.$('h3 a').each((index, element) => {
let data = this.$('h3 a'); years.push(
{
for (let i = 0; i < data.length; i++) { id: this.$(element).attr('href').split('/')[4],
name: this.$(element).text()
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);
}
}
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) { parseAnime(element) {
let el = this.$(element).children('a'); let el = this.$(element).find('a');
let title = el.text(); let title = this.$(el).text();
let mal_id = el.attr('href').split('/')[4]; let mal_id = this.$(el).attr('href').split('/')[4];
let next = this.$(element).next(); let next = this.$(element).next();
let theme = { let theme = {
@ -237,18 +243,12 @@ class ThemeParser {
title title
}; };
if (this.$(next).prop("tagName") === "TABLE") {
if (next.prop("tagName") === "TABLE") { theme.themes = this.parseTable(this.$(next));
}else if (this.$(next).prop("tagName") === "P") {
theme.themes = this.parseTable(next); theme.themes = this.parseTable(this.$(next).next());
}else if (next.prop("tagName") === "P") {
theme.themes = this.parseTable(next.next());
} }
return theme; return theme;
} }
@ -257,25 +257,22 @@ class ThemeParser {
and returns a object with all the and returns a object with all the
information. information.
*/ */
parseTable(element) { parseTable(element) {
if (element.prop('tagName') !== "TABLE") { if (this.$(element).prop('tagName') !== "TABLE") {
return this.parseTable(element.next()); return this.parseTable(this.$(element).next());
} }
let themes = []; let themes = [];
element.find('tbody').find('tr').each((i, elem) => { this.$(element).find('tbody').find('tr').each((i, elem) => {
let name = replaceAll(elem.children[1].children[0].data, "&quot;", "\"");
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 : "";
let name = replaceAll(this.$(elem).find('td').eq(0).text(), '&quot;', '"');
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({ themes.push({
name, name,
@ -286,8 +283,8 @@ class ThemeParser {
notes notes
}); });
}); });
return themes;
return themes;
} }
} }

@ -138,14 +138,16 @@ const jkanimeInfo = async (id) => {
}; };
function getPoster(id) { function getPosterAndType(id) {
let data = JSON.parse(JSON.stringify(require('../assets/directory.json'))); let data = JSON.parse(JSON.stringify(require('../assets/directory.json')));
//@CHECK
for (let anime of data) { for (let anime of data) {
if (anime.id === id) { 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) { async function getRelatedAnimes(id) {
const $ = await homgot(`${BASE_ANIMEFLV}/anime/${id}`, { scrapy: true }); const $ = await homgot(`${BASE_ANIMEFLV}/anime/${id}`, { scrapy: true });
let listRelated = {}; let listRelated = {};
let relatedAnimes = []; let relatedAnimes = [];
@ -164,9 +165,8 @@ async function getRelatedAnimes(id) {
}); });
for (related in listRelated) { for (related in listRelated) {
let posterUrl = getPoster(listRelated[related].split('/')[2]); let posterUrl = getPosterAndType(listRelated[related].split('/')[2]);
//@CHECK
relatedAnimes.push( relatedAnimes.push(
{ {
id: listRelated[related].split('/')[2], id: listRelated[related].split('/')[2],
@ -193,7 +193,6 @@ const animeflvGenres = async (id) => {
} }
const animeflvInfo = 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(); let scripts = $('script').toArray();
@ -459,6 +458,7 @@ const obtainPreviewNews = (encoded) => {
then returns a array with the themes selected. then returns a array with the themes selected.
*/ */
const structureThemes = async (body, indv) => { const structureThemes = async (body, indv) => {
let themes = [];
if (indv === true) { if (indv === true) {
@ -471,8 +471,6 @@ const structureThemes = async (body, indv) => {
} else { } else {
for (let i = 0; i <= body.length - 1; i++) { for (let i = 0; i <= body.length - 1; i++) {
const themes = [];
themes.push({ themes.push({
title: body[i].title, title: body[i].title,
year: body[i].year, year: body[i].year,

Loading…
Cancel
Save