Anilist Wrapper clean up

master
masterhc 1 year ago
parent 4cf4305a69
commit bf2a9b15b2

169
lib.js

@ -537,15 +537,17 @@ class AnimeInfo
* *
* @param {String} title * @param {String} title
* @param {String} status * @param {String} status
* @param {String} url
* @param {String} episodes * @param {String} episodes
* @param {String} trailer * @param {String} trailer
* @param {String} description * @param {String} description
* @param {String} coverImage * @param {String} coverImage
*/ */
constructor(title, status, episodes, trailer, description, coverImage) constructor(title, status, episodes, url , trailer, description, coverImage)
{ {
this.title = title; this.title = title;
this.status = status; this.status = status;
this.url = url
this.episodes=episodes; this.episodes=episodes;
this.trailer = trailer; this.trailer = trailer;
this.description = description; this.description = description;
@ -559,12 +561,14 @@ class CharInfo
* @param {String} name * @param {String} name
* @param {String} gender * @param {String} gender
* @param {String} image * @param {String} image
* @param {String} url
* @param {String} description * @param {String} description
*/ */
constructor(name, gender, image, description) constructor(name, gender, image, description)
{ {
this.name = name; this.name = name;
this.gender = gender; this.gender = gender;
this.url = url
this.image=image; this.image=image;
this.description = description; this.description = description;
} }
@ -575,6 +579,7 @@ class MangaInfo
* *
* @param {String} title * @param {String} title
* @param {String} status * @param {String} status
* @param {String} url
* @param {String} description * @param {String} description
* @param {String} coverImage * @param {String} coverImage
*/ */
@ -582,12 +587,54 @@ class MangaInfo
{ {
this.title = title; this.title = title;
this.status = status; this.status = status;
this.url = url
this.description = description; this.description = description;
this.coverImage = coverImage; this.coverImage = coverImage;
} }
} }
class Options
{
/**
*
* @param {String} method
* @param {String} headers
* @param {String} body
*/
constructor(method, headers, body)
{
this.method = method;
this.headers = headers;
this.body = body;
}
}
class aniListCli { class aniListCli {
/**
*
* @param {Options} options
* @returns {Info}
*/
async getInfo(options)
{
return await fetch('https://graphql.anilist.co', options).then(handleResponse)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleData(data)
{
return data
}
function handleError(error) {
return error;
}
}
/** /**
* *
* @param {String} ss - Search Query * @param {String} ss - Search Query
@ -615,8 +662,7 @@ class aniListCli {
} }
} }
` `
var url = 'https://graphql.anilist.co', var options = {
options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -627,20 +673,8 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
data = data.data.Page.media data = data.data.Page.media
if(!data[0]) return 'Error' if(!data[0]) return 'Error'
var aux = []; var aux = [];
@ -654,7 +688,6 @@ class aniListCli {
} }
return aux; return aux;
} }
}
async getAnimeInfo(id) async getAnimeInfo(id)
{ {
var variables = { var variables = {
@ -682,8 +715,7 @@ class aniListCli {
} }
} }
}`; }`;
var url = 'https://graphql.anilist.co', var options = {
options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -694,20 +726,7 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options);
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
return { return {
title:data.data.Media.title, title:data.data.Media.title,
malLink:'https://myanimelist.net/anime/'+data.data.Media.idMal, malLink:'https://myanimelist.net/anime/'+data.data.Media.idMal,
@ -719,8 +738,6 @@ class aniListCli {
coverImage:data.data.Media.coverImage.large coverImage:data.data.Media.coverImage.large
} }
} }
}
/** /**
* *
* @param {String} ss - Search Query * @param {String} ss - Search Query
@ -750,8 +767,7 @@ class aniListCli {
} }
} }
`; `;
var url = 'https://graphql.anilist.co', var options = {
options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -762,20 +778,7 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
var chars = data.data.Page.characters var chars = data.data.Page.characters
var aux = []; var aux = [];
if(!chars[0]) return 'Error' if(!chars[0]) return 'Error'
@ -791,8 +794,6 @@ class aniListCli {
} }
return aux; return aux;
} }
}
async getCharInfo(id) async getCharInfo(id)
{ {
@ -815,8 +816,7 @@ class aniListCli {
} }
} }
`; `;
var url = 'https://graphql.anilist.co', var options = {
options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -827,20 +827,7 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
return { return {
name:data.data.Character.name, name:data.data.Character.name,
gender:data.data.Character.gender, gender:data.data.Character.gender,
@ -848,7 +835,7 @@ class aniListCli {
image:data.data.Character.image.medium, image:data.data.Character.image.medium,
description:data.data.Character.description, description:data.data.Character.description,
} }
}
} }
/** /**
@ -878,8 +865,7 @@ class aniListCli {
} }
} }
}` }`
var url = 'https://graphql.anilist.co', var options = {
options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -890,20 +876,7 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
var mangas = data.data.Page.media; var mangas = data.data.Page.media;
var aux = []; var aux = [];
if(!mangas[0]) return 'Error' if(!mangas[0]) return 'Error'
@ -916,7 +889,7 @@ class aniListCli {
}) })
} }
return aux; return aux;
}
} }
async getMangaInfo(id) async getMangaInfo(id)
{ {
@ -939,8 +912,7 @@ class aniListCli {
} }
} }
`; `;
var url = 'https://graphql.anilist.co', var options = {
options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -951,20 +923,7 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
return { return {
title:data.data.Media.title, title:data.data.Media.title,
status:data.data.Media.status, status:data.data.Media.status,
@ -974,8 +933,6 @@ class aniListCli {
} }
} }
}
}module.exports.aniList = aniListCli; }module.exports.aniList = aniListCli;
const channelM = require('./models/channels'); const channelM = require('./models/channels');

Loading…
Cancel
Save