Openweathermap.py 4.6 KB

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