from time import sleep
import urequests
from mysecrets import api_key
from ujson import dumps
def prettify(s):
i = 0
for c in s:
if c in ['[', '{']:
print(c)
i += 2
print(i*' ', end='')
elif c in [']', '}']:
print("")
i -= 2
print(i*' ', end='')
print(c, end='')
elif c == ',':
print(c)
print((i-1)*' ', end='')
else:
print(c, end='')
print("")
while True:
location = "Porto"
url = "http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&appid={1}" \
.format(location, api_key)
r = urequests.get(url).json()
prettify(dumps(r))
print('Estão {0} graus na cidade do {1}, prevendo-se um máximo de {2} graus!' \
.format(r["main"]["temp"], r["name"], r["main"]["temp_max"]))
sleep(5)