How to use the pyowm.exceptions.parse_response_error.ParseResponseError function in pyowm

To help you get started, we’ve selected a few pyowm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github csparpa / pyowm / tests / unit / webapi25 / test_observationlistparser.py View on Github external
def test_parse_JSON_fails_when_JSON_data_is_None(self):
        self.assertRaises(ParseResponseError, ObservationListParser.parse_JSON,
                          self.__instance, None)
github csparpa / pyowm / tests / unit / webapi25 / parsers / test_so2indexparser.py View on Github external
def test_parse_JSON_fails_with_malformed_JSON_data(self):
        self.assertRaises(ParseResponseError, SO2IndexParser.parse_JSON,
                          self.__instance, SO2INDEX_MALFORMED_JSON)
github csparpa / pyowm / tests / unit / pollutionapi30 / test_parsers.py View on Github external
def test_parse_JSON_fails_with_malformed_JSON_data(self):
        self.assertRaises(ParseResponseError, SO2IndexParser.parse_JSON,
                          self.__instance, SO2INDEX_MALFORMED_JSON)
github csparpa / pyowm / tests / unit / commons / test_http_client.py View on Github external
def test_get_json_parse_error(self):

        def monkey_patched_get(uri, params=None, headers=None, timeout=None,
                               verify=False):
            return MockResponse(200, 123846237647236)

        requests.get = monkey_patched_get
        self.assertRaises(parse_response_error.ParseResponseError,
                          HttpClient().get_json,
                          'http://anyurl.com',
                          params=dict(a=1, b=2))
        requests.get = self.requests_original_get
github csparpa / pyowm / pyowm / webapi25 / jsonparser.py View on Github external
'temp_min': temp_min
                           }
        else:
            temperature = {}
        if 'weather' in d:
            status = d['weather'][0]['main'].lower() #Sometimes provided with a leading upper case!
            detailed_status = d['weather'][0]['description'].lower()
            weather_code = d['weather'][0]['id']
            weather_icon_name = d['weather'][0]['icon']
        else:
            status = u''
            detailed_status = u''
            weather_code = 0
            weather_icon_name = u''
    except KeyError:
        raise ParseResponseError(''.join([__name__,': impossible to read ' \
              'weather info from JSON data', linesep, str(d)]))
    else:
        return Weather(reference_time, sunset_time, sunrise_time, clouds,
                    rain, snow, wind, humidity, pressure, temperature, 
                    status, detailed_status, weather_code, 
                    weather_icon_name)
github Fuyukai / curiosity / curiosity / plugins / misc.py View on Github external
async def forecast(self, ctx: Context, *, place: str):
        """
        Shows the 3 hour weather forecast for the specified place.
        """
        async with ctx.channel.typing:
            try:
                result = await curio.abide(self._owm.daily_forecast, place, 1)  # type: Forecaster
            except (APICallError, APIResponseError) as e:
                await ctx.channel.send(":x: Error fetching results. Does this place exist?")
                return
            except ParseResponseError as e:
                await ctx.channel.send(":x: OWM is being stupid, please retry your request.")
                return

            forecast = result.get_forecast()  # type: Forecast
            location = forecast.get_location()  # type: Location

            fmtted = "{}, {}".format(location.get_name(), location.get_country())

            weather = forecast.get_weathers()[0]  # type: Weather

            status = weather.get_detailed_status().capitalize()

            # COPY PASTE O CLOCK
            temperature = weather.get_temperature("celsius")
            rain = weather.get_rain()
            if rain:
github csparpa / pyowm / pyowm / webapi25 / stationparser.py View on Github external
def parse_JSON(self, JSON_string):
        """
        Parses a *Station* 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 *Station* 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)
        try:
            name = d['station']['name']
            station_ID = d['station']['id']
            station_type = d['station']['type']
            status = d['station']['status']
            lat = d['station']['coord']['lat']
            if 'lon' in d['station']['coord']:
                lon = d['station']['coord']['lon']
            elif 'lng' in d['station']['coord']:
                lon = d['station']['coord']['lng']
            else:
                lon = None
            if 'distance' in d:
                distance = d['distance']
            else:
github csparpa / pyowm / pyowm / stationsapi30 / parsers / station_parser.py View on Github external
def parse_JSON(self, JSON_string):
        """
        Parses a *pyowm.stationsapi30.station.Station* instance out of raw JSON
        data.

        :param JSON_string: a raw JSON string
        :type JSON_string: str
        :return: a *pyowm.stationsapi30.station.Station* 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

        """
        if JSON_string is None:
            raise parse_response_error.ParseResponseError('JSON data is None')
        d = json.loads(JSON_string)
        try:
            id = d.get('ID', None) or d.get('id', None)
            external_id = d.get('external_id', None)
            lon = d.get('longitude', None)
            lat = d.get('latitude', None)
            alt = d.get('altitude', None)
        except KeyError as e:
            raise parse_response_error.ParseResponseError('Impossible to parse JSON: %s' % e)
        name = d.get('name', None)
        rank = d.get('rank', None)
        created_at = d.get('created_at', None)
        updated_at = d.get('updated_at', None)
        return Station(id, created_at, updated_at, external_id, name, lon, lat,
                       alt, rank)
github csparpa / pyowm / pyowm / webapi25 / parsers / ozoneparser.py View on Github external
def parse_JSON(self, JSON_string):
        """
        Parses an *Ozone* 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 *Ozone* 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)
        try:
            # -- reference time (strip away Z and T on ISO8601 format)
            ref_t = d['time'].replace('Z', '+00').replace('T', ' ')
            reference_time = timeformatutils._ISO8601_to_UNIXtime(ref_t)

            # -- reception time (now)
            reception_time = timeutils.now('unix')

            # -- location
            lon = float(d['location']['longitude'])
            lat = float(d['location']['latitude'])
            place = location.Location(None, lon, lat, None)

            # -- ozone Dobson Units value
            du = d['data']