Skip to content

Instantly share code, notes, and snippets.

@meyerweb
Last active October 21, 2021 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meyerweb/6422542afccaec0540495d5d4282e1e9 to your computer and use it in GitHub Desktop.
Save meyerweb/6422542afccaec0540495d5d4282e1e9 to your computer and use it in GitHub Desktop.
School Closings scraper script (ft. anonymized Dark Sky API call)
if ($debug) {
error_reporting(E_ALL);
ini_set('display_errors', '1');
$json = load_file("testdata_darksky/forecast_20170131.json");
$html = load_file("testdata_closings/wjw_schools-20170131.html");
} else {
$json = get_closings("https://api.darksky.net/forecast/%%%%%%%%%%/%%%%%%%%%%%");
$html = get_closings("http://s3.amazonaws.com/wjwclosings/wjw_schools.html");
}
if (isset($json)) {
$darksky = json_decode($json,true);
echo "\r<ul>";
echo "\r<li>Now: " . round($darksky["currently"]["temperature"]) . "ºF (feels like " . round($darksky["currently"]["apparentTemperature"]) . "ºF)";
echo "\r<li>Soon: " . $darksky["minutely"]["summary"] . "</li>";
echo "\r<li>Today: " . $darksky["hourly"]["summary"] . "</li>";
echo "\r</ul>";
}
$dom = new DOMDocument();
$dom->loadHTML($html);
$headings = $dom->getElementsByTagName('h4');
$update = $headings[0]->nodeValue;
$update = str_replace("School Closings Last Updated: ","",$update);
$items = $dom->getElementsByTagName('li');
if ($items->length > 0) {
$schools = Array(
'Cleveland Heights',
'Cleveland Hts.',
'Beachwood City Schools',
'Shaker Heights City Schools',
'Cleveland Metropolitan'
);
echo "<dl>";
$schoolcount = 0;
foreach ($items as $item) {
$text = $item->nodeValue;
foreach ($schools as $school) {
$pos = strpos($text,$school);
if ($pos !== FALSE) {
$bits = explode("|",$text);
$school = $bits[0];
$status = $bits[1];
echo "<dt>$school</dt><dd>$status</dd>";
$schoolcount++;
}
}
}
if (!$schoolcount) {
echo "<dt>None of the monitored schools are listed.</dt>";
}
echo "</dl>";
} else {
echo "<dl><dt>There are no listed closings.</dt></dl>";
}
echo "<footer>";
echo "<span>Listings: $items->length</span>";
$interval = _mw_lt_human_time(strtotime(str_replace(" AT","",$update)));
if ($interval) {
echo "<time>Last updated: $interval</time>";
} else {
echo "<time>Last updated: $update</time>";
}
echo "</footer>";
//echo $html;
if ($debug) {
echo "<pre style='font: 11px monospace;'>";
print_r($html);
echo "</pre>";
}
FUNCTION get_closings($url) {
if (FUNCTION_exists('curl_init') && $url) {
$session = curl_init($url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_FAILONERROR, true);
curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($session, CURLOPT_TIMEOUT, 10);
$stream = curl_exec($session);
curl_close($session);
}
return $stream;
}
FUNCTION load_file($path) {
$html = '';
$fs=fopen($path, "r");
while (!feof($fs)) {
$html .= fgets($fs);
}
fclose ($fs);
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment