Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from ReagentX/develop
Browse files Browse the repository at this point in the history
Release/1.0.4
  • Loading branch information
ReagentX authored Sep 11, 2020
2 parents 0def2ba + 64340f2 commit 2310178
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
12 changes: 12 additions & 0 deletions purpleair/api_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
"""
Constants for PurpleAir API
"""
from datetime import timedelta
from sqlite3 import OperationalError

import requests_cache

# Setup cache for requests
requests_cache.install_cache(expire_after=timedelta(hours=1))
try:
requests_cache.core.remove_expired_responses()
except OperationalError:
print('Unable to purge cache database, cache may contain old data!!!')


API_ROOT = 'https://www.purpleair.com/json'
18 changes: 8 additions & 10 deletions purpleair/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@


import json
from datetime import timedelta
from json.decoder import JSONDecodeError

import pandas as pd
import requests
import requests_cache

from .api_data import API_ROOT
from .sensor import Sensor

# Setup cache for requests
requests_cache.install_cache(expire_after=timedelta(hours=1))
requests_cache.core.remove_expired_responses()


class SensorList():
"""
Expand All @@ -41,7 +35,8 @@ def get_all_data(self):
try:
data = json.loads(response.content)
except JSONDecodeError as err:
raise ValueError('Invalid JSON data returned from network!') from err
raise ValueError(
'Invalid JSON data returned from network!') from err
print(f"Initialized {len(data['results']):,} sensors!")
self.data = data['results']

Expand All @@ -53,10 +48,13 @@ def to_dataframe(self, sensor_group: str) -> pd.DataFrame:
if sensor_group not in {'useful', 'outside', 'all'}:
raise ValueError(f'{sensor_group} is an invalid sensor group!')
if sensor_group == 'all':
sensor_data = pd.DataFrame([s.as_flat_dict() for s in self.all_sensors])
sensor_data = pd.DataFrame([s.as_flat_dict()
for s in self.all_sensors])
elif sensor_group == 'outside':
sensor_data = pd.DataFrame([s.as_flat_dict() for s in self.outside_sensors])
sensor_data = pd.DataFrame([s.as_flat_dict()
for s in self.outside_sensors])
elif sensor_group == 'useful':
sensor_data = pd.DataFrame([s.as_flat_dict() for s in self.useful_sensors])
sensor_data = pd.DataFrame([s.as_flat_dict()
for s in self.useful_sensors])
sensor_data.index = sensor_data.pop('id')
return sensor_data
10 changes: 0 additions & 10 deletions purpleair/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@
import json
from typing import Optional
from datetime import datetime, timedelta
from sqlite3 import OperationalError

import pandas as pd
import requests
import requests_cache
import thingspeak
from geopy.geocoders import Nominatim

from .api_data import API_ROOT

# Setup cache for requests
try:
requests_cache.install_cache(expire_after=timedelta(hours=1))
requests_cache.core.remove_expired_responses()
except OperationalError:
print('Unable to open cache database, requests will not be cached!!!')


class Sensor():
"""
Class for a single PurpleAir sensor; set initialize=True to fetch data from the API
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='purpleair',
version='1.0.3',
version='1.0.4',
description='Python API Client to get and transform PurpleAir data.',
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 2310178

Please sign in to comment.