anilist.py
147 lines · 3967 bytes
1import requests
2import json
3
4
5import requests
6
7def get_anilist_user_id(token):
8 url = "https://graphql.anilist.co"
9 query = '''
10 query {
11 Viewer {
12 id
13 name
14 }
15 }
16 '''
17
18 headers = {
19 "Authorization": f"Bearer {token}",
20 "Content-Type": "application/json",
21 "Accept": "application/json"
22 }
23
24 response = requests.post(url, json={"query": query}, headers=headers)
25
26 if response.status_code == 200:
27 data = response.json()
28 user_id = data['data']['Viewer']['id']
29 user_name = data['data']['Viewer']['name']
30 return user_id, user_name
31 else:
32 raise Exception(f"Error: {response.status_code}, {response.text}")
33
34# Replace 'your_token' with your actual AniList token
35
36
37def get_user_data(access_token, user_id):
38# Replace with your actual access token and user ID
39 # access_token =
40 # user_id =
41
42 # GraphQL query
43 query = """
44 {
45 MediaListCollection(userId: %s, type: ANIME) {
46 lists {
47 entries {
48 media {
49 id
50 episodes
51 duration
52 title {
53 romaji
54 english
55 }
56 }
57 status
58 score
59 progress
60 }
61 }
62 }
63 }
64 """ % user_id
65
66 # Send the request
67 response = requests.post(
68 'https://graphql.anilist.co',
69 json={'query': query},
70 headers={'Authorization': f'Bearer {access_token}'}
71 )
72
73
74 # Print the response
75 return response.json()
76
77def load_json_file(file_path):
78 with open(file_path, 'r', encoding='utf-8') as file:
79 return json.load(file)
80
81def search_anime_by_title(json_data, search_title):
82 results = []
83 if search_title=="1P":
84 search_title = "ONE PIECE"
85 for list_item in json_data['data']['MediaListCollection']['lists']:
86 for entry in list_item['entries']:
87 media = entry['media']
88 romaji_title = media['title']['romaji']
89 english_title = media['title']['english']
90 episodes = media['episodes']
91 duration = media['duration']
92 try:
93 if search_title.lower() in romaji_title.lower() or search_title.lower() in english_title.lower():
94 results.append({
95 'id': media['id'],
96 'progress': entry['progress'],
97 'romaji_title': romaji_title,
98 'english_title': english_title,
99 'episodes': episodes,
100 'duration': duration,
101 })
102 except:
103 pass
104
105 return results
106
107def main():
108 # Load the JSON file
109 json_file_path = 'response.json' # Replace with the path to your JSON file
110 json_data = load_json_file(json_file_path)
111
112 # Ask for the title to search
113 # search_title = input("Enter the anime title to search (Romaji or English): ")
114
115 search_title = "1P"
116
117 # Search for the anime
118 results = search_anime_by_title(json_data, search_title)
119
120 # Print the results
121 if results:
122 for result in results:
123 print(f"Anime ID: {result['id']}, Progress: {result['progress']}, "
124 f"Romaji Title: {result['romaji_title']}, English Title: {result['english_title']}")
125 else:
126 print("Anime not found.")
127
128if __name__ == "__main__":
129 main()
130
131
132# if __name__ == '__main__':
133# # Load JSON data from a file (replace 'your_file.json' with the actual file name)
134# with open('response.json', 'r') as file:
135# data = json.load(file)
136
137# # Define the title to search for
138# search_title = 'Fullmetal Alchemist: Brotherhood' # Replace with the actual title you're searching for
139
140# # Search for the anime and get its ID
141# anime_id = search_anime_by_title(data, search_title)
142
143# # Print the result
144# if anime_id:
145# print(f"Anime ID: {anime_id}")
146# else:
147# print("Anime not found.")