This repository was archived by the owner on Mar 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsnakes.py
More file actions
243 lines (195 loc) · 8.32 KB
/
Copy pathsnakes.py
File metadata and controls
243 lines (195 loc) · 8.32 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# coding=utf-8
import asyncio
import logging
import random
import re
import textwrap
from typing import Any, Dict
import aiohttp
import async_timeout
import discord
from discord.ext.commands import AutoShardedBot, Context, command, bot_has_permissions
from bot.converters import Snake
from bot.decorators import locked
from bot.utils import disambiguate
log = logging.getLogger(__name__)
URL = "https://en.wikipedia.org/w/api.php?"
class Snakes:
"""
Snake-related commands
"""
# I really hope this works
wiki_sects = re.compile(r'(?:=+ (.*?) =+)(.*?\n\n)', flags=re.DOTALL)
wiki_brief = re.compile(r'(.*?)(=+ (.*?) =+)', flags=re.DOTALL)
valid = ('gif', 'png', 'jpeg', 'jpg', 'webp')
def __init__(self, bot: AutoShardedBot):
self.bot = bot
async def fetch(self, session, url, params=None):
if params is None:
params = {}
async with async_timeout.timeout(10):
async with session.get(url, params=params) as response:
return await response.json()
async def get_snek(self, name: str) -> Dict[str, Any]:
"""
Go online and fetch information about a snake
The information includes the name of the snake, a picture of the snake, and various other pieces of info.
What information you get for the snake is up to you. Be creative!
If "python" is given as the snake name, you should return information about the programming language, but with
all the information you'd provide for a real snake. Try to have some fun with this!
:param name: The name of the snake to get information for - omit for a random snake
:return: A dict containing information on a snake
"""
snake_info = {}
async with aiohttp.ClientSession() as session:
params = {
'format': 'json',
'action': 'query',
'list': 'search',
'srsearch': name,
'utf8': '',
'srlimit': '1',
}
json = await self.fetch(session, URL, params=params)
# wikipedia does have a error page
try:
pageid = json["query"]["search"][0]["pageid"]
except KeyError:
# Wikipedia error page ID(?)
pageid = 41118
params = {
'format': 'json',
'action': 'query',
'prop': 'extracts|images|info',
'exlimit': 'max',
'explaintext': '',
'inprop': 'url',
'pageids': pageid
}
json = await self.fetch(session, URL, params=params)
# constructing dict - handle exceptions later
try:
snake_info["title"] = json["query"]["pages"][f"{pageid}"]["title"]
snake_info["extract"] = json["query"]["pages"][f"{pageid}"]["extract"]
snake_info["images"] = json["query"]["pages"][f"{pageid}"]["images"]
snake_info["fullurl"] = json["query"]["pages"][f"{pageid}"]["fullurl"]
snake_info["pageid"] = json["query"]["pages"][f"{pageid}"]["pageid"]
except KeyError:
snake_info["error"] = True
if snake_info["images"]:
i_url = 'https://commons.wikimedia.org/wiki/Special:FilePath/'
image_list = []
map_list = []
thumb_list = []
# Wikipedia has arbitrary images that are not snakes
banned = [
'Commons-logo.svg',
'Red%20Pencil%20Icon.png',
'distribution',
'The%20Death%20of%20Cleopatra%20arthur.jpg',
'Head%20of%20holotype',
'locator',
'Woma.png',
'-map.',
'.svg',
'ange.',
'Adder%20(PSF).png'
]
for image in snake_info["images"]:
# images come in the format of `File:filename.extension`
file, sep, filename = image["title"].partition(':')
filename = filename.replace(" ", "%20") # Wikipedia returns good data!
if not filename.startswith('Map'):
if any(ban in filename for ban in banned):
log.info("the image is banned")
else:
image_list.append(f"{i_url}{filename}")
thumb_list.append(f"{i_url}{filename}?width=100")
else:
map_list.append(f"{i_url}{filename}")
snake_info["image_list"] = image_list
snake_info["map_list"] = map_list
snake_info["thumb_list"] = thumb_list
return snake_info
@command(name="snakes.get()", aliases=["snakes.get"])
@bot_has_permissions(manage_messages=True)
@locked()
async def get(self, ctx: Context, name: Snake = None):
"""
Fetches information about a snake from Wikipedia.
:param ctx: Context object passed from discord.py
:param name: Optional, the name of the snake to get information for - omit for a random snake
"""
if name is None:
name = Snake.random()
data = await self.get_snek(name)
if data.get('error'):
return await ctx.send('Could not fetch data from Wikipedia.')
match = self.wiki_brief.match(data['extract'])
embed = discord.Embed(
title=data['title'],
description=match.group(1) if match else None,
url=data['fullurl'],
colour=0x59982F
)
fields = self.wiki_sects.findall(data['extract'])
excluded = ('see also', 'further reading', 'subspecies')
for title, body in fields:
if title.lower() in excluded:
continue
if not body.strip():
continue
# Only takes the first sentence
title, dot, _ = title.partition('.')
# There's probably a better way to do this
value = textwrap.shorten(body.strip(), width=200)
embed.add_field(name=title + dot, value=value + '\n\u200b', inline=False)
embed.set_footer(text='Powered by Wikipedia')
emoji = 'https://emojipedia-us.s3.amazonaws.com/thumbs/60/google/3/snake_1f40d.png'
image = next((url for url in data['image_list'] if url.endswith(self.valid)), emoji)
embed.set_thumbnail(url=image)
await ctx.send(embed=embed)
@command(hidden=True)
async def zen(self, ctx):
"""
>>> import this
Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles
for Python's design into 20 aphorisms, only 19 of which have been written down.
You must be connected to a voice channel in order to use this command.
"""
channel = ctx.author.voice.channel
if channel is None:
return
state = ctx.guild.voice_client
if state is not None:
# Already playing
return
voice = await channel.connect()
source = discord.FFmpegPCMAudio('zen.mp3')
voice.play(source, after=lambda *args: asyncio.run_coroutine_threadsafe(
voice.disconnect(), loop=ctx.bot.loop
))
@command(name="snakes.guess()", aliases=["snakes.guess", "identify"])
@locked()
async def guess(self, ctx):
"""
Snake identifying game!
"""
image = None
while image is None:
snakes = [Snake.random() for _ in range(5)]
answer = random.choice(snakes)
data = await self.get_snek(answer)
image = next((url for url in data['image_list'] if url.endswith(self.valid)), None)
embed = discord.Embed(
title='Which of the following is the snake in the image?',
colour=random.randint(1, 0xFFFFFF)
)
embed.set_image(url=image)
guess = await disambiguate(ctx, snakes, timeout=60, embed=embed)
if guess == answer:
return await ctx.send('You guessed correctly!')
await ctx.send(f'You guessed wrong. The correct answer was {answer}.')
def setup(bot):
bot.add_cog(Snakes(bot))
log.info("Cog loaded: Snakes")