Skip to content

Commit

Permalink
Authorization complete
Browse files Browse the repository at this point in the history
Added in an authorization method for users.
  • Loading branch information
AurelicButter committed Jul 15, 2018
1 parent 002e940 commit 32738d6
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 68 deletions.
45 changes: 32 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
# Anilist-Node
A simple, lightweight Node.js wrapper for the AniList API.

##Installing
```
npm install anilist-node
```
## Installing
Install with: `npm install anilist-node --dev` for the following:
1. Searches that need a user login
2. Uses such as editing a user's list
AND not having a client and token with AniList

Then, in order to begin using the package, just refer to it as with any other package.
```javascript
If that doesn't apply (ie: general searches, user lookups), you may install with `npm install anilist-node`

### Getting your token
You only need to generate a token once in order to use. To start, head to [Anilist's Developer Page](https://anilist.co/settings/developer) and click "Create New Client". Note the client id. Next, change the name of the settings_example.json to settings.json or copy paste into a new JSON file. As long as the end result leads to a settings.json with content like in the example JSON file. Add your email, password, and client id in the proper spots and save. In the same directory, run `node authorization.js`. A file titled "token.txt" should appear. That is your token. Store it securely. There is an example in the Example section on how to use the token.

Optional:
- You may delete your AniList credentials from the settings.json file (Email, password, and client id if you feel inclined)
- You may uninstall puppeteer via `npm uninstall puppeeteer` in the anilist-node directory
- You may delete authorization.js file if you don't need it.
- You may delete token.txt (Do store your token securely elsewhere before doing so)

## Example
### General lookup search (no login):
```javascript
const anilist = require('anilist-node');
const Anilist = new anilist();

Anilist.media.anime(21708).then(data => {
console.log(data);
});
```

##Example
```javascript
const list = require('anilist-node');
const anilist = new list();
### Lookup search (login):
```javascript
const settings = require('./settings.json');
const anilist = require('anilist-node');
const Anilist = new anilist(settings.token /* This being your token */);

anilist.media.anime(21708).then(data => {
Anilist.media.anime(21708).then(data => {
console.log(data);
});
```

##Documentation
Please see [documentation.md](Documentation.md) file for more information
## Documentation
Please see [documentation.md](documentation.md) file for more information
3 changes: 2 additions & 1 deletion fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fetch = require('node-fetch');
module.exports = class fetcher {
constructor () { };

async send(query, variables) {
async send(query, variables, auth) {
if (!query || !variables) { throw new Error("Query or variables are not given!"); }
var options = {
method: 'POST',
Expand All @@ -13,6 +13,7 @@ module.exports = class fetcher {
},
body: JSON.stringify({ query: query, variables: variables })
};
if (auth) { options.headers.Authorization = 'Bearer ' + auth; }
var response = await fetch('https://graphql.anilist.co', options);
var json = await response.json();

Expand Down
34 changes: 20 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
const User = require('./user');
const media = require('./media');
const people = require('./people');
const search = require('./search.json');
const fetcher = require('./fetcher');
const Fetch = new fetcher();

module.exports = class AniList {
constructor () {
this.user = new User();
this.media = new media();
this.people = new people();
constructor (accessKey) {
if (!accessKey) { var accessKey = null; }
this.user = new User(accessKey);
this.media = new media(accessKey);
this.people = new people(accessKey);
this.accessKey = accessKey;
};

studio(id) {
if (!id) { throw new Error("Studio id is not provided."); }
return Fetch.send(`query($id: Int) { Studio(id: $id) {
id name media { edges { id } }
siteUrl isFavourite } }`, { id: id });
return Fetch.send(`query($id: Int) { Studio(id: $id) { id name media { edges { id } } siteUrl isFavourite } }`, { id: id }, this.accessKey);
};

search(term, page, amount){
if (!term || !page || !amount) { throw new Error("Search term, page count, or amount per page was not provided!"); }
search(type, term, page, amount) {
if (!type) { throw new Error("Type of search not defined!"); }
else if (!term || !page || !amount) { throw new Error("Search term, page count, or amount per page was not provided!"); }
switch (type) {
case "anime": var query = search["anime"]; break;
case "manga": var query = search["manga"]; break;
case "char": 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.");
}
return Fetch.send(`query ($id: Int, $page: Int, $perPage: Int, $search: String) {
Page (page: $page, perPage: $perPage) {
pageInfo { total currentPage lastPage hasNextPage perPage }
media (id: $id, search: $search) { id title { romaji english native userPreferred } }
}
}`, { search: term, page: page, perPage: amount});
Page (page: $page, perPage: $perPage) { pageInfo { total currentPage lastPage hasNextPage perPage } ${query} } }`, { search: term, page: page, perPage: amount});
};
};
36 changes: 12 additions & 24 deletions media.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fetcher = require('./fetcher');
const Fetch = new fetcher();

module.exports = class media {
constructor() { };
constructor(accessKey) { this.accessKey = accessKey; };

anime(id) {
if (!id) { throw new Error("Anime id is not provided!"); }
Expand All @@ -11,47 +11,35 @@ module.exports = class media {
type episodes description
format status
startDate { year month day } endDate { year month day }
season duration
countryOfOrigin isLicensed
season duration countryOfOrigin isLicensed
source hashtag trailer { id }
updatedAt coverImage { large medium } bannerImage
genres synonyms averageScore meanScore
popularity trending tags { id }
relations { edges { id } }
characters { edges { id } }
staff { edges { id } }
studios { edges { id } }
isFavourite isAdult
nextAiringEpisode { id }
airingSchedule { edges { id } }
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 { id }
streamingEpisodes { title thumbnail url site }
rankings { id }
mediaListEntry { id }
rankings { id } mediaListEntry { id }
reviews { edges { node { id } } }
siteUrl autoCreateForumThread modNotes } }`, { id: id });
siteUrl autoCreateForumThread modNotes } }`, { id: id }, this.accessKey);
};

manga(id) {
if (!id) { throw new Error("Manga id is not provided!"); }
return Fetch.send(`query ($id: Int) { Media (id: $id, type: MANGA) { id idMal
title { romaji english native userPreferred }
type description format status
startDate { year month day } endDate { year month day }
type description format status startDate { year month day } endDate { year month day }
volumes countryOfOrigin isLicensed updatedAt
coverImage { large medium } bannerImage
genres synonyms averageScore meanScore
popularity trending tags { id }
relations { edges { id } }
characters { edges { id } }
staff { edges { id } }
popularity trending tags { id } relations { edges { id } }
characters { edges { id } } staff { edges { id } }
isFavourite isAdult
trends { edges { node { averageScore popularity inProgress episode } } }
externalLinks { id }
rankings { id }
mediaListEntry { id }
reviews { edges { node { id } } }
siteUrl autoCreateForumThread modNotes } }`, { id: id });
externalLinks { id } rankings { id } mediaListEntry { id }
reviews { edges { node { id } } } siteUrl autoCreateForumThread modNotes } }`, { id: id }, this.accessKey);
};
};
Loading

0 comments on commit 32738d6

Please sign in to comment.