Created
September 8, 2017 00:01
-
-
Save rossdakin/8497164ed3acd1b210475480672c3009 to your computer and use it in GitHub Desktop.
Apifier script for Miami-Dade County emergency info.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pageFunction(context) { | |
// called on every page the crawler visits, use it to extract data from it | |
var $ = context.jQuery; | |
var alerts = $('.important-content-container').map(function(i, div) { | |
var $div = $(div); | |
return { | |
title: $div.find('h2').html().trim(), | |
body: $div.find('p').map(function(i, p) { return $(p).html() }).toArray().join(' ') | |
}; | |
}).toArray(); | |
var currentStatus = $('.group-title:contains("Situation Status")').parent().find('.status').first().find('p').map(function(i, p) { return $(p).html() } ).toArray().join(' '); | |
var $evacBody = $('.group-title:contains("Evacuation Orders")').parent().find('.status').first(); | |
function evacZone($evacBody, zone) { | |
var $listP = $evacBody.find('p:first'); | |
var specialCase = $listP.siblings(':contains("' + zone + '")').text(); | |
return specialCase || !!$evacBody.text().toLowerCase().match(zone.toLowerCase()); | |
} | |
var evacuationZones = [ | |
{ | |
key: 'Zone A', | |
valFunc: function() { return evacZone($evacBody, 'Zone A'); } | |
}, | |
{ | |
key: 'Zone B', | |
valFunc: function() { return evacZone($evacBody, 'Zone B'); } | |
}, | |
{ | |
key: 'Zone C', | |
valFunc: function() { return evacZone($evacBody, 'Zone C'); } | |
}, | |
{ | |
key: 'Zone D', | |
valFunc: function() { return evacZone($evacBody, 'Zone D'); } | |
}, | |
{ | |
key: 'Zone E', | |
valFunc: function() { return evacZone($evacBody, 'Zone E'); } | |
}, | |
{ | |
key: 'All Mobile Homes', | |
valFunc: function() { return !!$evacBody.text().toLowerCase().match('all mobile homes'); } | |
}, | |
{ | |
key: 'Barrier Islands', | |
valFunc: function() { | |
var array = $evacBody.find('p:contains("barrier islands include")').next('ul').find('li').map(function(i, li) { return $(li).text() }).toArray() | |
return array.length > 0 && array; | |
} | |
}, | |
].reduce(function (obj, pair) { | |
obj[pair.key] = pair.valFunc(); | |
return obj; | |
}, {}); | |
var openClosed = $('.group-title:contains("Closed")').parent().find('.status-title').map(function(i, title) { | |
return { | |
title: $(title).text(), | |
bodyHtml: $(title).next().html().trim() | |
} | |
}).toArray(); | |
var lastEdited = ($('.lastEdited').text().match(/Page Last Edited: (.*)/) || [])[1]; | |
return { | |
alerts: alerts, | |
currentStatus: currentStatus, | |
evacuationZones: evacuationZones, | |
openClosed: openClosed, | |
lastEdited: lastEdited | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment