-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollution_bot.py
301 lines (266 loc) · 10.4 KB
/
pollution_bot.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from apscheduler.schedulers.blocking import BlockingScheduler
import praw
import collections
import time
import urllib.request, json
from praw.models import Message
from praw.models import Comment
from apscheduler.events import EVENT_JOB_ERROR
from apscheduler.events import EVENT_JOB_EXECUTED
print("INITIALIZING.....")
def my_listener(event):
if event.exception:
print('The job crashed :(')
else:
print('The job worked :)')
def pollution():
try:
with urllib.request.urlopen("https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key=API_KEY&format=json&offset=0&limit=500") as url:
data = json.loads(url.read().decode())
json_string = json.dumps(data)
python_obj = json.loads(json_string)
pol_data = {}
covid_string = ""
sample = {
"PM2.5":-1,
"PM10":-1,
"NO2":-1,
"NH3":-1,
"SO2":-1,
"CO":-1,
"OZONE":-1
}
timeofupdate = ""
cou = 0
for item in python_obj["records"]:
station = item["station"]
last_update = item["last_update"]
timeofupdate = last_update
station = station[:-14]
if station == "Lodhi Roa":
station = "Lodhi Road IMD"
if station == "North Campus, D":
station = "North Campus, DU"
if station == "Pus":
station = "Pusa IMD"
if station == "Aya Naga":
station = "Aya Nagar"
if station == "IGI Airport (T3":
station = "IGI Airport (T3)"
if station == "CRRI Mathura Roa":
station = "CRRI Mathura Road"
pollutant_name = item["pollutant_id"]
pollutant_min = "NA"
pollutant_max = "NA"
pollutant_avg = "NA"
if "pollutant_min" in item:
pollutant_min = item["pollutant_min"]
pollutant_max = item["pollutant_max"]
pollutant_avg = item["pollutant_avg"]
city = item["city"]
if(city == "Delhi"):
new_dict = sample.copy()
new_dict[pollutant_name] = pollutant_avg
if station not in pol_data.keys():
pol_data.update({station : new_dict})
else:
pol_data[station][pollutant_name] = new_dict[pollutant_name]
else :
cou = cou + 1
## print("Delhi Exceeded and others excluded")
print("Not Delhi : ",cou)
string = "*DELHI POLLUTION*\n"
string += "\n"
string += "**Area**^[AQIs-pollutant-wise]|**PM2.5**|**PM10**|**NO2**|**NH3**|**SO2**|**CO**|**OZONE**\n-|-|-|-|-|-|-|-\n"
print(pol_data)
for station in pol_data:
if station == "Pusa IMD" or "Punjabi Bagh":
string += station
string += "|"
for keys,value in pol_data[station].items():
if(value==-1):
string += "NA"
else:
string += str(value)
string += "|"
string = string[:-1]
string += "\n"
string += "-\n"
string += "Last updated at: "
string += timeofupdate
string += ".Data from -- Ministry of Environment and Forests , Central Pollution Control Board and covid19india.org.\n"
string += ('^(Beep Boop! I am a Bot.Mention me in comments.) [ ^(My Creator )](https://www.reddit.com/message/compose/?to=Pollution_Hater)')
return string
except Exception as e:
print("ERROR WITH POLLUTION API",e)
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def covid():
try:
with urllib.request.urlopen("https://api.covid19tracker.in/data/static/data.min.json") as url:
cdata = json.loads(url.read().decode())
cjson_string = json.dumps(cdata)
covid_data = json.loads(cjson_string)
covid_string = "*DELHI COVID STATUS*\n"
covid_string += "\n"
covid_string += "**Confirmed**|**Deceased**|**Recovered**|**1st Dose**|**2nd Dose**|**Tested**\n-|-|-|-|-|-\n"
population = covid_data["DL"]["meta"]["population"]
update = str(covid_data["DL"]["meta"]["last_updated"])
for key,value in covid_data["DL"]["total"].items():
if value == -1:
covid_string += "NA"
elif key == "vaccinated1" or key == "vaccinated2":
vax = value/population
covid_string += str(vax)[2:4]
covid_string += "%"
elif value != 0:
covid_string += str(value)
covid_string += "|"
covid_string = covid_string[:-1]
covid_string += "\n"
covid_string += "\n"
covid_string += "Last updated at:"
covid_string += update[:-1]
covid_string += "\nFrom covid19tracker.in "
covid_string += ('^(Beep Boop! I am a Bot.Mention me in comments.) [ ^(My Creator )](https://www.reddit.com/message/compose/?to=Pollution_Hater)')
return covid_string
except Exception as e:
print("ERROR WITH COVID API",e)
def noida_pollution():
try:
with urllib.request.urlopen("https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key=API_KEY&format=json&offset=1400&limit=400") as url:
data = json.loads(url.read().decode())
json_string = json.dumps(data)
python_obj = json.loads(json_string)
pol_data = {}
covid_string = ""
sample = {
"PM2.5":-1,
"PM10":-1,
"NO2":-1,
"NH3":-1,
"SO2":-1,
"CO":-1,
"OZONE":-1
}
timeofupdate = ""
cou = 0
for item in python_obj["records"]:
station = item["station"]
last_update = item["last_update"]
timeofupdate = last_update
station = station[:-14]
if "Greater" in station:
station = station[:-8]
if station != "Sector - 6":
station = station[:-1]
pollutant_name = item["pollutant_id"]
pollutant_min = "NA"
pollutant_max = "NA"
pollutant_avg = "NA"
if "pollutant_min" in item:
pollutant_min = item["pollutant_min"]
pollutant_max = item["pollutant_max"]
pollutant_avg = item["pollutant_avg"]
city = item["city"]
if(city == "Noida" or city == "Greater Noida"):
new_dict = sample.copy()
new_dict[pollutant_name] = pollutant_avg
if station not in pol_data.keys():
pol_data.update({station : new_dict})
else:
pol_data[station][pollutant_name] = new_dict[pollutant_name]
else :
cou = cou + 1
## print("Noida Exceeded and others excluded")
print("Not Noida : ",cou)
string = "*NOIDA POLLUTION*\n"
string += "\n"
string += "**Area**^[AQIs-pollutant-wise]|**PM2.5**|**PM10**|**NO2**|**NH3**|**SO2**|**CO**|**OZONE**\n-|-|-|-|-|-|-|-\n"
print(pol_data)
for station in pol_data:
if station == "Pusa IMD" or "Punjabi Bagh":
string += station
string += "|"
for keys,value in pol_data[station].items():
if(value==-1):
string += "NA"
else:
string += str(value)
string += "|"
string = string[:-1]
string += "\n"
string += "-\n"
string += "Last updated at: "
string += timeofupdate
string += ".Data from -- Ministry of Environment and Forests , Central Pollution Control Board and covid19india.org.\n"
string += ('^(Beep Boop! I am a Bot.Mention me in comments.) [ ^(My Creator )](https://www.reddit.com/message/compose/?to=Pollution_Hater)')
return string
except Exception as e:
print("ERROR WITH NOIDA POLLUTION API",e)
def killpoll():
while True:
time.sleep(70)
reddit = praw.Reddit(client_id='CLIENT_ID',
client_secret='CLIENT_SECRET',
user_agent='airdelhi v1.0 by /u/Pollution_Hater',
username='airdelhi',
password='PASSWORD'
)
string = pollution()
covid_string = covid()
noida_string = noida_pollution()
print("Checking for unread messages ")
for item in reddit.inbox.unread(limit=2):
if isinstance(item, Comment) or isinstance(item,Message):
if hasattr(item,"body"):
print(item.body)
parent = item.parent()
print("parent",parent)
try:
print("FOUND unread MESSAGE !")
if(False):
print("No Ho Ho !")
else:
print("REPLYING !")
try:
item.mark_read()
item.body=item.body.lower()
if "u/airdelhi" in item.body or "aqi" in item.body:
if "covid" in item.body or "corona" in item.body:
try:
item.reply(covid_string)
except Exception as er:
print("COVID EXCEPTION : %s"%(er))
item.mark_unread()
elif "noida" in item.body:
try:
item.reply(noida_string)
except Exception as er:
print("NOIDA COVID EXCEPTION : %s"%(er))
item.mark_unread()
else:
try:
item.reply(string)
except Exception as e:
print("REPLY EXCEPTION:%s"%(e))
item.mark_unread()
else:
print("Not related comment")
except Exception as e:
print("REPLY FAILED:-- %s"%(e))
except Exception as e:
print("MESSAGE SEARCH FAILED: %s"%(e))
print("Exiting Search")
print("killpoll finish")
try:
sched = BlockingScheduler()
sched.add_listener(my_listener, EVENT_JOB_ERROR | EVENT_JOB_EXECUTED)
@sched.scheduled_job('interval', minutes=5)
def timed_job():
print("Scheduler Start")
time.sleep(5)
killpoll()
sched.start()
except Exception as e:
print("Scheduler went wrong: %s"%(e))