☀️ A simple, clean, well-tested Python library for interacting with AirVisual©
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
pyairvisual
tests
.codeclimate.yml
.coveragerc
.gitignore
.travis.yml
AUTHORS.md
LICENSE
Makefile
Pipfile
README.md
example.py
pylintrc
setup.py
tox.ini

README.md

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

Travis 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.

PLEASE READ: Version 2.0.0 and Beyond

Version 2.0.0 of pyairvisual makes several breaking, but necessary changes:

  • Moves the underlying library from Requests to aiohttp
  • Changes the entire library to use asyncio
  • Makes 3.6 the minimum version of Python required

If you wish to continue using the previous, synchronous version of pyairvisual, make sure to pin version 1.0.0.

Python Versions

pyairvisual is currently supported on:

  • Python 3.5
  • Python 3.6
  • Python 3.7

However, running the test suite currently requires Python 3.6 or higher; tests run on Python 3.5 will fail.

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

pyairvisual starts within an aiohttp ClientSession:

import asyncio

from aiohttp import ClientSession

from pyairvisual import Client


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as websession:
      # YOUR CODE HERE


asyncio.get_event_loop().run_until_complete(main())

Create a client and get to work:

import asyncio

from aiohttp import ClientSession

from pyairvisual import Client


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as websession:
      client = Client('<YOUR AIRVISUAL API KEY>', websession)

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

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

      # ...or get it explicitly:
      data = await client.data.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 client.data.nearest_station()
      data = await client.data.nearest_station(
        latitude=39.742599, longitude=-104.9942557)
      data = await client.data.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 client.data.ranking()

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


asyncio.get_event_loop().run_until_complete(main())

Check out example.py, 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. Install the dev environment: make init.
  4. Enter the virtual environment: pipenv shell
  5. Code your new feature or bug fix.
  6. Write a test that covers your new functionality.
  7. Run tests and ensure 100% code coverage: make coverage
  8. Add yourself to AUTHORS.md.
  9. Submit a pull request!