def test_rain_data(): """Test format of retrieved rain data.""" result = get_data() # we must have content: assert(result[CONTENT] is not None) assert(result[RAINCONTENT] is not None) # check raindata: lines = result[RAINCONTENT].splitlines() for line in lines: (val, key) = line.split("|") try: # value must be a integer value: val = int(val) except ValueError: print("Uunable to parse line: <%s>, not na integer." % (line)) assert(False) try: datetime.strptime(key, '%H:%M') except ValueError: print("Unable to parse line: <%s>, not na time (HH:MM)." % (line))
def test_xml_data(): """Check xml data elements/xsd.""" result = get_data() # we must have content: assert(result[CONTENT] is not None) assert(result[RAINCONTENT] is not None) # check all elements we use from the xml: xmldata = xmltodict.parse(result[CONTENT])[__BRROOT] weergegevens = xmldata[__BRWEERGEGEVENS] assert(weergegevens is not None) actueelweer = weergegevens[__BRACTUEELWEER] assert(actueelweer is not None) weerstations = actueelweer[__BRWEERSTATIONS] assert(weerstations is not None) weerstation = weerstations[__BRWEERSTATION] assert(weerstation is not None) weerstation = weerstation[1] assert(weerstation[__BRLAT] is not None) assert(weerstation[__BRLON] is not None) assert(weerstation[__BRSTATIONCODE] is not None) assert(weerstation[__BRSTATIONNAAM] is not None) assert(weerstation[__BRSTATIONNAAM][__BRTEXT] is not None) assert(weerstation[SENSOR_TYPES[HUMIDITY][0]] is not None) assert(weerstation[SENSOR_TYPES[GROUNDTEMP][0]] is not None) assert(weerstation[SENSOR_TYPES[IRRADIANCE][0]] is not None) assert(weerstation[SENSOR_TYPES[MEASURED][0]] is not None) assert(weerstation[SENSOR_TYPES[PRECIPITATION][0]] is not None) assert(weerstation[SENSOR_TYPES[PRESSURE][0]] is not None) assert(weerstation[SENSOR_TYPES[STATIONNAME][0]] is not None) assert(weerstation[SENSOR_TYPES[CONDITION][0]] is not None) assert(weerstation[SENSOR_TYPES[CONDITION][0]][__BRZIN] is not None) assert(weerstation[SENSOR_TYPES[CONDITION][0]][__BRTEXT] is not None) assert(weerstation[SENSOR_TYPES[TEMPERATURE][0]] is not None) assert(weerstation[SENSOR_TYPES[VISIBILITY][0]] is not None) assert(weerstation[SENSOR_TYPES[WINDSPEED][0]] is not None) assert(weerstation[SENSOR_TYPES[WINDFORCE][0]] is not None) assert(weerstation[SENSOR_TYPES[WINDDIRECTION][0]] is not None) assert(weerstation[SENSOR_TYPES[WINDAZIMUTH][0]] is not None) assert(weerstation[SENSOR_TYPES[WINDGUST][0]] is not None)
def get_data(self, lat: Optional[float] = None, long: Optional[float] = None, time_frame: Optional[int] = None) \ -> Dict[str, Any]: # noinspection PyPackageRequirements from buienradar.buienradar import get_data, parse_data # noinspection PyPackageRequirements from buienradar.constants import SUCCESS, CONTENT, RAINCONTENT, DATA lat = lat or self.lat long = long or self.long time_frame = time_frame or self.time_frame result = get_data(latitude=lat, longitude=long) if not result.get(SUCCESS): raise RuntimeError('Error while retrieving data') data = result.get(CONTENT) rain_data = result.get(RAINCONTENT) result = parse_data(data, rain_data, lat, long, time_frame) return result.get(DATA, {})
def forecast(): timeframe = 60 raindata = 0 # gps-coordinates for the weather data latitude = float(request.args.get('latitude')) longitude = float(request.args.get('longitude')) result = get_data( latitude=latitude, longitude=longitude, ) if result.get(SUCCESS): data = result[CONTENT] raindata = result[RAINCONTENT] result = parse_data(data, raindata, latitude, longitude, timeframe) response = jsonify({ "forecast": result, "rainfall": parse_rainfall(raindata) }) response.headers.add('Access-Control-Allow-Origin', '*') return response
"""Example usage.""" from buienradar.buienradar import (CONTENT, RAINCONTENT, SUCCESS, get_data, parse_data) # minutes to look ahead for precipitation forecast # (5..120) timeframe = 60 # gps-coordinates for the weather data latitude = 51.50 longitude = 6.20 result = get_data( latitude=latitude, longitude=longitude, ) if result.get(SUCCESS): data = result[CONTENT] raindata = result[RAINCONTENT] result = parse_data(data, raindata, latitude, longitude, timeframe) print(result)