Skip to main content

A simple API for AirVisual air quality data

Project description

☀️ pyairvisual: a thin Python wrapper for the AirVisual© API

CI PyPi Version License Code Coverage Maintainability Say Thanks

pyairvisual is a simple, clean, well-tested library for interacting with AirVisual to retrieve air quality information.

Python Versions

pyairvisual is currently supported on:

  • Python 3.6
  • Python 3.7
  • Python 3.8
  • Python 3.9

Installation

pip install pyairvisual

API Key

You can get an AirVisual API key from the AirVisual API site. Depending on the plan you choose, more functionality will be available from the API:

Community

The Community Plan gives access to:

  • List supported countries
  • List supported states
  • List supported cities
  • Get data from the nearest city based on IP address
  • Get data from the nearest city based on latitude/longitude
  • Get data from a specific city

Startup

The Startup Plan gives access to:

  • List supported stations in a city
  • Get data from the nearest station based on IP address
  • Get data from the nearest station based on latitude/longitude
  • Get data from a specific station

Enterprise

The Enterprise Plan gives access to:

  • Get a global city ranking of air quality

Usage

Using the Cloud API

import asyncio

from pyairvisual import CloudAPI


async def main() -> None:
    """Run!"""
    cloud_api = CloudAPI("<YOUR_AIRVISUAL_API_KEY>")

    # Get data based on the city nearest to your IP address:
    data = await cloud_api.air_quality.nearest_city()

    # ...or get data based on the city nearest to a latitude/longitude:
    data = await cloud_api.air_quality.nearest_city(
        latitude=39.742599, longitude=-104.9942557
    )

    # ...or get it explicitly:
    data = await cloud_api.air_quality.city(
        city="Los Angeles", state="California", country="USA"
    )

    # If you have the appropriate API key, you can also get data based on
    # station (nearest or explicit):
    data = await cloud_api.air_quality.nearest_station()
    data = await cloud_api.air_quality.nearest_station(
        latitude=39.742599, longitude=-104.9942557
    )
    data = await cloud_api.air_quality.station(
        station="US Embassy in Beijing",
        city="Beijing",
        state="Beijing",
        country="China",
    )

    # With the appropriate API key, you can get an air quality ranking:
    data = await cloud_api.air_quality.ranking()

    # pyairvisual gives you several methods to look locations up:
    countries = await cloud_api.supported.countries()
    states = await cloud_api.supported.states("USA")
    cities = await cloud_api.supported.cities("USA", "Colorado")
    stations = await cloud_api.supported.stations("USA", "Colorado", "Denver")


asyncio.run(main())

By default, the library creates a new connection to AirVisual with each coroutine. If you are calling a large number of coroutines (or merely want to squeeze out every second of runtime savings possible), an aiohttp ClientSession can be used for connection pooling:

import asyncio

from aiohttp import ClientSession

from pyairvisual import CloudAPI


async def main() -> None:
    """Run!"""
    async with ClientSession() as session:
        cloud_api = CloudAPI("<YOUR_AIRVISUAL_API_KEY>", session=session)

        # ...


asyncio.run(main())

Working with Node/Pro Units

pyairvisual also allows users to interact with Node/Pro units, both via the cloud API:

import asyncio

from aiohttp import ClientSession

from pyairvisual import CloudAPI


async def main() -> None:
    """Run!"""
    cloud_api = CloudAPI("<YOUR_AIRVISUAL_API_KEY>")

    # The Node/Pro unit ID can be retrieved from the "API" section of the cloud
    # dashboard:
    data = await cloud_api.node.get_by_node_id("<NODE_ID>")


asyncio.run(main())

...or over the local network via Samba (the unit password can be found on the device itself):

import asyncio

from aiohttp import ClientSession

from pyairvisual.node import NodeSamba


async def main() -> None:
    """Run!"""
    async with NodeSamba("<IP_ADDRESS_OR_HOST>", "<PASSWORD>") as node:
        measurements = node.async_get_latest_measurements()

        # Can take some optional parameters:
        #   1. include_trends: include trends (defaults to True)
        #   2. measurements_to_use: the number of measurements to use when calculating
        #      trends (defaults to -1, which means "use all measurements")
        history = node.async_get_history()


asyncio.run(main())

Check out the examples, the tests, and the source files themselves for method signatures and more examples.

Contributing

  1. Check for open features/bugs or initiate a discussion on one.
  2. Fork the repository.
  3. (optional, but highly recommended) Create a virtual environment: python3 -m venv .venv
  4. (optional, but highly recommended) Enter the virtual environment: source ./.venv/bin/activate
  5. Install the dev environment: script/setup
  6. Code your new feature or bug fix.
  7. Write tests that cover your new functionality.
  8. Run tests and ensure 100% code coverage: script/test
  9. Update README.md with any new documentation.
  10. Add yourself to AUTHORS.md.
  11. Submit a pull request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyairvisual-5.0.6.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyairvisual-5.0.6-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file pyairvisual-5.0.6.tar.gz.

File metadata

  • Download URL: pyairvisual-5.0.6.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.9 Linux/5.4.0-1036-azure

File hashes

Hashes for pyairvisual-5.0.6.tar.gz
Algorithm Hash digest
SHA256 9252a37a4c262e9037967e5dc45fe71b4a09df7d6edf4ab8d03561a44e79c6db
MD5 e219164e82bffcf6592a4e674d30f1e8
BLAKE2b-256 189451b7b5b7a70bfd7e39ca532590d5c8eec9a41628299fe7203aecb4d905c5

See more details on using hashes here.

File details

Details for the file pyairvisual-5.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyairvisual-5.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.9 Linux/5.4.0-1036-azure

File hashes

Hashes for pyairvisual-5.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 78ee9c0ba883b35874d26075174abedcaffb7a98c472e85d7e2e4753ff9e7787
MD5 e2fc242c684f5274d50fd893fe2177fd
BLAKE2b-256 821a026664e9fe2136193a30e0f242057b612afcd3ca40134effc382cff073e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page