pseudosubs-v1/srv/LinksAnimes/obtenerLinks.ts

152 lines
5.0 KiB
TypeScript
Raw Normal View History

const GestorDeTareas = require("../GestorDeTareas/GestorDeTareas").GestorDeTareas;
2018-12-13 01:10:51 +00:00
const YAML = require('yaml');
interface links {
links_ID: number,
anime_ID: number,
aviso: string,
sigEp: string
}
interface links_opciones {
opcion_ID: number,
links_ID: number,
num_opcion: number,
formato: string,
res: string,
servidor: string,
color: string
}
interface eps {
ep_ID: number,
opcion_ID: number,
num_ep: number,
visitas: number,
link: string,
peso: string
}
interface LinksEps {
ep_ID: number,
visitas: number,
peso: string,
link: string
}
interface Links {
aviso: string,
sigEp: string
opciones: {
[num: number]: {
formato: string,
res: string,
servidor: string,
color: string,
eps: {
[num: number]: LinksEps
}
}
}
}
const obtenerLinks = (req: any, res: any) => {
const con = require('../index').conexionMySQL;
2018-12-13 01:10:51 +00:00
const animeID = req.body.animeID;
2018-12-13 01:10:51 +00:00
if (animeID) {
2018-12-13 01:10:51 +00:00
const data: Links = {aviso: '', sigEp: '', opciones: {}};
con.query(
`SELECT * FROM links WHERE anime_ID=${animeID} `,
(err: Error, response: links[]) => {
if (!err && response[0] ) {
2018-12-13 01:10:51 +00:00
const subData: links = response[0];
data.aviso = subData.aviso;
data.sigEp = subData.sigEp;
2018-12-13 01:10:51 +00:00
const gestorOpciones = new GestorDeTareas(() => {
res.send(YAML.stringify(data));
});
2018-12-13 01:10:51 +00:00
con.query(
`SELECT * FROM links_opciones WHERE links_ID=${subData.links_ID}`,
(err: Error, response: links_opciones[]) => {
if (!err && response[0]) {
2018-12-13 01:10:51 +00:00
for (const opcionID in response) {
const opcion = response[opcionID];
2018-12-13 01:10:51 +00:00
gestorOpciones.agregarTarea();
2018-12-13 01:10:51 +00:00
data.opciones[opcion.num_opcion] = {
formato: opcion.formato,
res: opcion.res,
servidor: opcion.servidor,
color: opcion.color,
eps: {}
};
2018-12-13 01:10:51 +00:00
con.query(
`SELECT * FROM eps WHERE opcion_ID=${opcion.opcion_ID}`,
(err: Error, response: eps[]) => {
if (!err && response[0]) {
2018-12-13 01:10:51 +00:00
for (const epID in response) {
const ep = response[epID];
2018-12-13 01:10:51 +00:00
data.opciones[opcion.num_opcion].eps[ep.num_ep] = {
ep_ID: ep.ep_ID,
visitas: ep.visitas,
peso: ep.peso,
link: ep.link
};
2018-12-13 01:10:51 +00:00
}
gestorOpciones.terminarTarea();
} else if (!err) {
console.log("No existen episodios para esta variante");
res.send(`{ "exito": false }`);
} else {
console.log("Error al obtener eps:\n" + err);
res.send(`{ "exito": false }`);
2018-12-13 01:10:51 +00:00
}
}
);
2018-12-13 01:10:51 +00:00
}
} else if (!err) {
console.log("No existen links_opciones ");
res.send(`{ "exito": false }`);
} else {
console.log("Error al obtener links opciones:\n" + err);
res.send(`{ "exito": false }`);
2018-12-13 01:10:51 +00:00
}
}
);
} else if (!err) {
console.log("Error: La consulta no dio ningun resultado en obtenerLinks");
res.send('{ "exito": false }');
} else {
console.log("Error al ejecutar Query en obtenerLinks.\n" + err);
res.send('{ "exito": false }');
}
}
);
2018-12-13 01:10:51 +00:00
} else {
console.log("AnimeID no existe");
res.send(`{ "exito": false }`);
}
};
module.exports.obtenerLinks = obtenerLinks;