Beispiel #1
0
def home():

    # Run new image per request to route
    lat_long = aqi_api()
    google_api(lat_long)
    aqi_json = FileIO.read_jsonfile('aqi.json')
    return render_template('index.html', lat_long=lat_long, aqi_json=aqi_json)
Beispiel #2
0
def aqi_api(api_key=None, url=None) -> dict:
    '''
  Air Quality Programmatic APIs
  '''

    if api_key == None and url == None:
        # API Key and URL Default
        api_key = FileIO.read_jsonfile('api_key.json')
        url = "http://api.airvisual.com/v2/nearest_city?key="
        url_aqi = url + api_key['iqair']

    try:
        # Call API
        response = requests.get(url_aqi)
        FileIO.log(str(response.json()), str(type(response.json())))
        request_json = response.json()

        # Handle Bad Request or Process
        if response.status_code == requests.codes.ok:
            aqi_json_file_name = "aqi.json"
            FileIO.json2file(request_json, aqi_json_file_name)

            air_quality_index = FileIO.read_jsonfile(aqi_json_file_name)
            #print("Pollution data", air_quality_index['data']['current']['pollution'])
            FileIO.log("Pollution data",
                       str(air_quality_index['data']['current']['pollution']))

            latitude = air_quality_index['data']['location']['coordinates'][0]
            longitude = air_quality_index['data']['location']['coordinates'][1]
            #print('latitude: ' + str(latitude), 'longitude: ' + str(longitude))
            FileIO.log('latitude: ' + str(latitude),
                       'longitude: ' + str(longitude))

    except Exception as e:
        print('error with request processing!', e)
        FileIO.log('error with request processing!', e)

    lat_lon = {'latitude': str(latitude), 'longitude': str(longitude)}
    return lat_lon