Openweathermap.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. temp_unit = kwargs.get('temp_unit', 'celsius')
  11. if api_key is None:
  12. raise NotImplementedError("OpenWeatherMap neuron needs an api_key")
  13. if location is None:
  14. raise NotImplementedError("OpenWeatherMap neuron needs a location")
  15. owm = pyowm.OWM(API_key=api_key, language=lang)
  16. # Tomorrow
  17. forecast = owm.daily_forecast(location)
  18. tomorrow = pyowm.timeutils.tomorrow()
  19. weather_tomorrow = forecast.get_weather_at(tomorrow)
  20. weather_tomorrow_status = weather_tomorrow.get_detailed_status()
  21. sunset_time_tomorrow = weather_tomorrow.get_sunset_time('iso')
  22. sunrise_time_tomorrow = weather_tomorrow.get_sunrise_time('iso')
  23. temp_tomorrow = weather_tomorrow.get_temperature(unit=temp_unit)
  24. temp_tomorrow_temp = temp_tomorrow['day']
  25. temp_tomorrow_temp_max = temp_tomorrow['max']
  26. temp_tomorrow_temp_min = temp_tomorrow['min']
  27. pressure_tomorrow = weather_tomorrow.get_pressure()
  28. pressure_tomorrow_press = pressure_tomorrow['press']
  29. pressure_tomorrow_sea_level = pressure_tomorrow['sea_level']
  30. humidity_tomorrow = weather_tomorrow.get_humidity()
  31. wind_tomorrow = weather_tomorrow.get_wind()
  32. # wind_tomorrow_deg = wind_tomorrow['deg']
  33. wind_tomorrow_speed = wind_tomorrow['speed']
  34. snow_tomorrow = weather_tomorrow.get_snow()
  35. rain_tomorrow = weather_tomorrow.get_rain()
  36. clouds_coverage_tomorrow = weather_tomorrow.get_clouds()
  37. # Today
  38. observation = owm.weather_at_place(location)
  39. weather_today = observation.get_weather()
  40. weather_today_status = weather_today.get_detailed_status()
  41. sunset_time_today = weather_today.get_sunset_time('iso')
  42. sunrise_time_today = weather_today.get_sunrise_time('iso')
  43. temp_today = weather_today.get_temperature(unit=temp_unit)
  44. temp_today_temp = temp_today['temp']
  45. temp_today_temp_max = temp_today['temp_max']
  46. temp_today_temp_min = temp_today['temp_min']
  47. pressure_today = weather_today.get_pressure()
  48. pressure_today_press = pressure_today['press']
  49. pressure_today_sea_level = pressure_today['sea_level']
  50. humidity_today = weather_today.get_humidity()
  51. wind_today= weather_today.get_wind()
  52. wind_today_deg = wind_today['deg']
  53. wind_today_speed = wind_today['speed']
  54. snow_today = weather_today.get_snow()
  55. rain_today = weather_today.get_rain()
  56. clouds_coverage_today = weather_today.get_clouds()
  57. message = {
  58. "location": location,
  59. "weather_today": weather_today_status,
  60. "sunset_today_time": sunset_time_today,
  61. "sunrise_today_time": sunrise_time_today,
  62. "temp_today_temp": temp_today_temp,
  63. "temp_today_temp_max": temp_today_temp_max,
  64. "temp_today_temp_min": temp_today_temp_min,
  65. "pressure_today_press": pressure_today_press,
  66. "pressure_today_sea_level": pressure_today_sea_level,
  67. "humidity_today": humidity_today,
  68. "wind_today_deg": wind_today_deg,
  69. "wind_today_speed": wind_today_speed,
  70. "snow_today": snow_today,
  71. "rain_today": rain_today,
  72. "clouds_coverage_today": clouds_coverage_today,
  73. "weather_tomorrow": weather_tomorrow_status,
  74. "sunset_time_tomorrow": sunset_time_tomorrow,
  75. "sunrise_time_tomorrow": sunrise_time_tomorrow,
  76. "temp_tomorrow_temp": temp_tomorrow_temp,
  77. "temp_tomorrow_temp_max": temp_tomorrow_temp_max,
  78. "temp_tomorrow_temp_min": temp_tomorrow_temp_min,
  79. "pressure_tomorrow_press": pressure_tomorrow_press,
  80. "pressure_tomorrow_sea_level": pressure_tomorrow_sea_level,
  81. "humidity_tomorrow": humidity_tomorrow,
  82. # "wind_tomorrow_deg": wind_tomorrow_deg,
  83. "wind_tomorrow_speed": wind_tomorrow_speed,
  84. "snow_tomorrow": snow_tomorrow,
  85. "rain_tomorrow": rain_tomorrow,
  86. "clouds_coverage_tomorrow": clouds_coverage_tomorrow
  87. }
  88. self.say(message)