Added Anilist Api
- moved graphql schemas and queries to lib/graphql - added anilistApiLib that contains functions to access the anilist graphql api - added graphql queries to lib/graphql to access data on anilist - added global ~anime command that returns information to an anime - modified help command so that it shows command categories for global and server commands - moved global command registration to lib/cmdpull/51/head
parent
aeaebb4f33
commit
42ee8cc4c5
@ -0,0 +1,103 @@
|
||||
const fetch = require('node-fetch'),
|
||||
fsx = require('fs-extra'),
|
||||
queryPath = './lib/graphql/AnilistApi',
|
||||
alApiEndpoint = 'https://graphql.anilist.co';
|
||||
|
||||
/**
|
||||
* Return a graphql query read from a file from a configured path.
|
||||
* @param name
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
async function getGraphqlQuery(name) {
|
||||
return await fsx.readFile(`${queryPath}/${name}.gql`, {encoding: 'utf-8'});
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a query read from a file to the configured graphql endpoint and return the data.
|
||||
* @param queryName
|
||||
* @param queryVariables
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
function postGraphqlQuery(queryName, queryVariables) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
fetch(alApiEndpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query: await getGraphqlQuery(queryName),
|
||||
variables: queryVariables
|
||||
})
|
||||
}).then(async (response) => {
|
||||
let json = await response.json();
|
||||
return response.ok ? json: Promise.reject(json);
|
||||
}).then((data) => resolve(data.data)).catch((err) => reject(err));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an anime by id.
|
||||
* @param id
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
exports.getAnimeById = async function(id) {
|
||||
let data = await postGraphqlQuery('AnimeById', {id: id});
|
||||
if (data.Media)
|
||||
return data.Media;
|
||||
else
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a manga by id.
|
||||
* @param id
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
exports.getMangaById = async function(id) {
|
||||
let data = await postGraphqlQuery('MangaById', {id: id});
|
||||
if (data.Media)
|
||||
return data.Media;
|
||||
else
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Search for a media entry by name and return it.
|
||||
* @param name
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
exports.searchMediaByName = async function(name) {
|
||||
let data = await postGraphqlQuery('MediaSearchByName', {name: name});
|
||||
if (data.Media)
|
||||
return data.Media;
|
||||
else
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Search for an anime by name and get it by id.
|
||||
* @param name
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
exports.searchAnimeByName = async function(name) {
|
||||
let data = await postGraphqlQuery('MediaSearchByName', {name: name, type: 'ANIME'});
|
||||
if (data && data.Media && data.Media.id)
|
||||
return await exports.getAnimeById(data.Media.id);
|
||||
else
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Search for a manga by name and get it by id.
|
||||
* @param name
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
exports.searchMangaByName = async function(name) {
|
||||
let data = await postGraphqlQuery('MediaSearchByName', {name: name, type: 'MANGA'}).data;
|
||||
if (data && data.Media && data.Media.id)
|
||||
return await postGraphqlQuery('MangaById', {id: data.Media.id});
|
||||
else
|
||||
return null;
|
||||
};
|
@ -0,0 +1,47 @@
|
||||
query ($id: Int) {
|
||||
Media (id: $id, type: ANIME) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
english
|
||||
native
|
||||
}
|
||||
status
|
||||
startDate {
|
||||
year
|
||||
month
|
||||
day
|
||||
}
|
||||
endDate {
|
||||
year
|
||||
month
|
||||
day
|
||||
}
|
||||
format
|
||||
season
|
||||
episodes
|
||||
duration
|
||||
genres
|
||||
siteUrl
|
||||
coverImage {
|
||||
large
|
||||
medium
|
||||
color
|
||||
}
|
||||
description(asHtml: false)
|
||||
averageScore
|
||||
favourites
|
||||
studios(isMain: true) {
|
||||
studioList: nodes {
|
||||
id
|
||||
name
|
||||
siteUrl
|
||||
}
|
||||
}
|
||||
nextAiringEpisode {
|
||||
id
|
||||
airingAt
|
||||
episode
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
query ($id: Int) {
|
||||
Media (id: $id, type: MANGA) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
english
|
||||
native
|
||||
}
|
||||
status
|
||||
startDate {
|
||||
year
|
||||
month
|
||||
day
|
||||
}
|
||||
endDate {
|
||||
year
|
||||
month
|
||||
day
|
||||
}
|
||||
format
|
||||
chapters
|
||||
volumes
|
||||
genres
|
||||
siteUrl
|
||||
coverImage {
|
||||
large
|
||||
medium
|
||||
color
|
||||
}
|
||||
staff {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
name {
|
||||
first
|
||||
last
|
||||
native
|
||||
}
|
||||
image {
|
||||
large
|
||||
medium
|
||||
}
|
||||
language
|
||||
siteUrl
|
||||
}
|
||||
role
|
||||
}
|
||||
}
|
||||
description(asHtml: false)
|
||||
averageScore
|
||||
favourites
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
query ($name: String, $type: MediaType) {
|
||||
Media (search: $name, type: $type) {
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
english
|
||||
native
|
||||
}
|
||||
type
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue