From 91a0f23e3286667a2c9a287e6688bfe6a6c609e5 Mon Sep 17 00:00:00 2001 From: tzatzikiweeb Date: Wed, 29 Apr 2020 01:24:44 +0300 Subject: [PATCH] Add 'anime_scripts/seasons.py' --- anime_scripts/seasons.py | 128 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 anime_scripts/seasons.py diff --git a/anime_scripts/seasons.py b/anime_scripts/seasons.py new file mode 100644 index 0000000..8356b2e --- /dev/null +++ b/anime_scripts/seasons.py @@ -0,0 +1,128 @@ +from requests import post +from sys import argv + +BASE_URL = "https://graphql.anilist.co" + +class SortableAnime: + def __init__(self, id, year, reltype, title, frmt): + self.id = id + self.year = year if year else 9999 + self.type = reltype + self.title = title + self.frmt = frmt + + def dict(self): + return { + "id": self.id, + "year": self.year, + "type": self.type, + "title": self.title, + "format": self.frmt + } + + def __eq__(self, other): + return self.id == other.id + + def __hash__(self): + return self.id + + def __str__(self): + return str(self.title) + + +def search(query): + response = post(BASE_URL, json={ + 'query': """ + query ($q: String) { + Media (search: $q) { + id + } + } + """, + 'variables': {"q": query} + }) + if response.ok: + return response.json()["data"]["Media"]["id"] + raise ValueError("Show does not exist") + +def get_show(id): + response = post(BASE_URL, json={ + 'query': """ + query ($id: Int) { + Media (id: $id) { + id + title { + english + romaji + } + startDate { + year + } + format + relations { + nodes { + id + format + startDate { + year + } + title { + english + romaji + } + } + edges{ + relationType + } + } + } + } + """, + 'variables': {"id": id} + }) + if response.ok: + return response.json() + raise ValueError("Bad show") + +def get_base_show(res): + base = res["data"]["Media"] + return SortableAnime(base["id"], base["startDate"]["year"], "BASE", base["title"], base["format"]) + +def process_shows(res): + ls = [] + ls.append(get_base_show(res)) + for i,v in enumerate(res["data"]["Media"]["relations"]["nodes"]): + ls.append(SortableAnime(v["id"], v["startDate"]["year"], res["data"]["Media"]["relations"]["edges"][i]["relationType"], v["title"], v["format"])) + pass + return ls + +def main(query): + items = [] + show_id = search(query) + res = get_show(show_id) + items.extend(process_shows(res)) + base_show = get_base_show(res) + + if "PREQUEL" not in [i.type for i in items]: + season = 1 + else: + ignore = [] + while True: + f = False + for i in items: + if i.type == "PREQUEL" and i not in ignore: + fi = [i for i in process_shows(get_show(i.id)) if i not in items] + items.extend(fi) + ignore.append(i) + f = True + if not f: + break + items = [i for i in items if i.frmt == "TV" and (i.type == "PREQUEL" or i.type == "SEQUEL" or i.type == "BASE")] + items.sort(key=lambda i: i.year) + season = items.index(base_show)+1 + return season, base_show, items + +if __name__ == "__main__": + season, show, items = main(argv[1]) + base_title = items[0].title + print("{}|{}|{}".format(base_title["romaji"], base_title["english"], season)) \ No newline at end of file