Skip to content

Commit 1d816ea

Browse files
committed
Initial commit
0 parents  commit 1d816ea

9 files changed

Lines changed: 317 additions & 0 deletions

File tree

.gitignore

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask stuff:
58+
instance/
59+
.webassets-cache
60+
61+
# Scrapy stuff:
62+
.scrapy
63+
64+
# Sphinx documentation
65+
docs/_build/
66+
67+
# PyBuilder
68+
target/
69+
70+
# Jupyter Notebook
71+
.ipynb_checkpoints
72+
73+
# pyenv
74+
.python-version
75+
76+
# celery beat schedule file
77+
celerybeat-schedule
78+
79+
# SageMath parsed files
80+
*.sage.py
81+
82+
# dotenv
83+
.env
84+
85+
# virtualenv
86+
.venv
87+
venv/
88+
ENV/
89+
90+
# Spyder project settings
91+
.spyderproject
92+
.spyproject
93+
94+
# Rope project settings
95+
.ropeproject
96+
97+
# mkdocs documentation
98+
/site
99+
100+
# mypy
101+
.mypy_cache/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Fabian Affolter
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include LICENSE

README.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
python-luftdaten
2+
================
3+
4+
Python API for interacting with `luftdaten.info <http://luftdaten.info/>`_.
5+
6+
Installation
7+
------------
8+
The module is available from the `Python Package Index <https://pypi.python.org/pypi>`_.
9+
10+
.. code:: bash
11+
12+
$ pip3 install luftdaten
13+
14+
Usage
15+
-----
16+
17+
The file ``example.py`` contains an example about how to use this module.
18+
19+
Development
20+
-----------
21+
For development is recommended to use a ``venv``.
22+
23+
.. code:: bash
24+
25+
$ python3.6 -m venv .
26+
$ source bin/activate
27+
$ python3 setup.py develop
28+
29+
License
30+
-------
31+
``python-luftdaten`` is licensed under MIT, for more details check LICENSE.

example.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Copyright (c) 2017 Fabian Affolter <fabian@affolter-engineering.ch>
3+
4+
Licensed under MIT. All rights reserved.
5+
"""
6+
import asyncio
7+
import aiohttp
8+
9+
from luftdaten import Luftdaten
10+
11+
12+
@asyncio.coroutine
13+
def main():
14+
with aiohttp.ClientSession() as session:
15+
data = Luftdaten(2154, loop, session)
16+
yield from data.async_get_data()
17+
18+
# Print the sensor values
19+
print("Sensor values:", data.values)
20+
21+
# Print the coordinates fo the sensor
22+
print("Location:", data.meta['latitude'], data.meta['longitude'])
23+
24+
25+
loop = asyncio.get_event_loop()
26+
loop.run_until_complete(main())

luftdaten/__init__.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Copyright (c) 2017 Fabian Affolter <fabian@affolter-engineering.ch>
3+
4+
Licensed under MIT. All rights reserved.
5+
"""
6+
import asyncio
7+
import logging
8+
9+
import aiohttp
10+
import async_timeout
11+
12+
from . import exceptions
13+
14+
_LOGGER = logging.getLogger(__name__)
15+
_RESOURCE = 'https://api.luftdaten.info/v1'
16+
17+
VOLUME_MICROGRAMS_PER_CUBIC_METER = 'µg/m3'
18+
19+
SENSOR_TEMPERATURE = 'temperature'
20+
SENSOR_HUMIDITY = 'humidity'
21+
SENSOR_PM10 = 'P1'
22+
SENSOR_PM2_5 = 'P2'
23+
24+
SENSOR_TYPES = {
25+
SENSOR_TEMPERATURE: ['Temperature', '°C'],
26+
SENSOR_HUMIDITY: ['Humidity', '%'],
27+
SENSOR_PM10: ['PM10', VOLUME_MICROGRAMS_PER_CUBIC_METER],
28+
SENSOR_PM2_5: ['PM2.5', VOLUME_MICROGRAMS_PER_CUBIC_METER]
29+
}
30+
31+
32+
class Luftdaten(object):
33+
"""A class for handling connections from Luftdaten.info."""
34+
35+
def __init__(self, sensor_id, loop, session):
36+
"""Initialize the connection."""
37+
self._loop = loop
38+
self._session = session
39+
self.sensor_id = sensor_id
40+
self.data = None
41+
self.values = {
42+
'humidity': None,
43+
'P1': None,
44+
'P2': None,
45+
'pressure': None,
46+
'temperature': None,
47+
}
48+
self.meta = {}
49+
50+
@asyncio.coroutine
51+
def async_get_data(self):
52+
url = '{}/{}/{}/'.format(_RESOURCE, 'sensor', self.sensor_id)
53+
54+
try:
55+
with async_timeout.timeout(5, loop=self._loop):
56+
response = yield from self._session.get(url)
57+
58+
_LOGGER.debug(
59+
"Response from luftdaten.info: %s", response.status)
60+
data = yield from response.json()
61+
_LOGGER.debug(data)
62+
except (asyncio.TimeoutError, aiohttp.ClientError):
63+
_LOGGER.error("Can not load data from luftdaten.info")
64+
raise exceptions.LuftdatenConnectionError()
65+
66+
try:
67+
self.data = data
68+
69+
for sensor_data in self.data:
70+
entry = sensor_data['sensordatavalues'][0]
71+
for measurement in self.values.keys():
72+
if measurement == entry['value_type']:
73+
self.values[measurement] = float(entry['value'])
74+
75+
self.meta['sensor_id'] = self.sensor_id
76+
self.meta['longitude'] = float(data[-1]['location']['longitude'])
77+
self.meta['latitude'] = float(data[-1]['location']['latitude'])
78+
79+
except (TypeError, IndexError):
80+
raise exceptions.LuftdatenError()

luftdaten/exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Copyright (c) 2017 Fabian Affolter <fabian@affolter-engineering.ch>
3+
4+
Licensed under MIT. All rights reserved.
5+
"""
6+
7+
8+
class LuftdatenError(Exception):
9+
"""General LuftdatenError exception occurred."""
10+
11+
pass
12+
13+
14+
class LuftdatenConnectionError(LuftdatenError):
15+
"""When a connection error is encountered."""
16+
17+
pass

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.rst

setup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import sys
5+
6+
try:
7+
from setuptools import setup
8+
except ImportError:
9+
from distutils.core import setup
10+
11+
if sys.argv[-1] == 'publish':
12+
os.system('python3 setup.py sdist upload')
13+
sys.exit()
14+
15+
setup(
16+
name='luftdaten',
17+
version='0.1',
18+
description='Python API for interacting with luftdaten.info.',
19+
url='https://github.com/fabaff/python-luftdatent',
20+
download_url='https://github.com/fabaff/python-luftdaten/releases',
21+
author='Fabian Affolter',
22+
author_email='fabian@affolter-engineering.ch',
23+
license='MIT',
24+
install_requires=['aiohttp'],
25+
packages=['luftdaten'],
26+
zip_safe=True,
27+
classifiers=[
28+
'Development Status :: 3 - Alpha',
29+
'Environment :: Console',
30+
'Intended Audience :: Developers',
31+
'License :: OSI Approved :: MIT License',
32+
'Operating System :: MacOS :: MacOS X',
33+
'Operating System :: Microsoft :: Windows',
34+
'Operating System :: POSIX',
35+
'Programming Language :: Python :: 3.4',
36+
'Topic :: Utilities',
37+
],
38+
)

0 commit comments

Comments
 (0)