forked from maxpowa/inumuta-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokedex.py
309 lines (259 loc) · 12.5 KB
/
pokedex.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# coding=utf-8
"""
pokedex.py - Get your Pokemon fix!
Copyright 2014 Max Gurela
Licensed under the Eiffel Forum License 2.
"""
from __future__ import unicode_literals
from sopel import web
from sopel.module import commands, rule, thread, example
from bs4 import BeautifulSoup
import re
import sys
if sys.version_info.major < 3:
from urllib import unquote
else:
from urllib.parse import unquote
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0' \
' Firefox/17.0'
@thread(True)
@commands('dex', 'pokedex', 'pok\u00e9dex')
@example('.pokedex Charmander', '[Pok\u00E9dex] #004 Charmander | Fire | Blaze | Generation 1 | \u215E male, \u215B female | Egg: Monster/Dragon')
def pokedex(bot, trigger):
"""
.pokedex <query> - Search for a Pokemon and much, much more. See '.pokedex manual' for more information
"""
if not trigger.group(3):
bot.say('[Pok\u00E9dex] Please consult the Pok\u00E9dex manual (.pokedex manual).')
return
if trigger.group(3).lower() == 'welcome':
bot.say('<Prof. Oak> Hello there! Welcome to the world of Pok\u00E9mon! My name is Oak! People call me the Pok\u00E9mon Prof! This world is inhabited by creatures called Pok\u00E9mon! For some people, Pok\u00E9mon are pets. Other use them for fights. Myself... I study Pok\u00E9mon as a profession. {nick}! Your very own Pok\u00E9mon legend is about to unfold! A world of dreams and adventures with Pok\u00E9mon awaits! Let\'s go!'.replace('{nick}', trigger.nick))
bot.say('<Prof. Oak> First things first, lets get your pok\u00E9dex set up. I\'ll need you to run a couple queries to make sure it\'s working properly.')
return
if trigger.group(3).lower() == 'manual':
bot.action('opens the Pok\u00E9dex man pages (transmitted via notice)')
bot.notice('The pok\u00e9dex is operated with two base commands, either .dex or .pokedex', destination=trigger.nick)
bot.notice('\u0081', destination=trigger.nick)
bot.notice('You may use the pokedex to research the following things:', destination=trigger.nick)
bot.notice(' - Pok\u00e9mon .dex Abra or .dex 63', destination=trigger.nick)
bot.notice(' - Pok\u00e9mon stats .dex -s Abra or .dex -s 63', destination=trigger.nick)
bot.notice(' - Pok\u00e9mon moves .dex move:tackle', destination=trigger.nick)
bot.notice(' - Pok\u00e9mon types .dex type:psychic', destination=trigger.nick)
bot.notice(' - Items .dex item:master ball', destination=trigger.nick)
bot.notice('For language-specific results, just prepend @<lang code>. (e.g. .dex @en:charge)', destination=trigger.nick)
return
query = trigger.group(2).strip()
all_stats = (trigger.group(3).lower() == '-s')
if all_stats:
query = query.replace(trigger.group(3), '').strip()
##TODO: REMOVE
#bot.say(u'[Pok\u00E9dex] There\'s a time and a place for everything. But not now. http://puu.sh/cvW4m/e510f8be5b.jpg')
#return
##TODO: REMOVE
url = 'http://veekun.com/dex/lookup?lookup='
if trigger.group(3).isdigit() and 'pokemon:' not in trigger.group(3):
url = 'http://veekun.com/dex/lookup?lookup=pokemon:'
url = follow_redirects(bot, url + query)
url = unquote(url)
if not url:
bot.say('[Pok\u00E9dex] Invalid query, please try again.')
return
soup = get_soup(url)
if soup:
try:
crumb = soup.find('ul', id='breadcrumbs').text.lower()
except:
bot.say('[Pok\u00E9dex] Please return to Professor Oak, you are missing an important software patch. (404)')
return
if 'moves' in crumb:
parse_move(bot, soup)
elif 'pok\u00E9mon' in crumb:
parse_poke(bot, soup, all_stats)
elif 'item' in crumb:
parse_item(bot, soup)
elif 'type' in crumb:
parse_type(bot, soup)
elif 'disambiguation' in crumb:
parse_disambig(bot, soup, trigger.nick)
elif 'abilities' in crumb:
parse_abilities(bot, soup)
else:
bot.say('[Pok\u00E9dex] There is no Pok\u00E9dex function that sufficiently matches what you\'re trying.')
return
else:
bot.say('[Pok\u00E9dex] http://puu.sh/cvW4m/e510f8be5b.jpg')
return
def follow_redirects(bot, url):
"""
Follow HTTP 3xx redirects, and return the actual URL. Return None if
there's a problem.
"""
try:
connection = web.get_urllib_object(url, 60)
url = connection.geturl() or url
connection.close()
except Exception as e:
return None
return url
def get_soup(url):
return BeautifulSoup(web.get(url, headers={'User-Agent': user_agent}), 'html.parser')
def parse_move(bot, soup):
soup = soup.find('div', id='content')
title = soup.find('p', id='dex-page-name').text
types = [img['title'].split(':')[0] for img in soup.find('p', id='dex-page-types').find_all('img')]
summary = soup.find('div', class_='dex-page-beside-portrait').find('p').text
soup = soup.find('div', class_='dex-column-container')
power = soup.find_all('div', class_='dex-column')[0].find_all('dd')[0].text
accuracy = soup.find_all('div', class_='dex-column')[0].find_all('dd')[1].text
pp = soup.find_all('div', class_='dex-column')[0].find_all('dd')[2].text
target = soup.find_all('div', class_='dex-column')[0].find_all('dd')[3].text
effect_chance = soup.find_all('div', class_='dex-column')[0].find_all('dd')[4].text
output = ['[Pok\u00E9dex] ', title, ' | ', types[1], '/', types[0], ' | ', summary]
output += ' | Dmg: '
output += power.split(';')[0].strip()
output += ' | Acc: '
output += accuracy.strip()
output += ' | PP: '
output += pp.split(',')[0].strip()
output += ' | Flags: '
flags = [item.find('a').text for item in soup.find_all('div', class_='dex-column')[1].find('ul').find_all('li') if not item.has_attr('class')]
for flag in flags:
output += flag
output += ', '
bot.say(''.join(output[:-2]))
def parse_poke(bot, soup, stats=False):
pokemon = dict()
soup = soup.find('div', id='content')
part = soup.find('div', class_='dex-page-portrait')
pokemon['name'] = part.find('p', id='dex-page-name').text
pokemon['types'] = [type.get('alt', '') for type in part.find('p', id='dex-page-types').find_all('img')]
part = soup.find('div', class_='dex-page-beside-portrait')
pokemon['abilities'] = [ability.text for ability in part.find('dl', class_='pokemon-abilities').find_all('dt')]
part = soup.find('div', class_='dex-column-container')
pokemon['gen'] = part.find_all('div', class_='dex-column')[0].find_all('dd')[0].find('img').get('alt', '')
pokemon['number'] = part.find_all('div', class_='dex-column')[0].find_all('dd')[1].text.replace('\n', '').strip().zfill(3)
pokemon['gender'] = part.find_all('div', class_='dex-column')[1].find_all('dd')[0].text.replace('\n', '').strip()
pokemon['egg_types'] = [egg.text for egg in part.find_all('div', class_='dex-column')[1].find('ul', class_='inline-commas').find_all('li')]
pokemon['steps_hatch'] = ' '.join(part.find_all('div', class_='dex-column')[1].find_all('dd')[3].text.replace('\n', '').strip().split())
pokemon['base_exp'] = part.find_all('div', class_='dex-column')[2].find_all('dd')[0].text.replace('\n', '').strip()
part = soup.find('table', class_='dex-pokemon-stats')
pokemon['base_hp'] = part.find_all('div', class_='dex-pokemon-stats-bar')[0].text
pokemon['base_atk'] = part.find_all('div', class_='dex-pokemon-stats-bar')[1].text
pokemon['base_def'] = part.find_all('div', class_='dex-pokemon-stats-bar')[2].text
pokemon['base_SpAtk'] = part.find_all('div', class_='dex-pokemon-stats-bar')[3].text
pokemon['base_SpDef'] = part.find_all('div', class_='dex-pokemon-stats-bar')[4].text
pokemon['base_speed'] = part.find_all('div', class_='dex-pokemon-stats-bar')[5].text
pokemon['base_total'] = part.find_all('div', class_='dex-pokemon-stats-bar')[6].text
output = []
if not stats:
output += '[Pok\u00E9dex] #'
output += pokemon['number']
output += ' '
output += pokemon['name']
output += ' | '
for type in pokemon['types']:
output += type
output += '/'
output.pop()
output += ' | '
for ability in pokemon['abilities']:
output += ability
output += '/'
output.pop()
output += ' | '
output += pokemon['gen']
output += ' | '
output += pokemon['gender']
output += ' | Egg: '
for ability in pokemon['egg_types']:
output += ability
output += '/'
output.pop()
else:
output += '[Pok\u00E9dex] #'
output += pokemon['number']
output += ' '
output += pokemon['name']
output += ' | '
output += pokemon['base_exp']
output += ' EXP | Speed '
output += pokemon['base_speed']
output += ' | '
output += pokemon['base_hp']
output += ' HP | Attack '
output += pokemon['base_atk']
output += ' | Defense '
output += pokemon['base_def']
output += ' | Sp. Atk '
output += pokemon['base_SpAtk']
output += ' | Sp. Def '
output += pokemon['base_SpDef']
output += ' | Total '
output += pokemon['base_total']
bot.say(''.join(output))
return
def parse_item(bot, soup):
soup = soup.find('div', id='content')
title = soup.find('p', id='dex-page-name').text
soup = soup.find('div', class_='dex-page-beside-portrait')
summary = soup.find('p').text
cost = soup.find('dl').find_all('dd')[0].text.strip()
flags = [item.text for item in soup.find('dl').find('ul', class_='classic-list').find_all('li')]
output = ['[Pok\u00E9dex] ', title, ' | ', cost, ' | ', summary, ' | Flags: ']
for flag in flags:
output += flag.lower()
output += ', '
bot.say(''.join(output[:-2]))
def parse_type(bot, soup):
soup = soup.find('div', id='content')
title = soup.find('p', id='dex-page-name').text
soup = soup.find('div', class_='dex-page-beside-portrait')
dealt = soup.find_all('ul', class_='dex-type-list')[0].find_all('li')
taken = soup.find_all('ul', class_='dex-type-list')[1].find_all('li')
for i, li in enumerate(dealt):
dealt[i] = (li.find('a').find('img').get('title'), li.text.strip())
dealt.sort(key=lambda x: x[1])
for i, li in enumerate(taken):
taken[i] = (li.find('a').find('img').get('title'), li.text.strip())
taken.sort(key=lambda x: x[1])
red = '\x034'
green = '\x033'
yellow = '\x038'
reset = '\x03'
output = ['[Pok\u00E9dex] ', title, ' | Legend:', green, ' 2x', reset, yellow, ' \xbdx', reset, red, ' 0x', reset, ' | Damage Dealt: ']
for type, value in dealt:
if value != '1' and type != title:
if value == '0':
type = red + type + reset
elif value == '\xbd':
type = yellow + type + reset
elif value == '2':
type = green + type + reset
output += type
output += ', '
output = output[:-2]
output += ' | Damage Taken: '
for type, value in taken:
if value != '1' and type != title:
if value == '0':
type = red + type + reset
elif value == '\xbd':
type = yellow + type + reset
elif value == '2':
type = green + type + reset
output += type
output += ', '
bot.say(''.join(output[:-2]))
def parse_abilities(bot, soup):
soup = soup.find('div', id='content')
title = soup.find('p', id='dex-page-name').text
summary = re.sub(r'\(.+\)$', '', soup.find('div', class_='dex-page-beside-portrait').find('div', class_="markdown").text)
bot.say('[Pok\u00E9dex] ' + title + ': ' + summary)
def parse_disambig(bot, soup, sender=None):
soup = soup.find('div', id='content')
things = [' '.join(thing.text.replace('\n', '').split()) for thing in soup.find('ul', class_="classic-list").find_all('li')]
things = [thing for thing in things if 'Conquest' not in thing]
if (len(things) > 10):
things = things[:10]
bot.say('[Pok\u00E9dex] Sorry, I couldn\'t find exactly what you\'re looking for. I did find ' + str(len(things)) + ' possible results though. (transmitted via notice)')
[bot.notice(' - ' + re.sub(r'\(.+\)$', '', thing), destination=sender) for thing in things]