fuzzy-pokedex/pokeinfo

125 lines
3.5 KiB
Plaintext
Raw Normal View History

2023-04-16 11:14:38 -05:00
#!/usr/bin/env python
from bs4 import BeautifulSoup
import requests
import sys
import re
######## Get Pokemon Name ########
if len(sys.argv) < 2:
pokemon = input("Enter the name of the pokemon to look up: ").title()
# Two Word Names Like Mime Jr.
elif len(sys.argv) == 3:
pokemon = (sys.argv[1] + " " + sys.argv[2]).title()
else:
pokemon = sys.argv[1].title()
if pokemon == "mr-mime":
pokemon = "Mr.Mime" #serebii listed this way
elif pokemon == "ho-oh":
pokemon = "Ho-oh" #serebii listed this way
######## Get Pokedex Entry Number ########
2023-04-17 00:56:19 -05:00
begin_url = ["http://serebii.net/pokedex-xy/",
"http://serebii.net/pokedex-sv/",
"http://serebii.net/pokedex-swsh/",
"http://serebii.net/pokedex-sm/",
"http://serebii.net/pokedex-bw/",
"http://serebii.net/pokedex-dp/",
"http://serebii.net/pokedex-rs/",
"http://serebii.net/pokedex-gs/",
"http://serebii.net/pokedex/"]
2023-04-17 03:28:49 -05:00
2023-04-17 00:03:49 -05:00
for i in begin_url:
2023-04-17 03:28:49 -05:00
try:
request1 = requests.get(i)
except OSError:
sys.exit(1)
html1 = request1.content
soup1 = BeautifulSoup(html1, "html.parser")
page_info1 = soup1.get_text()
2023-04-17 00:03:49 -05:00
# Pokedex Entries Start At Index 5000
2023-04-17 03:28:49 -05:00
pokemon_index = page_info1.find(pokemon, 5000)
if pokemon_index != -1:
break
2023-04-16 11:14:38 -05:00
if pokemon_index == -1:
2023-04-17 00:03:49 -05:00
print("You didn't enter a valid pokemon!")
2023-04-17 13:10:30 -05:00
sys.exit(1)
2023-04-16 11:14:38 -05:00
pokedex_number = page_info1[pokemon_index-4 : pokemon_index-1]
######## Get Pokemon Info ########
2023-04-17 00:03:49 -05:00
for i in begin_url:
2023-04-17 13:10:30 -05:00
try:
full_url = i + pokedex_number + ".shtml"
request2 = requests.get(full_url)
except OSError:
sys.exit(1)
2023-04-17 00:03:49 -05:00
html2 = request2.content
soup2 = BeautifulSoup(html2, "html.parser")
page_info2 = soup2.get_text()
page_not_found = bool(soup2.find(string=re.compile("Page Not Found")))
if page_not_found == False:
break
2023-04-17 03:28:49 -05:00
2023-04-17 00:03:49 -05:00
if page_not_found:
print(" Stats currently unavailable :( ")
2023-04-17 03:28:49 -05:00
sys.exit(1)
2023-04-16 11:14:38 -05:00
######## Get Stats ########
base_total = soup2.find(string=re.compile("Base Stats - Total:"))
# Only want everything after Total
2023-04-17 13:10:30 -05:00
base_total = str("Total Unavailable")
base_total = base_total[base_total.find("Total"):]
2023-04-16 11:14:38 -05:00
base_stats = soup2.find_all(class_="fooinfo")
hp = str(base_stats[len(base_stats)-21])
attack = str(base_stats[len(base_stats)-20])
defence = str(base_stats[len(base_stats)-19])
spec_attack = str(base_stats[len(base_stats)-18])
spec_defence = str(base_stats[len(base_stats)-17])
speed = str(base_stats[len(base_stats)-16])
hp = hp[hp.find(">")+1 : hp.find("/")-1]
attack = attack[attack.find(">")+1 : attack.find("/")-1]
defence = defence[defence.find(">")+1 : defence.find("/")-1]
spec_attack = spec_attack[spec_attack.find(">")+1 : spec_attack.find("/")-1]
spec_defence = spec_defence[spec_defence.find(">")+1 : spec_defence.find("/")-1]
speed = speed[speed.find(">")+1:speed.find("/")-1]
######## Get Abilities ########
abilities_index = page_info2.find("Abilities:")
abilities = page_info2[abilities_index:]
end_index = abilities.find("\n")
abilities = abilities[:end_index-1]
######## Get Gender ########
gender_index = page_info2.find("GenderType")
gender = page_info2[gender_index:]
end_index = gender.find("\n")
gender = gender[:end_index]
gender = gender[gender.find(" ")+1:]
if "is Genderless" in gender:
gender = "Genderless"
######## Print everything out ########
2023-04-17 00:03:49 -05:00
print("")
2023-04-16 11:14:38 -05:00
print("Name: ", pokemon)
print("Pokedex Number: ", pokedex_number)
print("Gender Ratio: ", str(gender))
print(abilities)
2023-04-17 00:03:49 -05:00
print("")
print("Base Stats")
print("----------------------")
2023-04-16 11:14:38 -05:00
print(base_total)
print("Hp: ", hp)
print("Attack: ", attack)
print("Defence: ", defence)
print("Special Attack: ", spec_attack)
print("Special Defence: ", spec_defence)
print("Speed: ", speed)