Commit dce8977c5f
Unsigned
Layout: unified · split
anilist.py +74 −29
| @@ -1,37 +1,82 @@ | |||
| 1 | import requests | 1 | import requests |
| 2 | import json | ||
| 2 | 3 | ||
| 4 | |||
| 5 | def get_user_data(): | ||
| 3 | # Replace with your actual access token and user ID | 6 | # Replace with your actual access token and user ID |
| 4 | access_token = '' | 7 | access_token = '' |
| 5 | user_id = '6719526' | 8 | user_id = '6719526' |
| 6 | 9 | ||
| 7 | # GraphQL query | 10 | # GraphQL query |
| 8 | query = """ | 11 | query = """ |
| 9 | { | 12 | { |
| 10 | MediaListCollection(userId: %s, type: ANIME) { | 13 | MediaListCollection(userId: %s, type: ANIME) { |
| 11 | lists { | 14 | lists { |
| 12 | entries { | 15 | entries { |
| 13 | media { | 16 | media { |
| 14 | id | 17 | id |
| 15 | title { | 18 | title { |
| 16 | romaji | 19 | romaji |
| 17 | english | 20 | english |
| 21 | } | ||
| 18 | } | 22 | } |
| 23 | status | ||
| 24 | score | ||
| 25 | progress | ||
| 19 | } | 26 | } |
| 20 | status | ||
| 21 | score | ||
| 22 | progress | ||
| 23 | } | 27 | } |
| 24 | } | 28 | } |
| 25 | } | 29 | } |
| 26 | } | 30 | """ % user_id |
| 27 | """ % user_id | 31 | |
| 28 | 32 | # Send the request | |
| 29 | # Send the request | 33 | response = requests.post( |
| 30 | response = requests.post( | 34 | 'https://graphql.anilist.co', |
| 31 | 'https://graphql.anilist.co', | 35 | json={'query': query}, |
| 32 | json={'query': query}, | 36 | headers={'Authorization': f'Bearer {access_token}'} |
| 33 | headers={'Authorization': f'Bearer {access_token}'} | 37 | ) |
| 34 | ) | 38 | |
| 35 | 39 | # Print the response | |
| 36 | # Print the response | 40 | return response.json() |
| 37 | print(response.json()) | 41 | |
| 42 | |||
| 43 | def search_anime_by_title(json_data, search_title): | ||
| 44 | """ | ||
| 45 | Search for an anime by its title in the giv en JSON data and return its ID. | ||
| 46 | |||
| 47 | :param json_data: The JSON data as a dictionary. | ||
| 48 | :param search_title: The title of the anime to search for. | ||
| 49 | :return: The ID of the anime if found, otherwise None. | ||
| 50 | """ | ||
| 51 | # Navigate to the list of entries | ||
| 52 | anime_list = json_data['data']['MediaListCollection']['lists'][0]['entries'] | ||
| 53 | |||
| 54 | # Search for the anime with the given title | ||
| 55 | for entry in anime_list: | ||
| 56 | media = entry['media'] | ||
| 57 | title_romaji = media['title']['romaji'] | ||
| 58 | title_english = media['title']['english'] | ||
| 59 | |||
| 60 | # Check if either title matches the search title | ||
| 61 | if search_title.lower() in title_romaji.lower() or search_title.lower() in title_english.lower(): | ||
| 62 | return media['id'] | ||
| 63 | |||
| 64 | # If not found, return None | ||
| 65 | return None | ||
| 66 | |||
| 67 | if __name__ == '__main__': | ||
| 68 | # Load JSON data from a file (replace 'your_file.json' with the actual file name) | ||
| 69 | with open('response.json', 'r') as file: | ||
| 70 | data = json.load(file) | ||
| 71 | |||
| 72 | # Define the title to search for | ||
| 73 | search_title = 'The Quintessential Quintuplets 2' # Replace with the actual title you're searching for | ||
| 74 | |||
| 75 | # Search for the anime and get its ID | ||
| 76 | anime_id = search_anime_by_title(data, search_title) | ||
| 77 | |||
| 78 | # Print the result | ||
| 79 | if anime_id: | ||
| 80 | print(f"Anime ID: {anime_id}") | ||
| 81 | else: | ||
| 82 | print("Anime not found.") | ||