ReagentX / purple_air_api

Python package to get and transform PurpleAir data
https://pypi.org/project/purpleair/
GNU General Public License v3.0
50 stars 19 forks source link

Possible bug with `get_historical()` #100

Closed ReagentX closed 2 years ago

ReagentX commented 2 years ago

User report: https://twitter.com/Frank_Hayes/status/1529034318785421312

I just started using it to look at data from se7488. The parent.get_historical() method with weeks =1 stops on May 10th.

ReagentX commented 2 years ago

Sample code:


from purpleair.sensor import Sensor
s1 = Sensor(7488, parse_location=True)
print(s1) 

# set the number of weeks of data to retrieve
weeks = 1

# Get the primary and secondary data for the first sensor location
df_s1_primary = Sensor(7488).parent.get_historical(weeks_to_get=weeks,thingspeak_field='primary')
df_s1_secondary = Sensor(7488).parent.get_historical(weeks_to_get=weeks,thingspeak_field='secondary')

df_s1_primary
hayesfj commented 2 years ago

Getting an error message now. Here is the traceback if it is any value.

JSONDecodeError Traceback (most recent call last) Untitled-1.ipynb Cell 1' in <cell line: 2>() 1 from purpleair.sensor import Sensor ----> 2 s1 = Sensor(7488, parse_location=True) 3 print(s1)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\purpleair\sensor.py:30, in Sensor.init(self, identifier, json_data, parse_location) 24 def init( 25 self, 26 identifier: int, 27 json_data: Optional[list] = None, 28 parse_location=False): 29 self.data: Optional[list] = json_data \ ---> 30 if json_data is not None else self.get_data(identifier) 32 # Validate the data we received 33 if not self.data:

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\purpleair\sensor.py:75, in Sensor.get_data(self, identifier) 73 session = CachedSession(expire_after=timedelta(hours=1)) 74 response = session.get(f'{API_ROOT}?show={identifier}') ---> 75 data = json.loads(response.content) 76 channel_data: Optional[list] = data.get('results') 78 # Handle various API problems

File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0\lib\json__init.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) [341](file:///c%3A/Program%20Files/WindowsApps/PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0/lib/json/init.py?line=340) s = s.decode(detect_encoding(s), 'surrogatepass') [343](file:///c%3A/Program%20Files/WindowsApps/PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0/lib/json/init.py?line=342) if (cls is None and object_hook is None and 344 parse_int is None and parse_float is None and 345 parse_constant is None and object_pairs_hook is None and not kw): --> 346 return _default_decoder.decode(s) 347 if cls is None: 348 cls = JSONDecoder

File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w) 332 def decode(self, s, _w=WHITESPACE.match): [333](file:///c%3A/Program%20Files/WindowsApps/PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0/lib/json/decoder.py?line=332) """Return the Python representation of s (a str instance 334 containing a JSON document). 335 336 """ --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end() 339 if end != len(s):

File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx) 353 obj, end = self.scan_once(s, idx) [354](file:///c%3A/Program%20Files/WindowsApps/PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64qbz5n2kfra8p0/lib/json/decoder.py?line=353) except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end

JSONDecodeError: Expecting value: line 2 column 1 (char 1)

ReagentX commented 2 years ago

This problem is related to the JSON API deprecation that happened a few days ago. See #69 for tracking that issue.