| 1 |
import re |
| 2 |
|
| 3 |
|
| 4 |
class FeedItemHelper(object): |
| 5 |
"""Helper class to get content from a Meteoclimatic RSS feed item.""" |
| 6 |
|
| 7 |
_regex_pattern = r"\[\[\<(?P<station_code>\w+);\((?P<temp_current>-?[0-9,]+);(?P<temp_max>-?[0-9,]+);(?P<temp_min>-?[0-9,]+);(?P<condition>\w*)\);\((?P<humidity_current>-?[0-9,]*);(?P<humidity_max>-?[0-9,]*);(?P<humidity_min>-?[0-9,]*)\);\((?P<pressure_current>-?[0-9,]*);(?P<pressure_max>-?[0-9,]*);(?P<pressure_min>-?[0-9,]*)\);\((?P<wind_current>-?[0-9,]*);(?P<wind_max>-?[0-9,]*);(?P<wind_bearing>-?[0-9,]*)\);\((?P<rain>-?[0-9,]*)\);" |
| 8 |
|
| 9 |
def __init__(self, feed_item): |
| 10 |
"""Initialize the class.""" |
| 11 |
self.match = re.search(self._regex_pattern, str(feed_item.description)) |
| 12 |
if not self.match: |
| 13 |
raise ValueError("Could not parse station information") |
| 14 |
|
| 15 |
def get_text(self, field_name): |
| 16 |
"""Return the value in 'field_name' from the item or None if not found.""" |
| 17 |
try: |
| 18 |
value = self.match.group(field_name) |
| 19 |
except IndexError: |
| 20 |
return None |
| 21 |
if len(value) == 0: |
| 22 |
return None |
| 23 |
return value |
| 24 |
|
| 25 |
def get_float(self, field_name): |
| 26 |
"""Return the value in 'field_name' from the item or None if not found.""" |
| 27 |
value = self.get_text(field_name) |
| 28 |
if value is None: |
| 29 |
return None |
| 30 |
try: |
| 31 |
value = float(value.replace(",", ".")) |
| 32 |
except ValueError: |
| 33 |
return None |
| 34 |
if value == -99.0: |
| 35 |
|
| 36 |
return None |
| 37 |
return value |