-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanime.py
164 lines (150 loc) · 5.24 KB
/
anime.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import discord
from discord.ext import commands
from pybooru import Danbooru
from dbinteraction import *
import re
import requests
def doALrequest(q: str, v: dict):
r = requests.post('https://graphql.anilist.co',
json={'query': q, 'variables': v})
return r.json()
class anime(commands.Cog):
def __init__(self, bot):
self.client = Danbooru(
'danbooru', api_key=dbexec('danbooruKey', log=True))
self.bot = bot
@commands.command(aliases=['dnb'])
async def danbooru(self, ctx, *, tags1: str):
"""Gets an image from Danbooru, tags must be valid tags on the website"""
# replacement_table = {
# r' ': '_',
# r',': ' '
# }
# for regex, replace_with in replacement_table.items():
# tags1 = re.sub(regex, replace_with, tags1)
try:
# Create post object
class post:
def __init__(self, data):
self.id = data[0]['id']
self.score = data[0]['score']
self.rating = data[0]['rating']
self.img = data[0]['file_url']
self.uploadername = data[0]['uploader_name']
try:
while True:
counter = 1
print("Attempt {}".format(counter))
data = self.client.post_list(
limit=1, tags=tags1, random=True)
if (not data):
await ctx.send("Invalid tag*(s)*")
return
bnd = post(data)
if(bnd.rating == 's'):
break
except(Exception) as e:
print(e)
await ctx.send('An error occured')
return
em = discord.Embed(title="Search result", url=bnd.img)
em.add_field(name='Uploaded by',
value=bnd.uploadername, inline=True)
em.add_field(name="Score", value=bnd.score, inline=True)
em.set_image(url=bnd.img)
await ctx.send(embed=em)
except(Exception) as e:
print(e)
@commands.command()
async def charinfo(self, ctx, *, name: str):
"""Gets info about anime/manga character"""
content = doALrequest(q="""query ($s: String, $page: Int) {
Page(page: $page, perPage: 1) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
characters(search: $s) {
name{
first
last
native
alternative
}
image{
large
}
description(asHtml: false)
}
}
}""", v={"s": name})
if content['data']['Page']['characters']:
c = content['data']['Page']['characters'][0]
em = discord.Embed(
title=f"{c['name']['first']} {c['name']['last']if c['name']['last'] is not None else ''} ({c['name']['native']})")
em.add_field(name='Description', value=c['description'] if len(
c['description']) < 1024 else c['description'][0:1023], inline=True)
em.set_image(url=c['image']['large'])
await ctx.send(embed=em)
else:
await ctx.send("This character doesn't exist")
@commands.command(aliases=['ainfo', 'anime'])
async def animeinfo(self, ctx, *, name: str):
"""Gets info about an anime"""
content = doALrequest(q="""query ($s: String, $page: Int) {
Page(page: $page, perPage: 1) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
media(search: $s) {
title {
romaji
english
native
}
status
startDate {
year
month
day
}
endDate {
year
month
day
}
episodes
duration
source
genres
averageScore
popularity
coverImage {
large
}
description(asHtml: false)
}
}
}""", v={"s": name})
if content['data']['Page']['media']:
c = content['data']['Page']['media'][0]
em = discord.Embed(
title=f"{c['title']['english'] if c['title']['english'] else ''} | {c['title']['romaji']} ({c['title']['native']})")
em.add_field(name=c['status'], value='lol')
em.add_field(name="Episodes", value=f"{c['episodes']}", inline=True)
em.add_field(name="Duration", value=f"{c['duration']}min per ep", inline=True)
em.add_field(name='Description', value=c['description'] if len(
c['description']) < 1024 else c['description'][0:1023], inline=True)
em.set_image(url=c['coverImage']['large'])
await ctx.send(embed=em)
else:
await ctx.send("This character doesn't exist")
def setup(bot):
bot.add_cog(anime(bot))