From f2c7d70878bd9b0170b6728de1f33b6a7932d84a Mon Sep 17 00:00:00 2001 From: marios8543 Date: Sat, 25 Apr 2020 21:19:02 +0300 Subject: [PATCH] Upload files to 'anime_scripts' --- anime_scripts/seasons.py | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 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..014c39c --- /dev/null +++ b/anime_scripts/seasons.py @@ -0,0 +1,115 @@ +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 __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") + +items = [] + +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 + +show_id = search(argv[1]) +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"] + items.sort(key=lambda i: i.year) + season = items.index(base_show)+1 + +print("Season {}".format(season))