-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokeapi.py
52 lines (38 loc) · 1022 Bytes
/
pokeapi.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
import urllib.request
import json
import time
types = []
dic = {}
def getJson(url):
request = urllib.request.Request(url)
# must add this or the request will fail
request.add_header('User-Agent', "cheese")
data = urllib.request.urlopen(request).read()
api = json.loads(data)
return api
def addTypes(type):
if type not in types:
types.append(type)
def addToDic(type, pokemon):
ls = []
if(type in dic):
ls = dic[type]
ls.append(pokemon)
dic.update({type: ls})
def listPokemons():
url = 'https://pokeapi.co/api/v2/pokemon/?limit=151'
api = getJson(url)
for val in api["results"]:
getPokemon(val["url"])
def getPokemon(url):
print(url)
api = getJson(url)
name = api["name"]
id = api["id"]
for val in api["types"]:
type = val["type"]["name"]
addTypes(type)
addToDic(type, {"id": id, "name": name})
listPokemons()
print(types)
print(dic)