|
1 | | -import requests |
2 | | -import sys |
3 | 1 | import getopt |
| 2 | +import sys |
4 | 3 |
|
| 4 | +import requests |
5 | 5 |
|
6 | | -def main(argv): |
7 | 6 |
|
| 7 | +def main(argv): |
8 | 8 | def usage(): |
9 | | - print('usage: %s [-c city] [-a accesstoken] [-h help] ...') \ |
10 | | - % argv[0] |
| 9 | + print("usage: %s [-c city] [-a accesstoken] [-h help] ...") % argv[0] |
11 | 10 | return 100 |
12 | 11 |
|
13 | 12 | try: |
14 | | - (opts, args) = getopt.getopt(argv[1:], 'c:a:h') |
| 13 | + (opts, args) = getopt.getopt(argv[1:], "c:a:h") |
15 | 14 | except getopt.GetoptError: |
16 | 15 | return usage() |
17 | 16 |
|
18 | 17 | def help(): |
19 | | - print("AQI Tracker -" |
20 | | - "\tLets the user know the real-time air quality values" |
21 | | - "\t\t(usage: %s [-c city] [-a accesstoken] [-h help])" |
22 | | - "\ncity:\t\tname of the cit" |
23 | | - "\naccesstoken:\tYou need to get access token by " |
24 | | - "registering on http://aqicn.org/data-platform/register/" |
25 | | - "\n\t\t*if no accesstoken provided then demo accesstoken will" |
26 | | - " be used \n\t\tand with demo access token only Shanghai's" |
27 | | - " air quality values can be retrieved.") |
28 | | - |
29 | | - city = 'shanghai' |
30 | | - accesstoken = 'demo' |
| 18 | + print( |
| 19 | + "AQI Tracker -" |
| 20 | + "\tLets the user know the real-time air quality values" |
| 21 | + "\t\t(usage: %s [-c city] [-a accesstoken] [-h help])" |
| 22 | + "\ncity:\t\tname of the cit" |
| 23 | + "\naccesstoken:\tYou need to get access token by " |
| 24 | + "registering on http://aqicn.org/data-platform/register/" |
| 25 | + "\n\t\t*if no accesstoken provided then demo accesstoken will" |
| 26 | + " be used \n\t\tand with demo access token only Shanghai's" |
| 27 | + " air quality values can be retrieved." |
| 28 | + ) |
| 29 | + |
| 30 | + city = "shanghai" |
| 31 | + accesstoken = "demo" |
31 | 32 |
|
32 | 33 | for (k, v) in opts: |
33 | | - if k == '-c': |
| 34 | + if k == "-c": |
34 | 35 | city = v |
35 | | - elif k == '-a': |
| 36 | + elif k == "-a": |
36 | 37 |
|
37 | 38 | accesstoken = v |
38 | | - elif k == '-h': |
| 39 | + elif k == "-h": |
39 | 40 |
|
40 | 41 | return help() |
41 | 42 |
|
42 | | - url = 'http://api.waqi.info/feed/' + city + '/?token=' + accesstoken |
43 | | - print('URL: ', url) |
| 43 | + url = "http://api.waqi.info/feed/" + city + "/?token=" + accesstoken |
| 44 | + print("URL: ", url) |
44 | 45 |
|
45 | | - r = requests.get(url, auth=('user', 'pass')) |
| 46 | + r = requests.get(url, auth=("user", "pass")) |
46 | 47 |
|
47 | 48 | if r.status_code == 200: |
48 | 49 | data = r.json() |
49 | | - value = data['data']['iaqi']['pm25']['v'] |
| 50 | + value = data["data"]["iaqi"]["pm25"]["v"] |
50 | 51 | toDisplay = str(value) |
51 | 52 |
|
52 | 53 | if value > 0 and value < 50: |
53 | | - print('Air Quality Alert ->') |
54 | | - print('Current Value: Healthy - ' + toDisplay) |
| 54 | + print("Air Quality Alert ->") |
| 55 | + print("Current Value: Healthy - " + toDisplay) |
55 | 56 | elif value > 50 and value < 100: |
56 | | - print('Air Quality Alert ->') |
57 | | - print('Current Value: Moderate - ' + toDisplay) |
| 57 | + print("Air Quality Alert ->") |
| 58 | + print("Current Value: Moderate - " + toDisplay) |
58 | 59 | elif value > 100 and value < 150: |
59 | | - print('Air Quality Alert ->') |
60 | | - print('Current Value: Sensitive - ' + toDisplay) |
| 60 | + print("Air Quality Alert ->") |
| 61 | + print("Current Value: Sensitive - " + toDisplay) |
61 | 62 | elif value > 150 and value < 200: |
62 | | - print('Air Quality Alert ->') |
63 | | - print('Current Value: UnHealhty - ' + toDisplay) |
| 63 | + print("Air Quality Alert ->") |
| 64 | + print("Current Value: UnHealhty - " + toDisplay) |
64 | 65 | elif value > 200 and value < 250: |
65 | | - print('Air Quality Alert ->') |
66 | | - print('Current Value: UnHealthy - ' + toDisplay) |
| 66 | + print("Air Quality Alert ->") |
| 67 | + print("Current Value: UnHealthy - " + toDisplay) |
67 | 68 | elif value > 250 and value > 300: |
68 | | - print('Air Quality Alert ->') |
69 | | - print('Current Value: Hazardous - ' + toDisplay) |
| 69 | + print("Air Quality Alert ->") |
| 70 | + print("Current Value: Hazardous - " + toDisplay) |
70 | 71 | else: |
71 | 72 |
|
72 | | - print('Error: Unable to connect to server') |
| 73 | + print("Error: Unable to connect to server") |
73 | 74 |
|
74 | 75 |
|
75 | | -if __name__ == '__main__': |
| 76 | +if __name__ == "__main__": |
76 | 77 | sys.exit(main(sys.argv)) |
0 commit comments