Last active
December 11, 2015 14:58
-
-
Save CNG/4617984 to your computer and use it in GitHub Desktop.
HTML scraping with e-mail notification example
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
<?php | |
error_reporting(E_ERROR | E_PARSE); | |
$dom = new DOMDocument; | |
$dom->loadHTMLFile('http://www.crowdrise.com/jobraising/'); | |
$node = $dom->getElementById('causes_module'); | |
$finder = new DomXPath($dom); | |
$classname="profile"; | |
$nodes = $finder->query("(//div[@id='causes_module'])[1]/div/div[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); | |
$results = array(); | |
foreach($nodes as $node){ | |
$name = $amount = ''; | |
foreach($node->getElementsByTagName('h4') as $node2){ | |
$name = trim($node2->nodeValue); | |
} | |
foreach($node->getElementsByTagName('h3') as $node2){ | |
$amount = $node2->nodeValue; | |
} | |
$results[] = array('name'=>$name,'amount'=>$amount); | |
} | |
$key = 0; | |
for($i = 0; $i < count($results); $i++){ | |
if(!strcmp($results[$i]['name'],'YouthBiz')){ | |
$key = $i; | |
} | |
} | |
$newplace = $key + 1; | |
$threshold = 4; | |
$file = '/var/www/zxzx.co/crowdrise/youthbizplace.txt'; | |
if(file_exists($file)){ | |
$oldplace = file_get_contents($file); | |
if($oldplace != $newplace){ | |
echo "At last check, YouthBiz was in place $oldplace, and now it is in place $newplace<br />"; | |
file_put_contents($file, $newplace); | |
$upName = $results[$newplace-2]['name']; | |
$upAmount = $results[$newplace-2]['amount']; | |
$upPlace = $newplace - 1; | |
$downName = $results[$newplace]['name']; | |
$downAmount = $results[$newplace]['amount']; | |
$downPlace = $newplace + 1; | |
$amount = $results[$newplace-1]['amount']; | |
$headers = "From: AB <[email protected]>" . "\r\n" . 'Reply-To: AB <[email protected]>' . "\r\n" . "BCC: [email protected]"; | |
$to = 'AB <[email protected]>, BB <[email protected]>'; | |
$body = "$upName is in place $upPlace with $upAmount and $downName is in place $downPlace with $downAmount\n\nGo to http://www.crowdrise.com/jobraising/ for complete information"; | |
// only send alerts if crosses threshold | |
if(false){ | |
if($oldplace <= $threshold && $newplace > $threshold){ | |
$subject = "YouthBiz has fallen to place $newplace with $amount"; | |
echo "$subject. $body<br />"; | |
mail($to, $subject, $body, $headers); | |
} else if($oldplace > $threshold && $newplace <= $threshold) { | |
$subject = "YouthBiz has risen to place $newplace with $amount"; | |
echo "$subject. $body<br />"; | |
mail($to, $subject, $body, $headers); | |
} | |
} else { | |
if($oldplace < $newplace){ | |
$subject = "YouthBiz has fallen to place $newplace with $amount"; | |
echo "$subject. $body<br />"; | |
mail($to, $subject, $body, $headers); | |
} else { | |
$subject = "YouthBiz has risen to place $newplace with $amount"; | |
echo "$subject. $body<br />"; | |
mail($to, $subject, $body, $headers); | |
} | |
} | |
} else { | |
echo "YouthBiz is still in place $oldplace<br />"; | |
} | |
} else { | |
echo "YouthBiz is in place $newplace<br />"; | |
echo file_put_contents($file, $newplace); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment