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
         "latitude": USER_LATITUDE,
14
         "latitude": USER_LATITUDE,
15
         "longitude": USER_LONGITUDE
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
 import json
24
 import json
38
             self.options['latitude'],
43
             self.options['latitude'],
39
             self.options['longitude']
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
         self.commands = {
51
         self.commands = {
44
             'whats the temperature': self._temperature,
52
             'whats the temperature': self._temperature,
53
 
61
 
54
         self.corpus_strings.update(self.commands)
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
     def _temperature(self):
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
     def _todays_low(self):
71
     def _todays_low(self):
61
-        return '{:.1f} degrees Fahrenheit'.format(
72
+        return self._format_temperature(
62
             self.cache['daily']['data'][0]['temperatureMin'])
73
             self.cache['daily']['data'][0]['temperatureMin'])
63
 
74
 
64
     def _todays_high(self):
75
     def _todays_high(self):
65
-        return '{:.1f} degrees Fahrenheit'.format(
76
+        return self._format_temperature(
66
             self.cache['daily']['data'][0]['temperatureMax'])
77
             self.cache['daily']['data'][0]['temperatureMax'])
67
 
78
 
68
     def _tomorrows_high(self):
79
     def _tomorrows_high(self):
69
-        return '{:.1f} degrees Fahrenheit'.format(
80
+        return self._format_temperature(
70
             self.cache['daily']['data'][1]['temperatureMax'])
81
             self.cache['daily']['data'][1]['temperatureMax'])
71
 
82
 
72
     def _relative_humidity(self):
83
     def _relative_humidity(self):

Loading…
Cancel
Save