dragon-bot/app/weather.py
2017-09-04 17:56:44 -07:00

49 lines
1.4 KiB
Python

import requests
from pyowm import OWM
def get_weather(location):
"""
get_weather(location)
Returns the weather forecast for the selected zip code. Uses PythonOWM to
make life super easy.
TODO: Replace basic code block with nice looking, bold text w/ emojis
"""
owm = OWM('593e0e182f9278a10443da354c4014db')
fc = owm.three_hours_forecast(location)
f = fc.get_forecast()
reg = f.get(0)
indicator = int(reg.get_reference_time(timeformat='iso')[11:13]) // 3
ret = '```'
ret = ret + ('\nStatus: ' + reg.get_detailed_status() + ' ' +
'Current Temperature: ' +
str(reg.get_temperature('fahrenheit').get('temp')) +
'F\n\nFive day forecast:\n')
for i in range((4 - indicator) % 8, len(f), 8):
high, low = 0, 100000
for j in range(0, 8):
if (i + j < len(f)):
day = f.get(i + j)
else:
break
temps = day.get_temperature('fahrenheit')
h = temps.get('temp_max')
l = temps.get('temp_min')
if high < h:
high = h
if low > l:
low = l
ret = ret + '{:<30}'.format('Status: ' + f.get(i).get_detailed_status())
ret = ret + '{:<18}'.format('High: ' + str(high) + 'F ')
ret = ret + 'Low: ' + str(low) + 'F\n'
return ret + '```'