36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from pyowm.owm import OWM
|
|
import discord
|
|
import os
|
|
# import pprint
|
|
# pp = pprint.PrettyPrinter(indent=4)
|
|
|
|
|
|
def parse_message(message):
|
|
location = message.split()[1:]
|
|
print(location)
|
|
return get_weather(' '.join(location))
|
|
|
|
|
|
def get_weather(location):
|
|
# owm = OWM('614e00c15660f75756544b83d556fe75')
|
|
owm = OWM(os.getenv('weather_api_key'))
|
|
mgr = owm.weather_manager()
|
|
observation = mgr.weather_at_place(location) # the observation object is a box containing a weather object
|
|
weather = observation.weather
|
|
|
|
wind_data = weather.wind(unit='miles_hour')
|
|
wind_speed = wind_data['speed']
|
|
wind_direction = degToCompass(wind_data['deg'])
|
|
|
|
embed = discord.Embed(description="Weather for %s" % location, color=5793266, type="rich")
|
|
embed.add_field(name='Current weather', value=weather.detailed_status, inline=False)
|
|
embed.add_field(name='Wind', value="**Wind Speed**: %s, **Wind Direction**: %s" % (wind_speed, wind_direction), inline=False)
|
|
embed.add_field(name='Temperature (F)', value=weather.temperature('fahrenheit'), inline=False)
|
|
embed.add_field(name='Humidity', value=str(weather.humidity) + '%', inline=False)
|
|
|
|
return embed
|
|
|
|
def degToCompass(num):
|
|
val=int((num/22.5)+.5)
|
|
arr=["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
|
|
return arr[(val % 16)] |