Anilist Wrapper clean up

master
masterhc 1 year ago
parent 4cf4305a69
commit bf2a9b15b2

283
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,33 +673,20 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData)
.catch(handleError); data = data.data.Page.media
function handleResponse(response) if(!data[0]) return 'Error'
{ var aux = [];
return response.json().then(function (json) for(var anime of data)
{ {
return response.ok ? json : Promise.reject(json); aux.push({
}); title:anime.title,
} id:anime.id,
function handleError(error) { coverImage:anime.coverImage.medium
return error; });
} }
function handleData(data) { return aux;
data = data.data.Page.media
if(!data[0]) return 'Error'
var aux = [];
for(var anime of data)
{
aux.push({
title:anime.title,
id:anime.id,
coverImage:anime.coverImage.medium
});
}
return aux;
}
} }
async getAnimeInfo(id) async getAnimeInfo(id)
{ {
@ -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,32 +726,17 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options);
.then(handleData) return {
.catch(handleError); title:data.data.Media.title,
function handleResponse(response) malLink:'https://myanimelist.net/anime/'+data.data.Media.idMal,
{ status:data.data.Media.status,
return response.json().then(function (json) url:data.data.Media.siteUrl,
{ episodes:data.data.Media.episodes,
return response.ok ? json : Promise.reject(json); trailer:data.data.Media.trailer,
}); description:data.data.Media.description,
} coverImage:data.data.Media.coverImage.large
function handleError(error) { }
return error;
}
function handleData(data) {
return {
title:data.data.Media.title,
malLink:'https://myanimelist.net/anime/'+data.data.Media.idMal,
status:data.data.Media.status,
url:data.data.Media.siteUrl,
episodes:data.data.Media.episodes,
trailer:data.data.Media.trailer,
description:data.data.Media.description,
coverImage:data.data.Media.coverImage.large
}
}
} }
/** /**
* *
@ -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,36 +778,21 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData) var chars = data.data.Page.characters
.catch(handleError); var aux = [];
function handleResponse(response) if(!chars[0]) return 'Error'
for(var char of chars)
{ {
return response.json().then(function (json) aux.push({
{ id:char.id,
return response.ok ? json : Promise.reject(json); name:char.name,
}); gender:char.gender,
} url:char.siteUrl,
function handleError(error) { image:char.image.medium
return error; })
}
function handleData(data) {
var chars = data.data.Page.characters
var aux = [];
if(!chars[0]) return 'Error'
for(var char of chars)
{
aux.push({
id:char.id,
name:char.name,
gender:char.gender,
url:char.siteUrl,
image:char.image.medium
})
}
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,28 +827,15 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData) return {
.catch(handleError); name:data.data.Character.name,
function handleResponse(response) gender:data.data.Character.gender,
{ url:data.data.Character.siteUrl,
return response.json().then(function (json) image:data.data.Character.image.medium,
{ description:data.data.Character.description,
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
return {
name:data.data.Character.name,
gender:data.data.Character.gender,
url:data.data.Character.siteUrl,
image:data.data.Character.image.medium,
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,33 +876,20 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData) var mangas = data.data.Page.media;
.catch(handleError); var aux = [];
function handleResponse(response) if(!mangas[0]) return 'Error'
for(var manga of mangas)
{ {
return response.json().then(function (json) aux.push({
{ id:manga.id,
return response.ok ? json : Promise.reject(json); title:manga.title,
}); image:manga.coverImage.medium
} })
function handleError(error) {
return error;
}
function handleData(data) {
var mangas = data.data.Page.media;
var aux = [];
if(!mangas[0]) return 'Error'
for(var manga of mangas)
{
aux.push({
id:manga.id,
title:manga.title,
image:manga.coverImage.medium
})
}
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,29 +923,14 @@ class aniListCli {
variables variables
}) })
}; };
return await fetch(url, options).then(handleResponse) var data = await this.getInfo(options)
.then(handleData) return {
.catch(handleError); title:data.data.Media.title,
function handleResponse(response) status:data.data.Media.status,
{ url:data.data.Media.siteUrl,
return response.json().then(function (json) description:data.data.Media.description,
{ coverImage:data.data.Media.coverImage.large
return response.ok ? json : Promise.reject(json); }
});
}
function handleError(error) {
return error;
}
function handleData(data) {
return {
title:data.data.Media.title,
status:data.data.Media.status,
url:data.data.Media.siteUrl,
description:data.data.Media.description,
coverImage:data.data.Media.coverImage.large
}
}
} }
}module.exports.aniList = aniListCli; }module.exports.aniList = aniListCli;

Loading…
Cancel
Save