Skip to content

Commit

Permalink
Added Lists class
Browse files Browse the repository at this point in the history
  • Loading branch information
salixor committed May 30, 2019
1 parent 1cc4266 commit 1caf50b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 22 deletions.
15 changes: 13 additions & 2 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Usernames must be strings and ids must be numbers!<br/>
- `profile.siteUrl` | User's AniList URL
- `profile.donatorTier` | Check if the user is a donator
- `profile.moderatorStatus` | Check if the user is a moderator
- `profile.updatedAt` | Timestamp of the last update of the user
- `profile.updatedAt` | Timestamp of the last update of the user

## Stats Unique
- `stats.watchedTime` | Total amount of watch time on the user's anime list
Expand All @@ -138,10 +138,21 @@ Usernames must be strings and ids must be numbers!<br/>
- `stats.favouredYears` | List of the user's favourite years. Gives three values per object: year, amount, meanScore
- `stats.favouredFormats` | List of the user's favourite formats. Gives two values per object: format, amount

# Lists
Usernames must be strings and ids must be numbers!<br/>
`Anilist.lists.manga(username|id)` | User manga lists function<br/>
`Anilist.lists.anime(username|id)` | User anime lists function<br/>

- `list.name` | The user's list name
- `list.isCustomList` | Checks if the list is a custom one (not created by default by AniList)
- `list.isSplitCompletedList` | Checks if the list is a split completed list ie. if the user chose to have each completed media format in a separate list (toggled in user's settings)
- `list.status` | The user's list status ("CURRENT", "PLANNING", "COMPLETED", "PAUSED", "DROPPED", "REREADING", "REWATCHING")
- `list.entries` | List of media entries in this list (refer to `Media` documentation)

# Studio
- `Anilist.studio(id)` | Get information on a studio by an id
- `studio.id` | The studio's id
- `studio.name` | The studio's name
- `studio.media` | An array of ids of all of the media the studio has done.
- `studio.siteUrl` | Site url of the studio on Anilist
- `studio.isFavourite` | Checks if the studio is favourited [Requires login]
- `studio.isFavourite` | Checks if the studio is favourited [Requires login]
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const User = require('./lib/user'),
lists = require('./lib/lists'),
media = require('./lib/media'),
people = require('./lib/people'),
search = require('./search.json'),
Fetch = require('./lib/fetcher');

module.exports = class AniList {
constructor (accessKey) {
constructor (accessKey) {
Fetch.key = accessKey ? accessKey : null;

this.user = User;
this.user = User;
this.lists = lists;
this.media = media;
this.people = people;
};
Expand All @@ -27,7 +29,7 @@ module.exports = class AniList {
case "character": var query = search["char"]; break;
case "staff": var query = search["staff"]; break;
case "studio": var query = search["studio"]; break;
default: throw new Error("Type not supported.");
default: throw new Error("Type not supported.");
}
return Fetch.send(`query ($id: Int, $page: Int, $perPage: Int, $search: String) {
Page (page: $page, perPage: $perPage) { pageInfo { total currentPage lastPage hasNextPage perPage } ${query} } }`, { search: term, page: page, perPage: amount});
Expand Down
37 changes: 20 additions & 17 deletions lib/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const fetch = require('node-fetch');
module.exports = {
edgeRemove: function(obj) {
var list = [];
for (var x = 0; x < obj.length; x++) {
for (var x = 0; x < obj.length; x++) {
if (obj[x].node) { list.push(obj[x].node); }
else if (obj[x].id) { list.push(obj[x].id); }
else if (obj[x].url) { list.push(obj[x].url); }
else { list.push(obj[x]); }
else if (obj[x].id) { list.push(obj[x].id); }
else if (obj[x].url) { list.push(obj[x].url); }
else { list.push(obj[x]); }
}; return list;
},
send: async function(query, variables) {
Expand All @@ -24,11 +24,11 @@ module.exports = {
var response = await fetch('https://graphql.anilist.co', options);
var json = await response.json();

if (json.data === null) {
if (json.data === null) {
if (json.errors[0].status === 404) { return { data: null, status: 404, message: "Search item by that term is not found." } }
else { return { data: null, status: json.errors[0].status, message: json.errors[0].message } }
} else if (json.data !== null) {
if (json.data.Media) {
if (json.data.Media) {
var keys = ["characters", "staff", "relations", "trends"];
if (json.data.Media.studios) { keys.push("studios"); }
if (json.data.Media.airingSchedule) { keys.push("airingSchedule"); }
Expand All @@ -40,32 +40,35 @@ module.exports = {
json.data.Media.externalLinks = await this.edgeRemove(json.data.Media.externalLinks);
json.data.Media.trends = await this.edgeRemove(json.data.Media.trends);

if (json.data.Media.trailer) {
if (json.data.Media.trailer) {
switch (json.data.Media.trailer.site) {
case "youtube": json.data.Media.trailer = "https://www.youtube.com/watch?v=" + json.data.Media.trailer.id; break;
case "dailymotion": json.data.Media.trailer = "https://www.dailymotion.com/video/" + json.data.Media.trailer.id; break;
case undefined: json.data.Media.trailer = null; break;
default: json.data.Media.trailer = json.data.Media.trailer; break;
}
}
return json.data.Media;
} else if (json.data.Character) {

return json.data.Media;
} else if (json.data.Character) {
json.data.Character.media = await this.edgeRemove(json.data.Character.media.edges);
return json.data.Character;
} else if (json.data.Staff) {
return json.data.Character;
} else if (json.data.Staff) {
json.data.Staff.staffMedia = await this.edgeRemove(json.data.Staff.staffMedia.edges);
json.data.Staff.characters = await this.edgeRemove(json.data.Staff.characters.edges);
if (json.data.Staff.description.length < 1) { json.data.Staff.description = null; }
return json.data.Staff;
return json.data.Staff;
} else if (json.data.Page) { return json.data.Page; }
else if (json.data.Studio) {
else if (json.data.Studio) {
json.data.Studio.media = await this.edgeRemove(json.data.Studio.media.edges);
return json.data.Studio;
} else if (json.data.User) {
return json.data.Studio;
} else if (json.data.User) {
if (json.data.User.stats) { return json.data.User.stats; }
else { return json.data.User; }
} else if (json.data.MediaListCollection) {
if (json.data.MediaListCollection.lists) { return json.data.MediaListCollection.lists; }
else { return json.data.MediaListCollection; }
} else { return json.data; }
}
}
};
};
37 changes: 37 additions & 0 deletions lib/lists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const Fetch = require('./fetcher');

module.exports = {
variables: function(user, type) {
if (type === 'ANIME' || type === 'MANGA') {
if (typeof user === 'string') { return [{ name: user, type: type }, `query ($name: String, $type: MediaType) { MediaListCollection(userName: $name, type: $type) { `]; }
else if (typeof user === 'number') { return [{ id: user, type: type }, `query ($id: Int, $type: MediaType) { MediaListCollection(userId: $id,, type: $type) { `]; }
}
},
anime: function(user) {
if (!user) { throw new Error("AniList username or id is missing!"); }
var start = this.variables(user, 'ANIME');
var query = start[1] + `lists { name isCustomList isSplitCompletedList status entries { media {
id idMal title { romaji english native userPreferred }
type episodes description format status startDate { year month day } endDate { year month day }
season duration countryOfOrigin isLicensed source hashtag trailer { id site }
updatedAt coverImage { large medium } bannerImage genres synonyms averageScore meanScore
popularity trending tags { name isMediaSpoiler } relations { edges { id } } characters { edges { id } }
staff { edges { id } } studios { edges { id } } isFavourite isAdult nextAiringEpisode { id } airingSchedule { edges { id } }
trends { edges { node { averageScore popularity inProgress episode } } } externalLinks { url }
streamingEpisodes { title thumbnail url site } rankings { id } mediaListEntry { id }
reviews { edges { node { id summary } } } siteUrl autoCreateForumThread modNotes } } } } }`;
return Fetch.send(query, start[0]);
},
manga: function(user) {
if (!user) { throw new Error("AniList username or id is missing!"); }
var start = this.variables(user, 'MANGA');
var query = start[1] + `lists { name isCustomList isSplitCompletedList status entries { media {
id idMal title { romaji english native userPreferred }
type description format status startDate { year month day } endDate { year month day } volumes countryOfOrigin isLicensed updatedAt
coverImage { large medium } bannerImage genres synonyms averageScore meanScore siteUrl autoCreateForumThread modNotes
popularity trending tags { name isMediaSpoiler } relations { edges { id } } characters { edges { id } } staff { edges { id } }
isFavourite isAdult trends { edges { node { averageScore popularity inProgress episode } } }
externalLinks { url } rankings { id } mediaListEntry { id } reviews { edges { node { id } } } } } } } }`;
return Fetch.send(query, start[0]);
}
};
7 changes: 7 additions & 0 deletions tests/listsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const anilist = require('../index');
const AniList = new anilist();

AniList.lists.anime('salixor')
.then(data => { console.log(data.filter(l => l.status === 'CURRENT')); });
AniList.lists.manga('salixor')
.then(data => { console.log(data.filter(l => l.status === 'CURRENT')); });

0 comments on commit 1caf50b

Please sign in to comment.