Openweathermap.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import pyowm
  2. from core.NeuronModule import NeuronModule
  3. class Openweathermap(NeuronModule):
  4. def __init__(self, **kwargs):
  5. # get message to spell out loud
  6. super(Openweathermap, self).__init__(**kwargs)
  7. api_key = kwargs.get('api_key', None)
  8. location = kwargs.get('location', None)
  9. lang = kwargs.get('lang', 'en')
  10. if api_key is None:
  11. raise NotImplementedError("OpenWeatherMap neuron needs an api_key")
  12. if location is None:
  13. raise NotImplementedError("OpenWeatherMap neuron needs a location")
  14. owm = pyowm.OWM(API_key=api_key, language=lang)
  15. forecast = owm.daily_forecast(location)
  16. tomorrow = pyowm.timeutils.tomorrow()
  17. weather_tomorrow = forecast.get_weather_at(tomorrow)
  18. weather_tomorrow_status = weather_tomorrow.get_detailed_status()
  19. observation = owm.weather_at_place(location)
  20. weather = observation.get_weather()
  21. weather_status = weather.get_detailed_status()
  22. print "weather :", weather_status
  23. print "weather_tomorrow :", weather_tomorrow_status
  24. message = {
  25. "location": location,
  26. "weather": weather_status,
  27. "weather_tomorrow":weather_tomorrow_status
  28. }
  29. self.say(message)