-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_report.py
252 lines (183 loc) · 7.69 KB
/
event_report.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
from dhooks import Webhook, File, Embed
import requests
import config
from datetime import datetime
from datetime import timedelta
import os
import time
import traceback
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
hook = Webhook(config.webhook_url)
def make_embed(d,c,t):
embed = Embed(
description=d,
color=c,
timestamp='now' # sets the timestamp to current time
)
return embed
def discord_funky(text:str):
return "```"+text+"```"
def send_within_char_limit(embed,offending_text):
text = offending_text.split(" ")
to_send = ""
sent_before = False
for word in text:
if to_send == "":
to_send = word
else:
to_send = to_send+" "+word
if len(to_send) > 600:
if sent_before:
embed.add_field("",discord_funky(to_send),False)
else:
embed.add_field("Event Text",discord_funky(to_send),False)
sent_before = True
to_send = ""
if to_send != "":
embed.add_field("",discord_funky(to_send),False)
try:
hook.send(embed=embed)
except Exception:
print(traceback.format_exc())
previous_date = ""
def run():
if os.name == "nt": #stink windows fix
current_date = (datetime.today()).strftime('%#m/%#d/%Y')
last_date = (datetime.today() - timedelta(days = 1)).strftime('%#m/%#d/%Y')
else:
current_date = (datetime.today()).strftime('%-m/%-d/%Y')
last_date = (datetime.today() - timedelta(days = 1)).strftime('%-m/%-d/%Y')
try:
request = requests.get("https://www.nrc.gov/reading-rm/doc-collections/event-status/event/en.html")
soup = BeautifulSoup(request.content, 'html.parser')
#search for everything that has <b>
text = soup.find_all(name = "b")
all_text = []
plants = []
for section in text:
extracted_text = section.find_next_sibling(string= True)
#if the text is Event Text, find the next sibling, then find all descendants that are strings
if "Event Text" in section.string:
parent = section.find_next_sibling(class_="border")
parent = parent.find_all(string = True)
extracted_text = ""
for p in parent:
extracted_text = extracted_text+p
if "Person" in section.string:
other_text = section.find_next_siblings(string=True)
extracted_text = ""
for p in other_text:
extracted_text = extracted_text+p
#clean it up
if not "Event Text" in section.string:
extracted_text = extracted_text.replace("\n","")
extracted_text = extracted_text.replace("\r","")
extracted_text = extracted_text.replace("\t","")
#special formatting for 10 CFR
if "Emergency Class" in section.string:
other_text = section.find_next_siblings(string=True)
extracted_text = ""
for p in other_text:
p = p.replace("\n","")
p = p.replace("\r","")
p = p.replace("\t","")
extracted_text = extracted_text+"\n"+p
#if we start a new report
if "Facility" in section.string or "Rep Org" in section.string:
all_text = []
plants.append(all_text)
#this is a power reactor, we can grab the table
if "Unit" in section.string:
rx_table = section.findParent()
rx_table = rx_table.findParent()
rx_table = rx_table.find_next_sibling()
rx_table = rx_table.find_all(string = True)
#remove unnecessary stuff
while "\n" in rx_table:
rx_table.remove("\n")
unit = []
text_list = []
start = False
for t in rx_table:
if t == "1":
start = True
if t in ["2","3"]:
if rx_table[rx_table.index(t)+1] in ["Y","N","y","n"]:
unit.append(text_list)
text_list = []
if start:
text_list.append(t)
if text_list != []:
unit.append(text_list)
all_text.append(unit)
#make it look nicer
if ":" in section.string:
all_text.append(section.string+extracted_text)
else:
all_text.append(section.string+":"+extracted_text)
for plant in plants:
is_power_reactor = False
for text in plant:
if "Unit:" in text:
is_power_reactor = True
#we dont care about non power reactors
if is_power_reactor == False:
plants.pop(plants.index(plant))
#run it twice for shiggles
for plant in plants:
is_power_reactor = False
for text in plant:
if "Unit:" in text:
is_power_reactor = True
#we dont care about non power reactors
if is_power_reactor == False:
plants.pop(plants.index(plant))
#maybe a third times a charm?
for plant in plants:
is_power_reactor = False
for text in plant:
if "Unit:" in text:
is_power_reactor = True
#we dont care about non power reactors
if is_power_reactor == False:
plants.pop(plants.index(plant))
#why did that work
for plant in plants:
embed = make_embed("Event Report",0x00000,"now")
plant_info = ""
emergency_class = ""
notification_info = ""
event_text = ""
unit_info = ""
for text in plant:
#TODO: good formatting
if "Facility:" in text:
plant_info = plant_info+text+"\n"
if "Unit:" in text:
plant_info = plant_info+text+"\n"
if "RX Type:" in text:
plant_info = plant_info+text
if "Emergency Class:" in text:
emergency_class = emergency_class+text
if "Event Date:" in text:
notification_info = notification_info+text+"\n"
if "Event Time:" in text:
notification_info = notification_info+text+"\n"
if "Last Update Date:" in text:
notification_info = notification_info+text
if "Event Text" in text:
event_text = (text.replace("Event Text:",""))
if type(text) == type([]):
unit_info = "Unit|Code|Crit|I PWR|I Mode|C PWR|C Mode\n"
for u in text:
for t in u:
unit_info = unit_info + " " + t
unit_info = unit_info + "\n"
embed.add_field(name = "Plant Info", value = discord_funky(plant_info))
embed.add_field(name = "Notification Info", value = discord_funky(notification_info))
embed.add_field(name = "Emergency Class", value = discord_funky(emergency_class), inline = False)
embed.add_field(name = "Unit Info", value = discord_funky(unit_info), inline = False)
send_within_char_limit(embed,event_text)
except:
time.sleep(10)