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 #83 from ReagentX/develop
Browse files Browse the repository at this point in the history
Release/1.2.5
  • Loading branch information
ReagentX committed Apr 13, 2021
2 parents b95d60e + bac87df commit 7b096b5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dist/*

# Requests cache
cache.sqlite
http_cache.sqlite

# Dev env
venv/*
Expand Down
12 changes: 0 additions & 12 deletions purpleair/api_data.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
"""
Constants for PurpleAir API
"""
from datetime import timedelta
from sqlite3 import OperationalError

import requests_cache

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


API_ROOT = 'https://www.purpleair.com/json'
Expand Down
6 changes: 4 additions & 2 deletions purpleair/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import time
from json.decoder import JSONDecodeError
from typing import List, Optional, Union
from datetime import timedelta

import pandas as pd
import requests
from requests_cache import CachedSession

from .api_data import API_ROOT
from .sensor import Sensor
Expand All @@ -33,7 +34,8 @@ def get_all_data(self) -> None:
"""
Get all data from the API
"""
response = requests.get(f'{API_ROOT}?q=""')
session = CachedSession(expire_after=timedelta(hours=1))
response = session.get(f'{API_ROOT}?q=""')
try:
data = json.loads(response.content)
except JSONDecodeError as err:
Expand Down
8 changes: 5 additions & 3 deletions purpleair/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import os
from re import sub
from typing import Optional, List
from datetime import timedelta

import requests
from requests_cache import CachedSession
from geopy.geocoders import Nominatim

from .api_data import API_ROOT
Expand Down Expand Up @@ -56,7 +57,8 @@ def get_data(self) -> Optional[list]:
raise ValueError(f'Invalid sensor ID: {self.identifier}')

# Fetch the JSON for parent and child sensors
response = requests.get(f'{API_ROOT}?show={self.identifier}')
session = CachedSession(expire_after=timedelta(hours=1))
response = session.get(f'{API_ROOT}?show={self.identifier}')
data = json.loads(response.content)
channel_data: Optional[list] = data.get('results')

Expand All @@ -68,7 +70,7 @@ def get_data(self) -> Optional[list]:
except IndexError:
raise IndexError from IndexError(
f'Parent sensor for {self.identifier} does not exist!')
response = requests.get(f'{API_ROOT}?show={parent_id}')
response = session.get(f'{API_ROOT}?show={parent_id}')
data = json.loads(response.content)
channel_data = data.get('results')
elif channel_data and len(channel_data) > 2:
Expand Down
10 changes: 5 additions & 5 deletions requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Usage requirements
requests
requests_cache
thingspeak
geopy
pandas
requests_cache==0.6.0
thingspeak==1.0.0
requests==2.25.1
pandas==1.2.4
geopy==2.1.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='purpleair',
version='1.2.4',
version='1.2.5',
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 7b096b5

Please sign in to comment.