Files
dtam-api/tools/get-mal.py
2023-07-19 00:47:09 -05:00

53 lines
1.3 KiB
Python

import requests
import time
import json
import os
# Setup
CLIENT_ID = os.getenv('MAL_CLIENT_ID')
offset = 0
anime_list = []
# Get list of TV animes using MAL API
while offset < 5000:
# Can only get 500 at a time
url = f'https://api.myanimelist.net/v2/anime/ranking?ranking_type=tv&limit=500&offset={offset}'
resp = requests.get(url, headers={
'X-MAL-CLIENT-ID': CLIENT_ID
})
anime = resp.json()
# Add into our list
for node in anime['data']:
anime_list.append({'title': node['node']['title'], 'id': node['node']['id']})
# Start at the next 500
offset = offset + 500
# Let's not spam the MAL API
time.sleep(1)
# Get the list of movie animes using MAL API
offset = 0
while offset < 250:
# Limit to 250 entries at a time
url = f'https://api.myanimelist.net/v2/anime/ranking?ranking_type=movie&limit=250&offset={offset}'
resp = requests.get(url, headers={
'X-MAL-CLIENT-ID': CLIENT_ID
})
anime = resp.json()
# Add into our list
for node in anime['data']:
anime_list.append({'title': node['node']['title'], 'id': node['node']['id']})
# Start at the next 250
offset = offset + 250
# Let's not spam the MAL API
time.sleep(1)
# Write to disk
with open('mal.json', 'w') as f:
json.dump(anime_list, f, ensure_ascii=False, indent=4)