Retrieve Anime data using AniList API

Hello friends, today we are going to retrieve anime show details by making an API request to AniList using Python. API stands for Application Programming Interface, it acts as a software median between two applications. It simplifies programming by hiding the underlying procedure and only exposing the contents useful to the developer. Let’s learn more on the Python Request module before diving into making an API request to AniList to retrieve anime show details from it.

Installing Requests Module

To install the request module in your system open the command prompt or terminal and run the following code.

pip install requests

Once you’ve installed the requests module, import the module to your code.

import requests

Making a Request

There are two different ways to make a request, GET method or POST method. Here we are going to understand the working of the POST method. To use the ‘POST’ method we are going to use the requests.post() function. The function takes a number of parameters like : url, json, auth, data, etc. It returns the response object.

POST Method

Syntax :

response = requests.post(url, json={key: value})

Code :

response = requests.post(url, json={'query': query, 'variables': variables})

Here is an example where I’ve made an API call to the AniList API and retrieved information about the different anime shows. I’ve used the post function to make a request and retrieved its title in different languages: romaji, english, native. Its start date, end date in years, months and days. It also tells you about the number of episodes, average score, mean score and its popularity amongst the people. It provides one with cover image links for people to download from.

Retrieve Anime Data from AniList

import requests
import json

# Here we define our query as a multi-line string
query = '''
query ($id: Int) { # Define which variables will be used in the query (id)
  Media (id: $id, type: ANIME) { # Insert our variables into the query arguments (id) (type: ANIME is hard-coded in the query)
    id
    title {
      romaji
      english
      native
    }
    startDate {
      year
      month
      day
    }
    endDate {
      year
      month
      day
    }
    episodes
    coverImage {
      extraLarge
      large
      medium
      color
    }
    averageScore
    popularity
    meanScore
    characters {
      edges {
        id
      }
    }
  }
}
'''

# Define our query variables and values that will be used in the query request
variables = {
    'id': 101922
}

url = 'https://graphql.anilist.co'

# Make the HTTP Api request
response = requests.post(url, json={'query': query, 'variables': variables})
json_response = response.json()

json_formatted_str = json.dumps(json_response, indent=2)

print(json_formatted_str)

I’ve imported json module to use the json.dump() function which takes a Python object as input and returns a json string. It also takes a number of arguments where indent is one of them that I’ve used in my code. Indent helps to pretty print the retrieved json string.

Output :

{
  "data": {
    "Media": {
      "id": 101922,
      "title": {
        "romaji": "Kimetsu no Yaiba",
        "english": "Demon Slayer: Kimetsu no Yaiba",
        "native": "\u9b3c\u6ec5\u306e\u5203"
      },
      "startDate": {
        "year": 2019,
        "month": 4,
        "day": 6
      },
      "endDate": {
        "year": 2019,
        "month": 9,
        "day": 28
      },
      "episodes": 26,
      "coverImage": {
        "extraLarge": "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx101922-PEn1CTc93blC.jpg",
        "large": "https://s4.anilist.co/file/anilistcdn/media/anime/cover/medium/bx101922-PEn1CTc93blC.jpg",
        "medium": "https://s4.anilist.co/file/anilistcdn/media/anime/cover/small/bx101922-PEn1CTc93blC.jpg",
        "color": "#4B4A95"
      },
      "averageScore": 84,
      "popularity": 661640,
      "meanScore": 84
    }
  }
}

This is how we can work we can retrieve anime details by making an API request using Python.

Leave a Reply

Your email address will not be published. Required fields are marked *