aboutsummaryrefslogtreecommitdiff
path: root/scripts/aqi.py
blob: 0f29a43a3313939495b6efe0e6bf4b66b1b4a204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import requests
import json
import datetime
import sys
from prettytable import PrettyTable
import argparse


log = "true"


def supports_color():
	plat = sys.platform
	global blue, green, red, yellow, cyan
	supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
	is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
	if not supported_platform or not is_a_tty:
		blue = lambda text: text
		green = lambda text: text
		red = lambda text: text
		cyan = lambda text: text
		yellow = lambda text: text
	blue = lambda text: '\033[0;34m' + text + '\033[0m'
	green = lambda text: '\033[0;32m' + text + '\033[0m'
	red = lambda text: '\033[0;31m' + text + '\033[0m'
	yellow = lambda text: '\033[0;33m' + text + '\033[0m'
	cyan = lambda text: '\033[0;36m' + text + '\033[0m'
def logging():
	if(log == "true"):
		print(blue("[LOG] " + str(datetime.datetime.now())))
def getResponseFromWAQI():
	params = (('token', '41ecfdf888497be166376a6bedd6dd5807cc9f4e'),)
	response = requests.get('http://api.waqi.info/feed/' + myLocation + "/", params=params)
	return response
def getAQI():
	response = getResponseFromWAQI()
	if(response.json()['status'] != 200):
		logging()
		global aqi
		aqi = response.json()['data']['aqi']
		print(green("[LOG] Good Response"))
		return aqi

	else:
		logging()
		print("Bad Response")
def getCondition(quality):
	if(quality == 1):
		condition = red("Hazardous")
		return condition
	elif(quality == 2):
		condition = red("Very Unhealthy")
		return condition
	elif(quality == 3):
		condition = red("Unhealthy")
		return condition
	elif(quality == 4):
		condition = yellow("Unhealthy for sensitive people")
		return condition
	elif(quality == 5):
		condition = yellow("Moderate")
		return condition
	elif(quality == 6):
		condition = green("Good")
		return condition
def getDominantPol():
	response = getResponseFromWAQI()
	dominantpollutant = response.json()['data']['dominentpol']
	return dominantpollutant
def getQuality(aqi):
	if(aqi <= 50):
		quality = 6
		return quality
	elif(aqi <= 100):
		quality = 5
		return quality
	elif(aqi <= 150):
		quality = 4
		return quality
	elif(aqi <= 200):
		quality = 3
		return quality
	elif(aqi <= 300):
		quality = 2
		return quality
	elif(aqi <= 301):
		quality = 1
		return quality
def prettyPrint(aqi,quality,condition, dominantpollutant, myLocation):
	table = PrettyTable(['Name','Value'])
	table.add_row([blue('Location'), cyan(myLocation)])
	table.add_row([blue('AQI'),aqi])
	table.add_row([blue('Quality'),cyan(str(quality) + '/6')])
	table.add_row([blue('Dominant Pollutant'), red(dominantpollutant)])
	table.add_row([blue('Condition'),condition])
	print(table)


parser = argparse.ArgumentParser()
parser.add_argument("location", help="fetch the data for a patricular location", type=str)
argument = parser.parse_args()
myLocation = argument.location


supports_color()
aqi = getAQI()
quality = getQuality(aqi)
condition = getCondition(quality)
dominantpollutant = getDominantPol()


prettyPrint(aqi, quality, condition, dominantpollutant, myLocation)