Browse Source

[Feature] Neuron Weather with the OpenWeatherMap API

monf 8 years ago
parent
commit
559a455dda

+ 6 - 3
brain.yml

@@ -11,11 +11,14 @@
   - name: "get the weather"
     neurons:
       - openweathermap:
-          api_key: "your_key"
+          api_key: "your_api"
+          lang: "fr"
           say_template:
-          - "Les prévisions pour {{ date }} sont {{ weather }}"
+          - "Aujourd'hui a {{ location }} le temps est {{ weather }}, demain il sera {{ weather_tomorrow }}"
+          args:
+          - location
     signals:
-      - order: "{{ date }} quel temps fait-il a {{ location }}"
+      - order: "quel temps fait-il à {{ location }}"
 
   - name: "say hello"
     neurons:

+ 0 - 37
neurons/OpenWeatherMap/OpenWeatherMap.py

@@ -1,37 +0,0 @@
-import pyowm
-
-from core.NeuronModule import NeuronModule
-
-
-class OpenWeatherMap(NeuronModule):
-    def __init__(self, **kwargs):
-        # get message to spell out loud
-        super(OpenWeatherMap, self).__init__(**kwargs)
-
-        api_key = kwargs.get('api_key', None)
-        location = kwargs.get('location', None)
-        date = kwargs.get('date', None)
-
-        if api_key is None:
-            raise NotImplementedError("OpenWeatherMap neuron needs an api_key")
-        if location is None:
-            raise NotImplementedError("OpenWeatherMap neuron needs a location")
-
-        owm = pyowm.OWM(api_key)
-
-
-        # forecast = owm.daily_forecast(location)
-        # tomorrow = pyowm.timeutils.tomorrow()
-        # forecast.will_be_sunny_at(tomorrow)
-
-        observation = owm.weather_at_place(location)
-        weather = observation.get_weather()
-
-        message = {
-            "date": date,
-            "weathers": weather,
-        }
-
-        self.say(message)
-
-

+ 0 - 3
neurons/OpenWeatherMap/__init__.py

@@ -1,3 +0,0 @@
-from OpenWeatherMap import OpenWeatherMap
-
-

+ 42 - 0
neurons/Openweathermap/Openweathermap.py

@@ -0,0 +1,42 @@
+import pyowm
+
+from core.NeuronModule import NeuronModule
+
+
+class Openweathermap(NeuronModule):
+    def __init__(self, **kwargs):
+        # get message to spell out loud
+        super(Openweathermap, self).__init__(**kwargs)
+
+        api_key = kwargs.get('api_key', None)
+        location = kwargs.get('location', None)
+        lang = kwargs.get('lang', 'en')
+
+        if api_key is None:
+            raise NotImplementedError("OpenWeatherMap neuron needs an api_key")
+        if location is None:
+            raise NotImplementedError("OpenWeatherMap neuron needs a location")
+
+        owm = pyowm.OWM(API_key=api_key, language=lang)
+
+        forecast = owm.daily_forecast(location)
+        tomorrow = pyowm.timeutils.tomorrow()
+        weather_tomorrow = forecast.get_weather_at(tomorrow)
+        weather_tomorrow_status = weather_tomorrow.get_detailed_status()
+
+        observation = owm.weather_at_place(location)
+        weather = observation.get_weather()
+        weather_status = weather.get_detailed_status()
+
+        print "weather :", weather_status
+        print "weather_tomorrow :", weather_tomorrow_status
+
+        message = {
+            "location": location,
+            "weather": weather_status,
+            "weather_tomorrow":weather_tomorrow_status
+        }
+
+        self.say(message)
+
+

+ 1 - 0
neurons/Openweathermap/__init__.py

@@ -0,0 +1 @@
+from Openweathermap import Openweathermap

+ 1 - 0
neurons/__init__.py

@@ -7,3 +7,4 @@ from sleep import Sleep
 from systemdate import Systemdate
 from gmail_checker import Gmail_checker
 from push_message import Push_message
+from Openweathermap import Openweathermap