-
Notifications
You must be signed in to change notification settings - Fork 0
/
sghaze.py
128 lines (120 loc) · 4.05 KB
/
sghaze.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from sopel.module import commands, example, NOLIMIT
import datetime
import iso8601
import urllib.request, urllib.parse, urllib.error
import json
import re
import time
areas =["North","South","East","West","Central"]
url = 'http://www.nea.gov.sg/anti-pollution-radiation-protection/air-pollution-control/psi/psi-readings-over-the-last-24-hours'
cache_duration= 60 * 30
cached_psi={}
cached_time=None
chosen_area=None
@commands('sghaze(\s+(north|south|east|west|central))?')
@example('.sghaze north')
def sghaze(bot,trigger):
global chosen_area
chosen_area=trigger.group(3)
# print(chosen_area)
result=cachedd_psi()
# print(result)
if chosen_area:
chosen_area=chosen_area.capitalize()
bot.say("Air Quality Index for Singapore "+chosen_area+" district: ")
# bot.say(str(result))
bot.say(rate_result(result[chosen_area]))
else:
bot.say("Air Quality Index for Singapore:")
for key in result:
bot.say(key+": "+rate_result(cached_psi[key]))
@commands('sghazeinfo')
@example('.sghazeinfo')
def sghazelegend(bot,trigger):
bot.say('Air Quality Index Info:')
bot.say('0-50: None')
bot.say('51-100: Moderate')
bot.say('101-200: Unhealthy')
bot.say('201-300: Very Unhealthy')
bot.say('301-400: Hazardous')
bot.say('>400 Beyond Hazardous')
bot.say('More information: https://en.wikipedia.org/wiki/Air_quality_index')
@commands('hazefc\s+(.+)')
def findhazecity(bot,trigger):
city=trigger.group(2)
queryurl="http://sg1.aqicn.org/services/search/?lang=en&key=_1ca%2BQ%1Ff%0D%5D%1F%0D%2B%40H%1D%1C%1B&jsoncallback=waqiloader.jsonp.LJdMztTHefKXTry&s="+city+"&xtra&qid=2"
queryurl2="http://aqicn.org/services/forecast/?city="+city+"&lang=en"
print(queryurl2)
result=urllib.request.urlopen(queryurl).read().decode()
testdata=json.loads(result[result.index('(')+1:-2])['data'][1]
testurl=json.loads(result[result.index('(')+1:-2])['data'][1]['url']
if "faq" in testurl:
testurl=json.loads(result[result.index('(')+1:-2])['data'][2]['url']
result2=json.loads(urllib.request.urlopen(queryurl2).read().decode())
if not "forecast"in result2:
bot.say("Could not find "+city+"! Syntax: .hazefc Country/City")
return
aqiforecast=result2['forecast']['aqi']
displayres={}
currentday=None
curdaystr=""
for elem in aqiforecast:
time=iso8601.parse_date(elem['t'])
if time.date()<=datetime.date.today():
continue
if currentday==None:
currentday=time
if currentday!=time.day:
currentday=time.day
curdaystr=str(time.day)+"."+str(time.month)+"."+str(time.year)
if currentday not in displayres:
displayres[curdaystr]=[]
displayres[curdaystr].append(elem['v'][0])
displayres[curdaystr].append(elem['v'][1])
print(displayres)
bot.say("Air Quality Forecast for "+city+":")
for elem in sorted(displayres):
bot.say(str(elem)+": "+rate_result(min(displayres[elem]))+"/"+rate_result(max(displayres[elem])))
#testurl=firstres['url']
#print(testurl)
def rate_result(resnumber):
if resnumber<=50:
return"None ("+str(resnumber)+")"
if resnumber>50 and resnumber<=100:
return "Moderate ("+str(resnumber)+")"
if resnumber>100 and resnumber<=200:
return "Unhealthy ("+str(resnumber)+")"
if resnumber>200 and resnumber<=300:
return "Very Unhealthy ("+str(resnumber)+")"
if resnumber>300 and resnumber<=400:
return "Hazardous ("+str(resnumber)+")"
if resnumber>400:
return "Beyond Hazardous ("+str(renumber)+")"
def cachedd_psi():
time_now=time.time()
global cached_psi
global cached_time
if cached_psi and cached_time and not time_now-cached_time>cached_duration:
return cached_psi
cached_psi=get_psi()
cache_time=time.time()
return cached_psi
def get_psi():
global chosen_area
psi={}
html=urllib.request.urlopen(url).read().decode()
# print(html)
if chosen_area:
# print(chosen_area)
m = re.search('<strong>'+chosen_area.capitalize()+'<\/strong>.*?<strong[^>]+>(\d+)',html,re.M|re.S)
if m:
psi[chosen_area.capitalize()]=int(m.group(1))
else:
for area in areas:
# print(area)
m = re.search('<strong>'+area+'<\/strong>.*?<strong[^>]+>(\d+)',html,re.M|re.S)
# print(m)
if m:
psi[area]=int(m.group(1))
return psi
#findhazecity("Singapore/North")