-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
irma.py
573 lines (491 loc) · 19 KB
/
irma.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
from base_scraper import BaseScraper
from irma_shelters import (
IrmaShelters,
IrmaShelterDupes,
IrmaSheltersFloridaMissing,
)
from gis_scrapers import (
FemaOpenShelters,
FemaNSS,
GemaAnimalShelters,
GemaActiveShelters,
)
from nyc import (
NewYorkShelters,
)
from north_bay import (
CaliforniaDOTRoadInfo,
SantaRosaEmergencyInformation,
SonomaRoadConditions,
CaliforniaHighwayPatrolIncidents,
PGEOutagesIndividual,
)
from BeautifulSoup import BeautifulSoup as Soup
import requests
import os
import sys
import time
import json
import datetime
import zipfile
import StringIO
from xml.etree import ElementTree
class GoogleCrisisKmlScraper(BaseScraper):
url = 'https://www.google.com/maps/d/u/1/kml?mid=1fJ4NZ21YW1Ru856hehpufId79CA&ll=22.47126398588183%2C-60.6005859375&z=5&cm.ttl=600'
source_url = 'http://google.org/crisismap/2017-irma'
filepath = 'google-crisis-irma-2017.json'
def create_message(self, new_data):
return self.update_message([], new_data, verb='Created')
def update_message(self, old_data, new_data, verb='Updated'):
def name(n):
if 'Name' not in n:
return None
return ('%s (%s)' % (
n['Name'], n.get('City, State/Province') or ''
)).replace(' ()', '')
current_names = [name(n) for n in new_data if name(n)]
previous_names = [name(n) for n in old_data if name(n)]
message = update_message_from_names(
current_names,
previous_names,
self.filepath,
verb=verb
)
message += '\nChange detected on %s' % self.source_url
return message
def fetch_data(self):
zipped = requests.get(self.url).content
zipdata = zipfile.ZipFile(StringIO.StringIO(zipped))
kml = zipdata.open('doc.kml').read()
et = ElementTree.fromstring(kml)
shelters = []
for placemark in et.findall('.//{http://www.opengis.net/kml/2.2}Placemark'):
shelter = {}
for data in placemark.findall('{http://www.opengis.net/kml/2.2}ExtendedData/{http://www.opengis.net/kml/2.2}Data'):
key = data.attrib['name']
value = ''.join(s.strip() for s in data.itertext())
shelter[key] = value
coords = placemark.find('.//{http://www.opengis.net/kml/2.2}coordinates').text.strip()
longitude, latitude, _ = coords.split(',')
shelter.update({
'latitude': latitude,
'longitude': longitude,
})
if 'Phone' in shelter:
# They come through in scientific number format for some reason
shelter['Phone'] = shelter['Phone'].replace('.', '').replace('E9', '')
shelters.append(shelter)
return shelters
class SouthCarolinaShelters(BaseScraper):
url = 'http://scemd.org/ShelterStatus.html'
filepath = 'scemd-shelters.json'
def create_message(self, new_data):
return self.update_message([], new_data, verb='Created')
def update_message(self, old_data, new_data, verb='Updated'):
def name(n):
return '%s (%s County, SC)' % (
n['Shelter Name'], n['County']
)
current_names = [name(n) for n in new_data]
previous_names = [name(n) for n in old_data]
message = update_message_from_names(
current_names,
previous_names,
self.filepath,
verb=verb
)
message += '\nChange detected on %s' % self.url
return message
def fetch_data(self):
s = Soup(requests.get(self.url).content)
table = s.find('table')
trs = table.findAll('tr')
headings = [
th.getText()
for th in trs[0].findAll('th')
]
shelters = []
for tr in trs[1:]:
content = [td.getText() for td in tr.findAll('td')]
shelters.append(dict(zip(headings, content)))
return shelters
class ZeemapsScraper(BaseScraper):
url = 'https://zeemaps.com/emarkers?g=2682928'
filepath = 'zeemaps-2682928.json'
slack_channel = None
def fetch_data(self):
data = requests.get(self.url).json()
data.sort(key=lambda d: d['nm'])
return data
class FplStormOutages(BaseScraper):
filepath = 'fpl-storm-outages.json'
url = 'https://www.fplmaps.com/data/storm-outages.js'
slack_channel = None
def fetch_data(self):
content = requests.get(
self.url,
timeout=10,
).content
# Stripe the 'define(' and ');'
if content.startswith('define('):
content = content.split('define(')[1]
if content.endswith(');'):
content = content.rsplit(');', 1)[0]
return json.loads(content)
class FplCountyOutages(BaseScraper):
filepath = 'fpl-county-outages.json'
url = 'https://www.fplmaps.com/customer/outage/CountyOutages.json'
slack_channel = None
def fetch_data(self):
return requests.get(
self.url,
timeout=10,
).json()
class ScegOutages(BaseScraper):
filepath = 'sceg-outages.json'
url = 'https://www.sceg.com/scanapublicservice/outagemapdata/gismapdataonly.aspx?gisUrl=OUTAGE_EX/Outage_EX&gisMapLayer=6'
source_url = 'https://www.sceg.com/outages-emergencies/power-outages/outage-map'
slack_channel = None
def fetch_data(self):
data = requests.get(self.url).json()
return [feature['attributes'] for feature in data['features']]
class GeorgiaOutages(BaseScraper):
filepath = 'georgiapower-outages.json'
url = 'http://outagemap.georgiapower.com/external/data/interval_generation_data/2017_09_12_00_59_50/thematic/thematic_areas.js?timestamp='
slack_channel = None
def fetch_data(self):
url = self.url + str(int(time.time()))
return requests.get(url).json()
class NorthGeorgiaOutages(BaseScraper):
filepath = 'north-georgia-outages.json'
url = 'http://www2.ngemc.com:81/api/weboutageviewer/get_live_data'
slack_channel = None
def fetch_data(self):
return requests.get(self.url).json()
class TampaElectricOutages(BaseScraper):
filepath = 'tampa-electric-outages.json'
url = 'http://www.tampaelectric.com/residential/outages/outagemap/datafilereader/index.cfm'
slack_channel = None
def fetch_data(self):
return requests.get(
self.url,
headers={
'Referer': 'http://www.tampaelectric.com/residential/outages/outagemap/',
}
).json()['markers']
class JemcOutages(BaseScraper):
filepath = 'jemc-outages.json'
url = 'https://jemc.maps.sienatech.com/data/outages.xml'
slack_channel = None
def fetch_data(self):
et = ElementTree.fromstring(requests.get(self.url).content)
reports = et.find('reports').findall('report')
data = {}
for report in reports:
id = report.attrib['id']
keys = [d.attrib['key'] for d in report.findall('dimension/dim')]
rows = report.findall('dataset/t')
results = [
dict(zip(keys, [e.text for e in row]))
for row in rows
]
data[id] = results
return data
class BaseDukeScraper(BaseScraper):
slack_channel = None
def fetch_data(self):
metadata_url = 'https://s3.amazonaws.com/outagemap.duke-energy.com/data/%s/external/interval_generation_data/metadata.xml?timestamp=%d' % (
self.state_code, int(time.time())
)
metadata = requests.get(metadata_url).content
directory = metadata.split('<directory>')[1].split('</directory>')[0]
data_url = 'https://s3.amazonaws.com/outagemap.duke-energy.com/data/%s/external/interval_generation_data/%s/thematic/thematic_areas.js?timestamp=%d' % (
self.state_code, directory, int(time.time())
)
return requests.get(data_url).json()
class DukeFloridaOutages(BaseDukeScraper):
filepath = 'duke-fl-outages.json'
state_code = 'fl'
class DukeCarolinasOutages(BaseDukeScraper):
filepath = 'duke-ncsc-outages.json'
state_code = 'ncsc'
class PascoCounty(BaseScraper):
# From http://www.pascocountyfl.net/index.aspx?NID=2816
# in particular this iframe:
# https://secure.pascocountyfl.net/sheltersdisplay
filepath = 'pascocountyfl.json'
url = 'https://secure.pascocountyfl.net/SheltersDisplay/Home/GetShelterInfo'
def create_message(self, new_data):
return self.update_message([], new_data, verb='Created')
def update_message(self, old_data, new_data, verb='Updated'):
def name(n):
return '%s (Pasco County FL)' % n['Name']
current_names = [name(n) for n in new_data]
previous_names = [name(n) for n in old_data]
message = update_message_from_names(
current_names,
previous_names,
self.filepath,
verb=verb
)
message += '\nChange detected on http://www.pascocountyfl.net/index.aspx?NID=2816'
return message
def fetch_data(self):
data = requests.post(self.url).json()
data.sort(key=lambda d: d['Name'])
return data
class LedgerPolkCounty(BaseScraper):
filepath = 'ledger-polk-county.json'
url = 'http://www.ledgerdata.com/hurricane-guide/shelter/'
def create_message(self, new_data):
return self.update_message([], new_data, verb='Created')
def update_message(self, old_data, new_data, verb='Updated'):
current_names = [n['name'] for n in new_data]
previous_names = [n['name'] for n in old_data]
added_names = [name for name in current_names if name not in previous_names]
removed_names = [name for name in previous_names if name not in current_names]
message = []
for name in added_names:
shelter = [n for n in new_data if n['name'] == name][0]
message.append('Added shelter: %s, %s' % (
shelter['name'], shelter['city']
))
message.append(' %s' % shelter['url'])
if added_names and removed_names:
message.append('')
for name in removed_names:
shelter = [n for n in old_data if n['name'] == name][0]
message.append('Removed shelter: %s, %s' % (
shelter['name'], shelter['city']
))
body = '\n'.join(message)
summary = []
if added_names:
summary.append('%d shelter%s added' % (
len(added_names), '' if len(added_names) == 1 else 's',
))
if removed_names:
summary.append('%d shelter%s removed' % (
len(removed_names), '' if len(removed_names) == 1 else 's',
))
if summary:
summary_text = '%s %s: %s' % (
verb, self.filepath, (', '.join(summary))
)
else:
summary_text = '%s %s' % (verb, self.filepath)
return '%s\n\n%s\nChange detected on %s' % (
summary_text, body, self.url
)
def fetch_data(self):
s = Soup(requests.get(self.url).content)
trs = s.find('table').findAll('tr')[1:]
shelters = []
for tr in trs:
tds = tr.findAll('td')
shelters.append({
'name': tds[1].getText(),
'url': 'http://www.ledgerdata.com/' + tds[1].find('a')['href'],
'city': tds[2].getText(),
'type': tds[3].getText(),
})
return shelters
class HernandoCountyShelters(BaseScraper):
filepath = 'hernando-county.json'
url = 'http://www.hernandocounty.us/em/shelter-information'
def create_message(self, new_data):
return self.update_message([], new_data, verb='Created')
def update_message(self, old_data, new_data, verb='Updated'):
current_names = [n['name'] for n in new_data]
previous_names = [n['name'] for n in old_data]
added_names = [name for name in current_names if name not in previous_names]
removed_names = [name for name in previous_names if name not in current_names]
message = []
for name in added_names:
shelter = [n for n in new_data if n['name'] == name][0]
message.append('Added shelter: %s, Hernando County' % (
shelter['name']
))
message.append(' %s, %s' % (
shelter['type'], shelter['status']
))
message.append(' %s' % shelter['address'])
if added_names and removed_names:
message.append('')
for name in removed_names:
shelter = [n for n in old_data if n['name'] == name][0]
message.append('Removed shelter: %s, Hernando County' % (
shelter['name']
))
body = '\n'.join(message)
summary = []
if added_names:
summary.append('%d shelter%s added' % (
len(added_names), '' if len(added_names) == 1 else 's',
))
if removed_names:
summary.append('%d shelter%s removed' % (
len(removed_names), '' if len(removed_names) == 1 else 's',
))
if summary:
summary_text = '%s %s: %s' % (
verb, self.filepath, (', '.join(summary))
)
else:
summary_text = '%s %s' % (verb, self.filepath)
return '%s\n\n%s\nChange detected on %s' % (
summary_text, body, self.url
)
def fetch_data(self):
s = Soup(requests.get(self.url).content)
shelters = []
for tr in s.find('table').findAll('tr'):
tds = tr.findAll('td')
img = tds[1].find('img')
if img is not None:
shelter_type = img['alt'].title()
else:
shelter_type = 'General'
shelters.append({
'name': tds[2].getText(),
'type': shelter_type,
'address': tds[3].getText(),
'status': tds[4].getText(),
})
return shelters
def update_message_from_names(current_names, previous_names, filepath, verb='Updated'):
added_names = [n for n in current_names if n not in previous_names]
removed_names = [n for n in previous_names if n not in current_names]
message = []
for name in added_names:
message.append('Added shelter: %s' % name)
if added_names:
message.append('')
for name in removed_names:
message.append('Removed shelter: %s' % name)
body = '\n'.join(message)
summary = []
if added_names:
summary.append('%d shelter%s added' % (
len(added_names), '' if len(added_names) == 1 else 's',
))
if removed_names:
summary.append('%d shelter%s removed' % (
len(removed_names), '' if len(removed_names) == 1 else 's',
))
if summary:
summary_text = filepath + ': ' + (', '.join(summary))
else:
summary_text = '%s %s' % (verb, filepath)
return summary_text + '\n\n' + body
def is_heading(tr):
return tr.findAll('td')[1].text == 'Shelter Name'
def is_shelter(tr):
return len(tr.findAll('td')) == 4 and not is_heading(tr)
def is_county_heading(tr):
if tr.find('td').get('colspan') == '5' and (u'#d4d4d4' in tr.find('td').get('style', '')) and tr.text != ' ':
return tr.text
else:
return None
class FloridaDisasterShelters(BaseScraper):
filepath = 'florida-shelters.json'
url = 'http://www.floridadisaster.org/shelters/summary.aspx'
def update_message(self, old_data, new_data):
def name(n):
return '%s (%s County)' % (n['name'], n['county'])
current_names = [name(n) for n in new_data]
previous_names = [name(n) for n in old_data]
message = update_message_from_names(current_names, previous_names, self.filepath)
message += '\nChange detected on %s' % self.url
return message
def fetch_data(self):
r = requests.get(self.url)
if r.status_code != 200:
print "Oh no - status code = %d" % r.status_code
return None
table = Soup(r.content).findAll('table')[9]
current_county = None
shelters = []
for tr in table.findAll('tr'):
heading = is_county_heading(tr)
if heading:
current_county = heading
if is_shelter(tr):
shelters.append({
'type': tr.findAll('td')[0].text,
'county': current_county.title(),
'name': tr.findAll('td')[1].text,
'address': tr.findAll('td')[2].text,
'map_url': tr.findAll('td')[2].find('a')['href'].split(' ')[0],
'city': tr.findAll('td')[3].text,
})
shelters.sort(key=lambda s: (s['county'], s['name']))
return shelters
class CrowdSourceRescue(BaseScraper):
filepath = 'crowdsourcerescue.json'
owner = 'simonw'
repo = 'private-irma-data'
slack_channel = None
url = 'https://crowdsourcerescue.com/rescuees/searchApi/'
def fetch_data(self):
return requests.post(self.url, {
'needstring': '',
'lat_min': '23.882475192722612',
'lat_max': '29.761185051094046',
'lng_min': '-86.76083325000002',
'lng_max': '-77.97177075000002',
'status': '0',
}).json()
if __name__ == '__main__':
test_mode = ('--test' in sys.argv)
github_token = os.environ.get('GITHUB_API_TOKEN', '')
slack_token = os.environ.get('SLACK_TOKEN', '')
scrapers = [
klass(github_token, slack_token)
for klass in (
SantaRosaEmergencyInformation,
SonomaRoadConditions,
GoogleCrisisKmlScraper,
SouthCarolinaShelters,
FemaOpenShelters,
FemaNSS,
IrmaShelters,
IrmaShelterDupes,
FloridaDisasterShelters,
ZeemapsScraper,
PascoCounty,
CrowdSourceRescue,
LedgerPolkCounty,
HernandoCountyShelters,
FplStormOutages,
FplCountyOutages,
GemaAnimalShelters,
GemaActiveShelters,
ScegOutages,
IrmaSheltersFloridaMissing,
GeorgiaOutages,
DukeFloridaOutages,
DukeCarolinasOutages,
NorthGeorgiaOutages,
TampaElectricOutages,
JemcOutages,
NewYorkShelters,
CaliforniaDOTRoadInfo,
CaliforniaHighwayPatrolIncidents,
PGEOutagesIndividual,
)
]
while True:
print datetime.datetime.now()
for scraper in scrapers:
if test_mode and not scraper.test_mode:
continue
try:
scraper.scrape_and_store()
except Exception, e:
print "!!!! %s: %s !!!!!" % (
scraper.__class__.__name__, e
)
if test_mode:
import pdb; pdb.post_mortem()
time.sleep(120)