anilist.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import requests
  2. from enum import Enum
  3. ANILIST_GRAPHQL_URL='https://graphql.anilist.co'
  4. class MediaType(Enum):
  5. ANIME="ANIME"
  6. MANGA="MANGA"
  7. def anilist_id_to_mal(media_id, media_type: MediaType):
  8. """ Convert an AniList media ID to a MyAnimeList ID and returns it """
  9. query = '''query($id: Int, $type: MediaType){
  10. Media(id: $id, type: $type) {
  11. idMal
  12. }
  13. }'''
  14. variables = {
  15. 'id': media_id,
  16. 'type': media_type.value
  17. }
  18. try:
  19. response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
  20. response.raise_for_status()
  21. return response.json()["data"]["Media"]["idMal"]
  22. except requests.HTTPError as e:
  23. #TODO Correct error response
  24. print('ERROR WRONG RESPONSE CODE')
  25. except Exception as e:
  26. #TODO Correct error response
  27. print('UNKNOWN Error when trying to get mal id :')
  28. print(e)
  29. print(anilist_id_to_mal(110277, MediaType.ANIME))
  30. # [x] Convertir AniList ID en MAL ID
  31. # [ ] Recuperer utilisateurs qui nous interessent
  32. # [ ] Recuperer activites de ces users
  33. # [ ] Traiter les donnees et les mettre en DB
  34. # [ ] Faire task pour fetch automatiquement
  35. # [ ] Rajouter requests dans la liste de dependances pip (Site de Penta)