Skip to content

Commit

Permalink
Merge pull request #1 from sakethramanujam/v0.3-dev
Browse files Browse the repository at this point in the history
v0.3
  • Loading branch information
sakethramanujam authored Sep 13, 2020
2 parents c9ff082 + 3b82e1a commit 658fa6d
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 50 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Client Tests

on: [pull_request]

jobs:
test:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install dependencies and test
env:
working-directory: cpcbccr-python-client
run: |
python -m pip install --upgrade pip
python setup.py install
pip install pytest
pytest tests/*
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ $ pip install cpcbccr
criteria='24 Hours')
```
> For more supported criteria check `doc-strings`
> For more supported criteria check `doc-string.`
- Save Data

```
>>> cpcb.save_data(path='/path/to/file.csv',
from_date='01-01-2020',
end_date = '02-01-2020',
station_id = 'site_279',
criteria = '24 Hours')
```
> Supported Fileformats: `csv`, `xlsx`, `json`

## License

Expand Down
125 changes: 77 additions & 48 deletions cpcbccr/cpcbccr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import requests
import pandas as pd


API_URL = "https://love-the-air.herokuapp.com/api"
pd.DataFrame()


def get_states() -> List[str]:
Expand All @@ -18,14 +16,11 @@ def get_states() -> List[str]:
>>> states
['Andhra Pradesh','Karnataka', 'Assam',....]
"""
try:
r = requests.get(f'{API_URL}/states')
status = r.status_code
if status != 200:
Exception(f'failed to fetch states {r.status_code}')
return r.json()['states']
except Exception as e:
print(e)
r = requests.get(f'{API_URL}/states')
status = r.status_code
if status != 200:
raise Exception(f'failed to fetch states {r.status_code}')
return r.json()['states']


def get_cities(state: str) -> List[str]:
Expand All @@ -46,14 +41,11 @@ def get_cities(state: str) -> List[str]:
>>> cities
['Eloor', 'Ernakulam', 'Kannur', 'Kochi', 'Kollam', 'Kozhikode', 'Thiruvananthapuram']
"""
try:
r = requests.get(f'{API_URL}/state/{state}')
status = r.status_code
if status != 200:
Exception(f'failed to fetch cities with status:{status}')
return r.json()['cities']
except Exception as e:
print(e)
r = requests.get(f'{API_URL}/state/{state}')
status = r.status_code
if status != 200:
raise Exception(f'failed to fetch cities with status:{status}')
return r.json()['cities']


def get_stations(city: str) -> List[dict]:
Expand All @@ -73,14 +65,11 @@ def get_stations(city: str) -> List[dict]:
>>> stations
[{'id': 'site_5334', 'live': True, 'name': 'Polayathode, Kollam - Kerala PCB'}]
"""
try:
r = requests.get(f'{API_URL}/city/{city}')
status = r.status_code
if status != 200:
Exception(f'failed to fetch stations with status:{status}')
return r.json()['stations']
except Exception as e:
print(e)
r = requests.get(f'{API_URL}/city/{city}')
status = r.status_code
if status != 200:
raise Exception(f'failed to fetch stations with status:{status}')
return r.json()['stations']


def get_data(from_date: str, to_date: str,
Expand Down Expand Up @@ -123,31 +112,71 @@ def get_data(from_date: str, to_date: str,
"station_id": station_id,
"criteria": criteria
}
try:
r = requests.post(f'{API_URL}/data', json=payload)
status = r.status_code
if status == 422:
print(r.json())
elif status == 200:
return _format(json=r.json())
except Exception as e:
print(e)


def _format(json: dict) -> pd.DataFrame:
r = requests.post(f'{API_URL}/data', json=payload)
status = r.status_code
if status == 422:
print(r.json())
elif status == 200:
return _format(json_data=r.json())


def _format(json_data: dict) -> pd.DataFrame:
"""
Return a well formatted Dataframe from json
Parameters
----------
json: json response from request response
json_data: json response from request response
"""
data = json_data['data']
if not data:
raise Exception("API returned empty data")
else:
df = pd.DataFrame(data=data).drop('exceeding', axis=1)
columns = []
for _, col in enumerate(df.columns):
if '_' in col:
col = col.split('_')[-1]
columns.append(col)
df.columns = columns
return df

def save_data(path: str, from_date: str, to_date: str,
station_id: str, criteria: str):
"""
wrapper around get data method to save
data into a file
Parameters
----------
path : path to store file
from_date : str/ISO datetime
Starting Date from which data is required
to_date : str/ISO datetime
End Date until which the date is required
station_id : str
Station Id as listed in the station dictionary
criteria: str
Frequency of data required
Supported Criteria
- 24 Hours
- 8 Hours
- 4 Hours
- 1 Hours
- 30 Minute
- 15 Minute
"""
data = json['data']
df = pd.DataFrame(data=data).drop('exceeding', axis=1)
columns = []
for _, col in enumerate(df.columns):
if '_' in col:
col = col.split('_')[-1]
columns.append(col)
df.columns = columns
return df
formats = ['csv', 'xlsx', 'json']
ff = path.split('.')[-1]
df = get_data(from_date=from_date,
to_date=to_date,
station_id=station_id,
criteria=criteria)
if ff not in formats:
raise Exception(f'FileFormat {ff} Not Supported!')
if ff == 'csv':
df.to_csv(path, index=False)
elif ff == 'excel':
df.to_excel(path, index=False)
elif ff == 'json':
df.to_json(path, index=False)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
description="An unofficial python client to obtain cpcb ccr data",
long_description=readme,
long_description_content_type='text/markdown',
version="0.2",
version="0.3",
python_requires='>=3.6',
install_requires=['requests', 'pandas'],
author='Saketha Ramanujam',
Expand Down
Empty file added tests/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions tests/test_data_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import cpcbccr as cpcb
import pandas as pd
import pytest

expected_data = {'from date': {0: '01-Jan-2020 - 00:00'},
'AT': {0: '15.57'},
'BP': {0: '732.58'},
'Benzene': {0: '8.03'},
'NO': {0: '21.52'},
'NO2': {0: '59.95'},
'NOx': {0: '30.89'},
'Ozone': {0: '28.57'},
'PM2.5': {0: '274.89'},
'RH': {0: '76.24'},
'SO2': {0: '17.72'},
'SR': {0: '58.18'},
'Toluene': {0: '15.94'},
'VWS': {0: '-1.33'},
'WD': {0: '188.71'},
'WS': {0: '0.76'},
'Xylene': {0: '9.93'},
'to date': {0: '02-Jan-2020 - 00:00'}}


def test_get_data_success():
data = cpcb.get_data(from_date='01-01-2020', to_date='01-01-2020',
criteria='24 Hours', station_id='site_273')
assert data.to_dict() == expected_data


def test_get_data_failure():
with pytest.raises(Exception):
cpcb.get_data(from_date='01-01-2020',
criteria='24 Hours', station_id='site_273')

with pytest.raises(Exception):
cpcb.get_data(to_date='01-01-2020',
criteria='24 Hours', station_id='site_273')
with pytest.raises(Exception):
cpcb.get_data(from_date='01-01-2020', to_date='01-01-2020',
criteria='24 Hours')
29 changes: 29 additions & 0 deletions tests/test_get_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cpcbccr as cpcb
import pytest


def test_get_states():
states = cpcb.get_states()
assert len(states) > 0


def test_get_cities_success():
cities = cpcb.get_cities('Assam')
assert cities == ['Guwahati']


def test_get_cities_failure():
with pytest.raises(Exception):
cpcb.get_cities('qwerty')


def test_get_stations_success():
stations = cpcb.get_stations('Guwahati')
assert stations == [{'id': 'site_5073',
'live': True,
'name': 'Railway Colony, Guwahati - APCB'}]


def test_get_stations_failure():
with pytest.raises(Exception):
cpcb.get_stations('qwerty')

0 comments on commit 658fa6d

Please sign in to comment.