weatherbit / weatherbit-python

A python weather api wrapper for weatherbit.io
MIT License
28 stars 9 forks source link

Value Error: 'time data' with 'get_current' function #6

Closed joshsamara closed 2 years ago

joshsamara commented 6 years ago

Overview

Hi -- I'm running into an issue when using the get_current function.

Looks like when called it's always raising a Value Error because it's expecting some time data from a point to be formatted differently than it is.

I'm not sure if this is an issue with the lib, the data being returned from the api, or my setup.

Example Reproduction

api = Api(api_key)
api.get_current(city="Boston", state="Massachusetts", country="US")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/api.py", line 157, in get_current
    return self._make_request(url, self._parse_current)
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/api.py", line 221, in _make_request
    return callback(request_url)
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/api.py", line 214, in _parse_current
    return Current(json, weatherbitio_reponse, headers)
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/models.py", line 69, in __init__
    self._load(self.json)
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/models.py", line 84, in _load
    self._load_from_points(response['data'])
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/models.py", line 88, in _load_from_points
    self.points.append(SingleTimePoint(point))
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/models.py", line 148, in __init__
    self.sunrise = self._get_date_from_timestamp(point.get('sunrise'), True)
  File "/Users/josh/.virtualenvs/weather-xOXsiLtW/lib/python3.6/site-packages/weatherbit/models.py", line 162, in _get_date_from_timestamp
    return datetime.datetime.strptime(datestamp, date_format)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '09:42' does not match format '%H:%M:%S'

I've tried a few other cities like "New York" and "Seattle" with the same issue.

Looks like this error isn't getting hit when using other functions like get_forecast.

Specifications

tombresson commented 6 years ago

Yeah, I'm seeing the same thing. The JSON data for the sunrise and sunset parameters are coming back from Weatherbit in the format %H:%M which isn't being handled by the parsing code and is failing out.

Here's a sample of the JSON data:

{
   "data":[
      {
         "wind_cdir":"WNW",
         "rh":93,
         "pod":"n",
         "lon":-81.8418,
         "pres":980.3,
         "timezone":"America\/New_York",
         "ob_time":"2018-08-08 02:28",
         "country_code":"US",
         "clouds":25,
         "vis":10,
         "state_code":"OH",
         "wind_spd":3.09,
         "lat":41.23811,
         "wind_cdir_full":"west-northwest",
         "slp":1013.7,
         "datetime":"2018-08-08:02",
         "ts":1533695280,
         "station":"E2886",
         "h_angle":-90,
         "dewpt":21,
         "uv":0,
         "dni":0,
         "wind_dir":290,
         "elev_angle":-14.3819,
         "ghi":0,
         "dhi":0,
         "precip":null,
         "city_name":"Brunswick",
         "weather":{
            "icon":"c01n",
            "code":"800",
            "description":"Clear sky"
         },
         "sunset":"00:35",
         "temp":22.2,
         "sunrise":"10:29",
         "app_temp":23
      }
   ],
   "count":1
}

https://github.com/weatherbit/weatherbit-python/blob/6afb41e30c4f915814ce307a70f72147d7d4ea19/weatherbit/models.py#L157-L159

Looking at the code, the only time the "%H:%M:%S" format is being invoked is for the sunrise/sunset. I think that date_format string can be changed to just be "%H:%M" and it would resolve this issue.