This repository has been archived by the owner on Dec 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pokemon.py
276 lines (230 loc) · 11.2 KB
/
pokemon.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'''
pokemon.py -- a hangoutsbot plugin for retrieving information about pokemon, given a name
This uses the pokeapi.co API to retrieve the information.
Because Pokéapi limits requests to 300 requests per resource, we store cached data for each Pokémon for 5 days - this should be sufficient for up to 1500 Pokémon overall. Currently there are approximately 811 Pokémon accessible through the Pokéapi so this should be more than enough. Of course, the API limit is per resource so theoretically no caching is required, but let's be API-friendly.
'''
import hangups, plugins, asyncio, logging, datetime
import urllib.request
import json
import aiohttp, os, io
logger = logging.getLogger(__name__)
pokedex_config = {}
def _initialise(bot):
plugins.register_admin_command(["clearpokedex"])
plugins.register_user_command(["pokedex", "pokemon"])
global pokedex_config
try:
pokedex_config = bot.get_config_option('pokedex') or {}
except:
logger.error('Unable to load pokedex configuration - default configuration will apply.')
try:
if not pokedex_config['info']:
pokedex_config['info'] = ['types']
except KeyError:
pokedex_config['info'] = ['types']
def check_config(bot, conf):
try:
if not bot.memory.get_by_path(['pokedex','_config','cache']) == conf['info']:
bot.memory.set_by_path(['pokedex'],{'_config':{'cache': conf['info']}})
except:
bot.memory.set_by_path(['pokedex'],{'_config':{'cache': conf['info']}})
def comparetypes(data1, data2):
weak1 = [x['name'] for x in data1['damage_relations']['double_damage_from']]
weak2 = [x['name'] for x in data2['damage_relations']['double_damage_from']]
resist1 = [x['name'] for x in data1['damage_relations']['half_damage_from']]
resist2 = [x['name'] for x in data2['damage_relations']['half_damage_from']]
immune1 = [x['name'] for x in data1['damage_relations']['no_damage_from']]
immune2 = [x['name'] for x in data2['damage_relations']['no_damage_from']]
immune = set(immune1).union(immune2)
four = set(weak1).intersection(weak2).difference(immune)
quarter = set(resist1).intersection(resist2).difference(immune)
two = set(weak1).symmetric_difference(weak2).difference(set(resist1).symmetric_difference(resist2)).difference(immune)
half = ((set(resist1).symmetric_difference(resist2)).difference(set(weak1).symmetric_difference(weak2))).difference(immune)
matchup = {'4x':four,'2x':two,'1/2':half,'1/4':quarter,'immune':immune}
return matchup
@asyncio.coroutine
def clearpokedex(bot, event):
'''Clear the cached pokedex - useful when the data seems outdated.'''
bot.memory.set_by_path(["pokedex"], {})
yield from bot.coro_send_message(event.conv, "Pokedex cache cleared")
def gettypefromcache(bot, pkmntype):
if not bot.memory.exists(["pokedex", "_pokemontypes"]):
bot.memory.set_by_path(["pokedex", "_pokemontypes"], {})
return None
else:
if not bot.memory.exists(["pokedex", "_pokemontypes", pkmntype]):
return None
elif bot.get_by_path(["pokedex", "_pokemontypes", pkmntype, "expires"]) < str(datetime.datetime.now()):
logger.info("Cached data for {} type expired.".format(pkmntype))
return None
else:
return bot.get_by_path(["pokedex","_pokemontypes",pkmntype])
@asyncio.coroutine
def cachepkmntype(bot, pkmntypedata):
if not bot.memory.exists(["pokedex", "_pokemontypes"]):
bot.memory.set_by_path(["pokedex", "_pokemontypes"], {})
bot.memory.set_by_path(["pokedex", "_pokemontypes", pkmntypedata["name"]], {"name":pkmntypedata["name"],"damage_relations":pkmntypedata['damage_relations'],'expires':str(datetime.datetime.now() + datetime.timedelta(days=5))})
logger.info("Writing {} type data into cache".format(pkmntypedata['name']))
def getpkmntype(bot, pkmntype):
url = "http://pokeapi.co/api/v2/type/{}".format(pkmntype.lower())
request = urllib.request.Request(url, headers = {"User-agent":"Mozilla/5.0"})
try:
data = json.loads(urllib.request.urlopen(request).read().decode("utf-8"))
except:
return None
return data
def getfromcache(bot, pokemonname):
if not bot.memory.exists(["pokedex"]):
bot.memory.set_by_path(["pokedex"], {})
if not bot.memory.exists(["pokedex", pokemonname]):
logger.info("{} not in cache.".format(pokemonname))
return None
else:
try:
if bot.memory.get_by_path(["pokedex", pokemonname, "expires"]) < str(datetime.datetime.now()):
logger.info("Cached data for {} expired.".format(pokemonname))
return None
except:
logger.info("No cache timestamp for {} available.".format(pokemonname))
return None
return bot.memory.get_by_path(["pokedex", pokemonname])
return
def cachepkmn(bot, pkmndata, name):
if not bot.memory.exists(["pokedex"]):
bot.memory.set_by_path(["pokedex"], {})
bot.memory.set_by_path(["pokedex", name], {"id":pkmndata["id"],"name":pkmndata["name"],"types":pkmndata["types"],"abilities":pkmndata['abilities'],"expires": str(datetime.datetime.now() + datetime.timedelta(days=5))})
logger.info("Writing {} into cache".format(name))
return
def gettype(bot, pkmntype, logger):
cache = gettypefromcache(bot, pkmntype)
if cache:
return cache
else:
typedata = getpkmntype(bot, pkmntype)
if typedata:
cachepkmntype(bot, typedata)
return typedata
return
@asyncio.coroutine
def pokedex(bot, event, pokemon):
pokemon = pokemon.strip('#')
'''Returns the number, types, weaknesses and image of a pokemon'''
if pokedex_config:
check_config(bot, pokedex_config)
try:
if not pokedex_config['info']:
pokedex_config['info'] = ['types']
except KeyError:
pokedex_config['info'] = ['types']
url = "http://pokeapi.co/api/v2/pokemon/{}/".format(pokemon.lower())
request = urllib.request.Request(url, headers = {"User-agent":"Mozilla/5.0"})
cache = getfromcache(bot, pokemon.lower())
if cache:
logger.info("Found {} in cache".format(pokemon.lower()))
data = cache
pkmn = "<b><a href='http://pokemondb.net/pokedex/{}'>{}</a></b> [#{}]".format(pokemon.lower(),data['name'].capitalize(),data["id"])
else:
logger.info("{} not found in cache OR cache expired, getting from pokeapi".format(pokemon.capitalize()))
try:
data = json.loads(urllib.request.urlopen(request).read().decode("utf-8"))
except urllib.error.URLError as e:
yield from bot.coro_send_message(event.conv, "{}: Error: {}".format(event.user.full_name, json.loads(e.read().decode("utf8","ignore"))['detail']))
return
cachepkmn(bot, data, pokemon.lower())
pkmn = "<b><a href='http://pokemondb.net/pokedex/{}'>{}</a></b> (#{})".format(pokemon.lower(),data['name'].capitalize(),data["id"])
if 'types' in pokedex_config['info']:
type1 = gettype(bot, data['types'][0]['type']['name'], logger)
pkmn = pkmn + "<br><b>Type</b>: <a href='http://pokemondb.net/type/{}'>{}</a>".format(data['types'][0]["type"]["name"],data['types'][0]["type"]["name"].capitalize())
if len(data['types']) > 1 :
type2 = gettype(bot, data['types'][1]['type']['name'], logger)
pkmn = pkmn + " / <a href='http://pokemondb.net/type/{}'>{}</a>".format(data['types'][1]["type"]["name"],data['types'][1]["type"]["name"].capitalize())
if type1 and type2:
matchups = comparetypes(type1, type2)
else:
if type1:
matchups = {'2x':type1['damage_relations']['double_damage_from'],'1/2':type1['damage_relations']['half_damage_from'],'immune':type1['damage_relations']['no_damage_from']}
matches = ""
if matchups:
for x in matchups:
if len(matchups[x]) > 0:
matches = matches + "<br><b>{}</b>: ".format(x.capitalize())
for y in matchups[x]:
if isinstance(y, dict):
matches = matches + " <a href='http://pokemondb.net/type/{}'>{}</a>".format(y['name'].lower(),y['name'].capitalize())
else:
matches = matches + " <a href='http://pokemondb.net/type/{}'>{}</a>".format(y.lower(),y.capitalize())
else:
matches = "<i>Type matchups not currently available. Sorry :(</i>"
pkmn = pkmn + matches
if 'ability' in pokedex_config['info']:
abilities = ["<b>Abilities:</b>"]
for ability in data['abilities']:
abilities.append("<a href='{}'>{}</a>".format("http://bulbapedia.bulbagarden.net/wiki/{}_(Ability)".format(ability['ability']['name'].replace('-','_')),ability['ability']['name'].replace('-',' ').title()))
pkmn = "<br>".join([pkmn, "<br>".join(abilities)])
link_image = "http://img.pokemondb.net/artwork/{}.jpg".format(data['name'])
filename = os.path.basename(link_image)
r = yield from aiohttp.request('get', link_image)
raw = yield from r.read()
image_data = io.BytesIO(raw)
image_id = yield from bot._client.upload_image(image_data, filename=filename)
yield from bot.coro_send_message(event.conv, pkmn, image_id=image_id)
def formatNature(data):
rtndata = ["<b><u>{}</u></b>".format(data['name'].title())]
try:
rtndata.append("<b>10%+</b> {}".format(data['increased_stat']['name']).title())
except TypeError:
rtndata.append("<i>{} does not increase any stat.</i>".format(data['name'].title()))
try:
rtndata.append("<b>10%-</b> {}".format(data['decreased_stat']['name']).title())
except TypeError:
rtndata.append("<i>{} does not decrease any stat.</i>".format(data['name'].title()))
return "<br>".join(rtndata)
@asyncio.coroutine
def cacheNature(bot, data):
try:
if not bot.memory.exists(["_pokemondata"]):
bot.memory.set_by_path(["_pokemondata"], {"natures":{}})
except:
bot.memory.set_by_path(["_pokemondata"], {"natures":{}})
try:
if not bot.memory.exists(["_pokemondata", "natures"]):
bot.memory.set_by_path(["_pokemondata", "natures"], {})
except:
bot.memory.set_by_path(["_pokemondata", "natures"], {})
bot.memory.set_by_path(["_pokemondata", "natures", data["name"]], {"name":data["name"], "increased_stat":data["increased_stat"], "decreased_stat":data["decreased_stat"], "expires": str(datetime.datetime.now() + datetime.timedelta(days=5))})
return
def getNatureFromCache(bot, nature):
try:
if bot.memory.exists(["_pokemondata", "natures", nature]):
if bot.memory.get_by_path(["_pokemondata", "natures", nature, "expires"]) < str(datetime.datetime.now()):
return False
else:
return bot.memory.get_by_path(["_pokemondata", "natures", nature])
else:
return False
except:
return False
return False
@asyncio.coroutine
def getNature(bot, event, *args):
nature = " ".join(args)
try:
data = getNatureFromCache(bot, nature)
except KeyError:
data = False
if not data:
url = "http://pokeapi.co/api/v2/nature/{}/".format(nature.lower())
request = urllib.request.Request(url, headers = {"User-agent":"Mozilla/5.0"})
try:
data = json.loads(urllib.request.urlopen(request).read().decode("utf-8"))
except urllib.error.URLError as e:
yield from bot.coro_send_message(event.conv, "{}: Error: {}".format(event.user.full_name, json.loads(e.read().decode("utf8","ignore"))['detail']))
return
yield from cacheNature(bot, data)
msg = formatNature(data)
yield from bot.coro_send_message(event.conv, msg)
def pokemon(bot, event, command, *args):
'''A placeholder command for future functions.'''
if command == 'nature':
yield from getNature(bot, event, *args)
return