1
0

anilist.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import requests
  2. import time
  3. from enum import Enum
  4. ANILIST_GRAPHQL_URL = 'https://graphql.anilist.co'
  5. DEBUG_USERS = [
  6. 102213, # lululekiddo
  7. 151824 # Pentou
  8. ]
  9. DEBUG_USERS_NAME = [
  10. "lululekiddo",
  11. "Pentou"
  12. ]
  13. class MediaType(Enum):
  14. ANIME="ANIME"
  15. MANGA="MANGA"
  16. def get_mal_id_from_anilist_id(anilist_media_id, media_type: MediaType):
  17. """ Converts an AniList media ID to a MyAnimeList ID and returns it """
  18. query = '''query($id: Int, $type: MediaType){
  19. Media(id: $id, type: $type) {
  20. idMal
  21. }
  22. }'''
  23. variables = {
  24. 'id': anilist_media_id,
  25. 'type': media_type.value
  26. }
  27. try:
  28. response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
  29. response.raise_for_status()
  30. return response.json()["data"]["Media"]["idMal"]
  31. except requests.HTTPError as e:
  32. #TODO Correct error response
  33. print('ERROR WRONG RESPONSE CODE')
  34. except Exception as e:
  35. #TODO Correct error response
  36. print('UNKNOWN Error when trying to get mal id :')
  37. print(e)
  38. return None
  39. def get_anilist_userId_from_name(user_name : str):
  40. """ Searches an AniList user by its name and returns its ID """
  41. query = '''query($userName: String){
  42. User(name: $userName) {
  43. id
  44. }
  45. }'''
  46. variables = {
  47. 'userName': user_name
  48. }
  49. try:
  50. response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
  51. response.raise_for_status()
  52. return response.json()["data"]["User"]["id"]
  53. except requests.HTTPError as e:
  54. #TODO Correct error response
  55. print('ERROR WRONG RESPONSE CODE')
  56. except Exception as e:
  57. #TODO Correct error response
  58. print('UNKNOWN Error when trying to get user id :')
  59. print(e)
  60. return None
  61. def get_latest_users_activities(users_id, page, perPage = 5):
  62. """ Get latest users' activities """
  63. query = '''query ($userIds: [Int], $page: Int, $perPage: Int) {
  64. Page (page: $page, perPage: $perPage) {
  65. activities (userId_in: $userIds, sort: ID_DESC) {
  66. __typename
  67. ... on ListActivity {
  68. id
  69. userId
  70. type
  71. status
  72. progress
  73. isLocked
  74. createdAt
  75. media {
  76. id
  77. title {
  78. romaji
  79. english
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }'''
  86. variables = {
  87. "userIds": DEBUG_USERS,
  88. "perPage": perPage,
  89. "page": page
  90. }
  91. try:
  92. response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
  93. response.raise_for_status()
  94. return response.json()["data"]["Page"]["activities"]
  95. except requests.HTTPError as e:
  96. #TODO Correct error response
  97. print('ERROR WRONG RESPONSE CODE')
  98. except Exception as e:
  99. #TODO Correct error response
  100. print('UNKNOWN Error when trying to get the users\' activities :')
  101. print(e)
  102. return None
  103. def get_latest_activity(users_id):
  104. """ Get the latest users' activity """
  105. # TODO Will fail if last activity is not a ListActivity
  106. query = '''query ($userIds: [Int]) {
  107. Activity(userId_in: $userIds, sort: ID_DESC) {
  108. __typename
  109. ... on ListActivity {
  110. id
  111. userId
  112. createdAt
  113. }
  114. }
  115. }'''
  116. variables = {
  117. "userIds": users_id
  118. }
  119. try:
  120. response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
  121. response.raise_for_status()
  122. return response.json()["data"]["Activity"]
  123. except requests.HTTPError as e:
  124. #TODO Correct error response
  125. print('ERROR WRONG RESPONSE CODE')
  126. except Exception as e:
  127. #TODO Correct error response
  128. print('UNKNOWN Error when trying to get the latest activity :')
  129. print(e)
  130. return None
  131. def process_new_activities(last_activity_date):
  132. """ Fetch and process all newest activities """
  133. continue_fetching = True
  134. page_number = 1
  135. while continue_fetching:
  136. # Get activities
  137. activities = get_latest_users_activities(DEBUG_USERS, page_number)
  138. # Processing them
  139. for activity in activities:
  140. print(activity) # TODO Remove, DEBUG
  141. # If the activity is older than the last_activity_date, we processed all the newest activities
  142. if activity["createdAt"] < last_activity_date:
  143. continue_fetching = False
  144. break
  145. # Process activity
  146. # TODO Insert in DB
  147. # Load next activities page
  148. # TODO How can I avoid duplicate if insertion in between? With storing ids?
  149. if continue_fetching:
  150. print('Fetching next page') # TODO Remove, Debug
  151. page_number += 1
  152. time.sleep(1)
  153. def check_new_activities():
  154. """ Check if there is new activities and process them """
  155. last_activity_date = 1608340203 # TODO SELECT DATE IN DB
  156. # Get latest activity on AniList
  157. latest_activity = get_latest_activity(DEBUG_USERS)
  158. if latest_activity is not None:
  159. # If the latest activity is more recent than the last we stored
  160. if last_activity_date < latest_activity["createdAt"]:
  161. print("Latest activity is more recent")
  162. process_new_activities(last_activity_date)
  163. # [x] Convertir AniList ID en MAL ID
  164. # [ ] Recuperer utilisateurs qui nous interessent
  165. # [X] Recuperer activites de ces users
  166. # [ ] Traiter les donnees et les mettre en DB
  167. # [ ] Faire task pour fetch automatiquement
  168. # [ ] Rajouter requests dans la liste de dependances pip (Site de Penta)