-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_anime_stats
executable file
·75 lines (57 loc) · 1.6 KB
/
get_anime_stats
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import pandas as pd
import requests
import time
from collections import Counter
from typing import List
def counter_str(counter: Counter) -> str:
return ", ".join([f"{n}: {q}" for n, q in counter.most_common()])
def get_animes() -> List[str]:
animes = []
with open("animes", "r") as f:
for line in f:
if not line.startswith("#"):
animes.append(line.strip())
return animes
def main():
base_url = "https://graphql.anilist.co"
animes = get_animes()
cols = ["Original Name", "Name", "Genres"]
c = Counter()
data = []
query = """
query ($name: String) {
Media (search: $name, type: ANIME) {
id
title {
english
romaji
}
genres
}
}
"""
max_str_length = 0
for anime in animes:
message = f"Getting info for: {anime}"
if len(message) > max_str_length:
max_str_length = len(message)
message = message.ljust(max_str_length)
print(message, end="\r")
variables = {"name": anime}
r = requests.post(base_url, json={"query": query, "variables": variables})
res = r.json()["data"]["Media"]
s = [
res["title"]["romaji"],
res["title"]["english"],
", ".join([x for x in res["genres"]]),
]
c.update(res["genres"])
data.append(s)
time.sleep(0.7)
print(" " * max_str_length, end="\r")
df = pd.DataFrame(columns=cols, data=data)
print(df)
print(counter_str(c))
if __name__ == "__main__":
main()