瀏覽代碼

Adding include/anilist.py

With anilist id converter to mal id
Lucas Villeneuve 5 年之前
父節點
當前提交
2da4fc82cb
共有 1 個文件被更改,包括 45 次插入0 次删除
  1. 45 0
      include/anilist.py

+ 45 - 0
include/anilist.py

@@ -0,0 +1,45 @@
+import requests
+from enum import Enum
+
+ANILIST_GRAPHQL_URL='https://graphql.anilist.co'
+
+class MediaType(Enum):
+    ANIME="ANIME"
+    MANGA="MANGA"
+
+def anilist_id_to_mal(media_id, media_type: MediaType):
+    """ Convert an AniList media ID to a MyAnimeList ID and returns it """
+
+    query = '''query($id: Int, $type: MediaType){
+        Media(id: $id, type: $type) {
+            idMal
+        }
+    }'''
+
+    variables = {
+        'id': media_id,
+        'type': media_type.value
+    }
+
+    try:
+        response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
+        response.raise_for_status()
+        return response.json()["data"]["Media"]["idMal"]
+    except requests.HTTPError as e:
+        #TODO Correct error response
+        print('ERROR WRONG RESPONSE CODE')
+    except Exception as e:
+        #TODO Correct error response
+        print('UNKNOWN Error when trying to get mal id :')
+        print(e)
+
+
+print(anilist_id_to_mal(110277, MediaType.ANIME))
+
+
+# [x] Convertir AniList ID en MAL ID
+# [ ] Recuperer utilisateurs qui nous interessent
+# [ ] Recuperer activites de ces users
+# [ ] Traiter les donnees et les mettre en DB
+# [ ] Faire task pour fetch automatiquement
+# [ ] Rajouter requests dans la liste de dependances pip (Site de Penta)