Commit dce8977c5f
Unsigned
Layout: unified · split
anilist.py +74 −29
| @@ -1,37 +1,82 @@ | ||
| 1 | 1 | import requests |
| 2 | import json | |
| 2 | 3 | |
| 4 | ||
| 5 | def get_user_data(): | |
| 3 | 6 | # Replace with your actual access token and user ID |
| 4 | access_token = '' | |
| 5 | user_id = '6719526' | |
| 6 | ||
| 7 | # GraphQL query | |
| 8 | query = """ | |
| 9 | { | |
| 10 | MediaListCollection(userId: %s, type: ANIME) { | |
| 11 | lists { | |
| 12 | entries { | |
| 13 | media { | |
| 14 | id | |
| 15 | title { | |
| 16 | romaji | |
| 17 | english | |
| 7 | access_token = '' | |
| 8 | user_id = '6719526' | |
| 9 | ||
| 10 | # GraphQL query | |
| 11 | query = """ | |
| 12 | { | |
| 13 | MediaListCollection(userId: %s, type: ANIME) { | |
| 14 | lists { | |
| 15 | entries { | |
| 16 | media { | |
| 17 | id | |
| 18 | title { | |
| 19 | romaji | |
| 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 | } | |
| 27 | """ % user_id | |
| 28 | ||
| 29 | # Send the request | |
| 30 | response = requests.post( | |
| 31 | 'https://graphql.anilist.co', | |
| 32 | json={'query': query}, | |
| 33 | headers={'Authorization': f'Bearer {access_token}'} | |
| 34 | ) | |
| 35 | ||
| 36 | # Print the response | |
| 37 | print(response.json()) | |
| 30 | """ % user_id | |
| 31 | ||
| 32 | # Send the request | |
| 33 | response = requests.post( | |
| 34 | 'https://graphql.anilist.co', | |
| 35 | json={'query': query}, | |
| 36 | headers={'Authorization': f'Bearer {access_token}'} | |
| 37 | ) | |
| 38 | ||
| 39 | # Print the response | |
| 40 | return 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.") | |