Openweathermap.py 5.3 KB

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