-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyAnimeCommands.py
142 lines (127 loc) · 7.32 KB
/
MyAnimeCommands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#this cog uses the MyAnimeList and AniList APIs
import requests
import discord
from discord.ext import commands
import json
import random
class MyAnimeCommands(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("MAC.py is ready!")
#details on available genres:
@commands.command()
async def genres(self, ctx):
embed_var=discord.Embed(title="Available genres for !animerec {genre}:\n\n",description="\n\nAction, Adventure, Comedy, Drama, Ecchi, Fantasy, Horror, Mahou Shoujo, Mecha, Music, Mystery,"
"Psychological, Romance, Sci-Fi, Slice of Life, Sports, Supernatural, and Thriller.\n\nEnter the genre without {curly braces}.\nExample: !animerec Action",color=discord.Color.blurple())
await ctx.send(embed=embed_var)
#MAL reccomendation from the recc page
@commands.command()
async def recme(self, ctx):
url = "https://myanimelist.p.rapidapi.com/anime/recommendations/1"
headers = {
"X-RapidAPI-Key": "YOUR_KEY_HERE",
"X-RapidAPI-Host": "myanimelist.p.rapidapi.com"}
response = requests.get(url, headers=headers)
json_data = json.loads(response.text)
number = random.randint(0, 98)
title=json_data['recommendations'][number]['recommendation']['title']
pic_url=json_data['recommendations'][number]['recommendation']['picture_url']
desc=json_data['recommendations'][number]['description']
embed_var = discord.Embed(title="My Anime List users recommends:", description=f"{title}\n \n{desc}",
color=discord.Color.dark_teal())
embed_var.set_thumbnail(url=pic_url)
await ctx.send(embed=embed_var)
@commands.command()
async def animerec(self,ctx, user_genre):
if user_genre=="Mahou":
user_genre="Mahou Shoujo"
if user_genre == "Slice":
user_genre = "Slice of Life"
print(user_genre)
genre_list=["Action", "Adventure", "Comedy", "Drama", "Ecchi", "Fantasy",
"Horror", "Mahou Shoujo", "Mecha", "Music", "Mystery", "Psychological",
"Romance", "Sci-Fi", "Slice of Life", "Sports", "Supernatural", "Thriller"]
if user_genre in genre_list:
query = '''
query($page: Int, $perPage: Int, $genre: String) {
Page(page: $page, perPage: $perPage) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
media(genre: $genre) {
title {
romaji
english
}
}
}
}
'''
url = 'https://graphql.anilist.co'
number = random.randint(0, 1000)
try:
url = 'https://graphql.anilist.co'
number = random.randint(0, 1000)
print(number)
variables = {'genre': user_genre, 'page': number, 'perPage': 1}
response = requests.post(url, json={'query': query, 'variables': variables})
json_data = json.loads(response.text)
if json_data['data']['Page']['media'][0]['title']['english'] is None or type(
json_data['data']['Page']['media'][0]['title']['english']) != str:
print(json_data['data']['Page']['media'][0]['title']['english'])
assert False
elif (json_data['data']['Page']['media'][0]['title']['english']) == "None":
print("none pizza left beef")
rec = ("Reccomendation: \n\n"
"English: " + json_data['data']['Page']['media'][0]['title']['romaji'] + "\n\n"
"Romaji: " +
json_data['data']['Page']['media'][0]['title']['romaji'])
else:
print('here!')
print(json_data['data']['Page']['media'][0]['title']['english'])
rec = ("Reccomendation: \n\n"
"English: " + json_data['data']['Page']['media'][0]['title']['english'] + "\n\n"
"Romaji: " +
json_data['data']['Page']['media'][0]['title']['romaji'])
except:
number = random.randint(0, 25)
print(number)
variables = {'genre': user_genre, 'page': number, 'perPage': 1}
print("last, resort,,,,")
response = requests.post(url, json={'query': query, 'variables': variables})
json_data = json.loads(response.text)
print(json_data)
number = 0
while (type(json_data['data']['Page']['media'][0]['title']['english']) != str or
json_data['data']['Page']['media'][0]['title']['english'] == "None" or
json_data['data']['Page']['media'][0]['title']['english'] is None):
number += 1
response = requests.post(url, json={'query': query, 'variables': variables})
json_data = json.loads(response.text)
if number > 4:
break
if number > 4:
variables = {'genre': user_genre, 'page': 5, 'perPage': 1}
response = requests.post(url, json={'query': query, 'variables': variables})
json_data = json.loads(response.text)
rec= ("Reccomendation: \n\n"
"English: " + json_data['data']['Page']['media'][0]['title']['english'] + "\n\n"
"Romaji: " +
json_data['data']['Page']['media'][0]['title']['romaji'])
embed_var = discord.Embed(title=f"Genre: {user_genre}", description=f"\n{rec}",
color=discord.Color.random())
await ctx.send(embed=embed_var)
else:
embed_var=discord.Embed(title="Sorry! There was an error!",description="\nError is likely either too many requests or faulty requests.\n\n Please wait a moment before trying again. \n"
"\nDouble check your requested genre as well. Available genres include:\n"
"\nAction, Adventure, Comedy, Drama, Ecchi, Fantasy, Horror, Mahou Shoujo, Mecha, Music, Mystery, "
"Psychological, Romance, Sci-Fi, Slice of Life, Sports, Supernatural, and Thriller.",color=discord.Color.dark_orange())
await ctx.send(embed=embed_var)
async def setup(client):
await client.add_cog(MyAnimeCommands(client))