def parse_JSON(self, JSON_string): """ Parses a *Forecast* instance out of raw JSON data. Only certain properties of the data are used: if these properties are not found or cannot be parsed, an error is issued. :param JSON_string: a raw JSON string :type JSON_string: str :returns: a *Forecast* instance or ``None`` if no data is available :raises: *ParseResponseError* if it is impossible to find or parse the data needed to build the result, *APIResponseError* if the JSON string embeds an HTTP status error (this is an OWM web API 2.5 bug) """ if JSON_string is None: raise parse_response_error.ParseResponseError('JSON data is None') d = json.loads(JSON_string) # Check if server returned errors: this check overcomes the lack of use # of HTTP error status codes by the OWM API 2.5. This mechanism is # supposed to be deprecated as soon as the API fully adopts HTTP for # conveying errors to the clients if 'message' in d and 'cod' in d: if d['cod'] == "404": print( "OWM API: data not found - response payload: " + json.dumps(d), d['cod']) return None elif d['cod'] != "200": raise api_response_error.APIResponseError( "OWM API: error - response payload: " + json.dumps(d), d['cod']) try: place = location.location_from_dictionary(d) except KeyError: raise parse_response_error.ParseResponseError(''.join([ __name__, ': impossible to read location info from JSON data' ])) # Handle the case when no results are found if 'count' in d and d['count'] == "0": weathers = [] elif 'cnt' in d and d['cnt'] == 0: weathers = [] else: if 'list' in d: try: weathers = [weather.weather_from_dictionary(item) \ for item in d['list']] except KeyError: raise parse_response_error.ParseResponseError( ''.join([__name__, ': impossible to read weather ' \ 'info from JSON data']) ) else: raise parse_response_error.ParseResponseError( ''.join([__name__, ': impossible to read weather ' \ 'list from JSON data']) ) current_time = int(round(time.time())) return forecast.Forecast(None, current_time, place, weathers)
def parse_JSON(self, JSON_string): """ Parses an *Observation* instance out of raw JSON data. Only certain properties of the data are used: if these properties are not found or cannot be parsed, an error is issued. :param JSON_string: a raw JSON string :type JSON_string: str :returns: an *Observation* instance or ``None`` if no data is available :raises: *ParseResponseError* if it is impossible to find or parse the data needed to build the result, *APIResponseError* if the JSON string embeds an HTTP status error (this is an OWM web API 2.5 bug) """ if JSON_string is None: raise parse_response_error.ParseResponseError('JSON data is None') d = loads(JSON_string) # Check if server returned errors: this check overcomes the lack of use # of HTTP error status codes by the OWM API 2.5. This mechanism is # supposed to be deprecated as soon as the API fully adopts HTTP for # conveying errors to the clients if 'message' in d and 'cod' in d: if d['cod'] == "404": print("OWM API: observation data not available - response " \ "payload: " + dumps(d)) return None else: raise api_response_error.APIResponseError( "OWM API: error - response payload: " + \ dumps(d)) try: place = location.location_from_dictionary(d) except KeyError: raise parse_response_error.ParseResponseError( ''.join([__name__, ': impossible to ' \ 'read location info from JSON data'])) try: w = weather.weather_from_dictionary(d) except KeyError: raise parse_response_error.ParseResponseError( ''.join([__name__, ': impossible to ' \ 'read weather info from JSON data'])) current_time = int(round(time())) return observation.Observation(current_time, place, w)
def parse_JSON(self, JSON_string): """ Parses a *StatioHistory* instance out of raw JSON data. Only certain properties of the data are used: if these properties are not found or cannot be parsed, an error is issued. :param JSON_string: a raw JSON string :type JSON_string: str :returns: a *StatioHistory* instance or ``None`` if no data is available :raises: *ParseResponseError* if it is impossible to find or parse the data needed to build the result, *APIResponseError* if the JSON string embeds an HTTP status error (this is an OWM web API 2.5 bug) """ d = json.loads(JSON_string) # Check if server returned errors: this check overcomes the lack of use # of HTTP error status codes by the OWM API but it's supposed to be # deprecated as soon as the API implements a correct HTTP mechanism for # communicating errors to the clients. In addition, in this specific # case the OWM API responses are the very same either when no results # are found for a station and when the station does not exist! measurements = {} try: if 'cod' in d: if d['cod'] != "200": raise api_response_error.APIResponseError( "OWM API: error - response " + \ "payload: " + str(d)) if str(d['cnt']) == "0": return None else: for item in d['list']: if 'temp' not in item: temp = None elif isinstance(item['temp'], dict): temp = item['temp']['v'] else: temp = item['temp'] if 'humidity' not in item: hum = None elif isinstance(item['humidity'], dict): hum = item['humidity']['v'] else: hum = item['humidity'] if 'pressure' not in item: pres = None elif isinstance(item['pressure'], dict): pres = item['pressure']['v'] else: pres = item['pressure'] if 'rain' in item and isinstance(item['rain']['today'], dict): rain = item['rain']['today']['v'] else: rain = None if 'wind' in item and isinstance(item['wind']['speed'], dict): wind = item['wind']['speed']['v'] else: wind = None measurements[item['dt']] = { "temperature": temp, "humidity": hum, "pressure": pres, "rain": rain, "wind": wind } except KeyError: raise parse_response_error.ParseResponseError(__name__ + \ ': impossible to read JSON data') current_time = round(time.time()) return stationhistory.StationHistory(None, None, current_time, measurements)