summaryrefslogtreecommitdiff
path: root/pyscripts/anilist_crawl.py
blob: c529343f7e9355539749512835d808f638b7afe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python3

import requests
import json
import os

url = 'https://graphql.anilist.co'
variables = {}
home = os.path.expanduser("~")
subdir = "notes/misc"


# Here we define our query as a multi-line string
query = '''
query ($name: String, $type: MediaType) {
MediaListCollection (userName: $name, type: $type) {
  lists {
    status
    entries {
      media {
        title {
          romaji
        }
      }
    }
  }
  }
}
'''

variables['name'] = input('Enter username: ')

for MediaType in ['ANIME', 'MANGA']:

    variables['type'] = MediaType

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

    with open(os.path.join(home, subdir, "AniList_{}_{}.txt".format(variables['name'], variables['type'])), "w") as f:
        for sublist in data["data"]["MediaListCollection"]["lists"]:
            print('----- {} -----'.format(sublist["status"]), end="\n"*2, file=f)
            for entry in sublist["entries"]:
                print(entry["media"]["title"]["romaji"], file=f)
            print("\n", file=f)