Utilities for the Anilist GraphQL API.
Project description
Nifty Anilist Tools
Overview
This is a simple utility library for interfacing with the Anilist GraphQL API. It provides useful tools like an Anilist client to make validated requests with query builder objects and handles authentication for you.
Using the Library
This library is available on PyPi.
To use this library, you will need to have the variables shown in .env.example in environment variables or your local .env file.
Any blank variables need to be present and will throw an error if are missing.
Features
GraphQL Requests
The Anilist API is GraphQL-based and provides a public schema. This library uses an Ariadne code-generated GraphQL client to make GraphQL requests to Anilist.
To make requests to Anilist, create an AnilistClient and use the anilist_request() function. Example:
client = AnilistClient()
async with client:
data = await client.anilist_request(my_query)
# OR
async with AnilistClient() as client:
data = await client.anilist_request(my_query)
One of the benefits of Ariadne is that queries (and manipulations, etc.) are made with objects instead of raw GraphQL strings, so you get a sort of schema pre-validation for your requests. These objects are also generated by Ariadne and are available from the nifty_anilist.client.custom_XXX files. Feel free to reference the Anilist schema and use the playground before writing these queries in code.
The following is a simple example for building such a query for getting a user's avatar:
from nifty_anilist.client.custom_queries import Query
from nifty_anilist.client.custom_fields import UserFields, UserAvatarFields
...
query = Query.user(name=username).fields(
UserFields.avatar().fields(
UserAvatarFields.large
)
)
response = await client.anilist_request(query)
avatar_url = response["User"]["avatar"]["large"]
print(f"Avatar URL: {avatar_url}")
The client takes an optional user ID to make requests for. If not provided, the client will try to use global user. You can also turn off authentication by setting use_auth to False.
These settings should not be changed for an existing client; you should make a new one if you need to make requests under a different user's credentials.
Anilist Auth
Token Storage
This library will store Anilist auth token(s) locally in one of two ways (customizable with the TOKEN_SAVING_METHOD environment variable):
- Using your system's keyring (kept "permanently"):
KEYRING - In-memory (lost when you restart your program):
IN_MEMORY
Using Auth
In order to get an auth token for the first time, you can use the sign_in_if_no_global() function from auth.py. After the first time, these details will be stored locally and added to your Anilist requests automatically.
Note: The sign-in function currently opens an instance of Google Chrome to the Anilist login page, from which your auth code will be automatically extracted. There will be more ways to do sign-in later.
There are two ways that auth headers can be added to your requests:
- Using a global user ID stored in the
.envfile: The ID is stored asANILIST_CURRENT_USER. This is the user ID that will be used to retrieve the token from the storage method(s) above. When making requests with the client'sanilist_request(), you can ignore the optionaluser_idparameter to use this approach. Example:
async def do_something():
query = # some query
async with AnilistClient() as client:
return await client.anilist_request(query)
if __name__ == "__main__":
sign_in_if_no_global()
data = asyncio.run(do_something())
print(data)
- Manually passing in a user ID to the
anilist_request()with theuser_idparameter will try to get that user's token from local storage and use it instead of the global one. Example:
async def do_something(user_id: str):
query = # some query
async with AnilistClient(user_id=user_id) as client:
return await client.anilist_request(query)
if __name__ == "__main__":
my_user_id = "12345"
data = asyncio.run(do_something(user_id=my_user_id))
print(data)
You can also choose to not use auth on requests with the use_auth parameter (default is True):
async def do_something():
query = # some query
async with AnilistClient(use_auth=False) as client:
return await client.anilist_request(query)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nifty_anilist-0.1.2.tar.gz.
File metadata
- Download URL: nifty_anilist-0.1.2.tar.gz
- Upload date:
- Size: 47.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5958ef76a38eaa56d0cb491d5563a5ae0c7bafe94a43862ff770f180968fc49d
|
|
| MD5 |
a299c3eb6fd12a46e6e8846a0a0fb844
|
|
| BLAKE2b-256 |
24207fb15b6d29f09698f0ace5fdfbe169c26c1c9424f3fd7b28254125d92f1c
|
File details
Details for the file nifty_anilist-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nifty_anilist-0.1.2-py3-none-any.whl
- Upload date:
- Size: 49.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bb600857ab9d486332b99609c4ec41acbc83011dab0dcb53e25212ad3794b65
|
|
| MD5 |
3b346dbfb231741fd1fc25e8ca2a6b5c
|
|
| BLAKE2b-256 |
773fc74cac898ed62c7dbdd7ecb42fd267c72bde5f8c1d3cda1c0d97224de4bd
|