Skip to content

Instantly share code, notes, and snippets.

@CNG
Last active December 11, 2015 14:58
Show Gist options
  • Save CNG/4617984 to your computer and use it in GitHub Desktop.
Save CNG/4617984 to your computer and use it in GitHub Desktop.
HTML scraping with e-mail notification example
<?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 <test@test.com>" . "\r\n" . 'Reply-To: AB <test@test.com>' . "\r\n" . "BCC: test@test.com";
$to = 'AB <test@test.com>, BB <test@test.com>';
$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