Browse Source

Weather plugin updates

The cache time is now configurable by the user.  If no cache time is
set, the old default of half an hour is still used.

Temperature formatting has been factored out into its own function.
Clara Hobbs 7 years ago
parent
commit
3695411da6
1 changed files with 17 additions and 6 deletions
  1. 17
    6
      kayleevc/plugins/darksky.py

+ 17
- 6
kayleevc/plugins/darksky.py View File

@@ -14,6 +14,11 @@ the Dark Sky API, as well as decimal latitude and longitude::
14 14
         "latitude": USER_LATITUDE,
15 15
         "longitude": USER_LONGITUDE
16 16
     }
17
+
18
+Weather information is cached for up to ``cache_max_age`` seconds
19
+(default 1800, half an hour).  To conserve API calls, the cache is not
20
+updated until the first time the user requests weather information
21
+after the cache becomes stale.
17 22
 """
18 23
 
19 24
 import json
@@ -38,7 +43,10 @@ class Plugin(PluginBase):
38 43
             self.options['latitude'],
39 44
             self.options['longitude']
40 45
         )
41
-        self._cache_max_age = 1800
46
+        try:
47
+            self._cache_max_age = self.options['cache_max_age']
48
+        except NameError:
49
+            self._cache_max_age = 1800
42 50
 
43 51
         self.commands = {
44 52
             'whats the temperature': self._temperature,
@@ -53,20 +61,23 @@ class Plugin(PluginBase):
53 61
 
54 62
         self.corpus_strings.update(self.commands)
55 63
 
64
+    def _format_temperature(self, temperature, unit='Fahrenheit'):
65
+        """Format a temperature for the TTS system"""
66
+        return '{:.1f} degrees {}'.format(temperature, unit)
67
+
56 68
     def _temperature(self):
57
-        return '{:.1f} degrees Fahrenheit'.format(
58
-            self.cache['currently']['temperature'])
69
+        return self._format_temperature(self.cache['currently']['temperature'])
59 70
 
60 71
     def _todays_low(self):
61
-        return '{:.1f} degrees Fahrenheit'.format(
72
+        return self._format_temperature(
62 73
             self.cache['daily']['data'][0]['temperatureMin'])
63 74
 
64 75
     def _todays_high(self):
65
-        return '{:.1f} degrees Fahrenheit'.format(
76
+        return self._format_temperature(
66 77
             self.cache['daily']['data'][0]['temperatureMax'])
67 78
 
68 79
     def _tomorrows_high(self):
69
-        return '{:.1f} degrees Fahrenheit'.format(
80
+        return self._format_temperature(
70 81
             self.cache['daily']['data'][1]['temperatureMax'])
71 82
 
72 83
     def _relative_humidity(self):

Loading…
Cancel
Save