Command Line RSS Feed Monitor

<?php	
	class timer {
 
		public $callBack, $i, $t;
 
		public function __construct($i,$t,$w = 0) {
			$this->i = $i;
			$this->t = $t;
			if($i*$t > ini_get('max_execution_time') || $t < 1 && $w)
				trigger_error("max_execution_time will prevent timer from functioning completely", E_USER_WARNING);
		}
 
		public function add($cb,array $params=null) {
			if((@function_exists($cb)&&is_string($cb))||is_array($cb)&&count($cb)==2&&@method_exists($cb[0],$cb[1]))
				$this->callBack[] = array("f" => $cb, "p" => $params);
		}
 
		public function exec() {
			do {
				foreach($this->callBack as $cb) {
					if(is_array($cb['f']))
						$cb['f'] = implode("::",$cb['f']);
					call_user_func_array($cb['f'],(array)$cb['p']);
				}
				$this->t--;
				sleep($this->i);
			}	while($this->t != 0);
		}
 
		public function __toString() {
			return __CLASS__;
		}
	}
 
 
	class Rss extends timer {
		public $lCache, $cCache, $tCache, $rss;
 
		public function __construct($rss,$i,$t) {
			parent::__construct($i,$t);
			$this->rss = $rss;
			$this->add(array($this,"getRss"),array($this->rss));
			echo "\nWatching:\t{$rss}\n\n";
			$this->exec();
		}
 
		public function getRss($url) {
			$this->cCache = $this->rssParser($url);
			if($this->lCache[0] != $this->cCache[0] && $this->lCache[0] != NULL) {
				$tCache = array();
				for($a = 0; $this->cCache[$a]; $a++)
					if($this->cCache[$a]==$this->lCache[0])
						break;
				for(;$a >= 0;$a--)
					$tCache[] = $this->cCache[$a];
				unset($tCache[0]);
				$this->alert($tCache);
			}
			$this->lCache = $this->cCache;
			$this->cCache = array();
		}
 
		public function rssParser($url) {
			preg_match_all('/(?s)<item>(.+?)<\/item>/',file_get_contents($url),$rss);
			$array = array();
			foreach($rss[1] as &$rss) {
				preg_match_all('/(?s)<(title|description|link|pubDate|author)>(.+)<\/\1>/',$rss,$i);
				$item = array("title" => $i[2][0],"author" => $i[2][1],"link" => $i[2][2],"desc" => $i[2][3],"date" => $i[2][4]);
				array_push($array,$item);
			}
			return $array;
		}
 
		public function alert(array $cache) {
			echo "New posts detected! (".date('h:i:s A').")\n";
			foreach($cache as $t)
				echo "{$t['title']}\n";
			echo "\x07\x07\n";
		}
 
		public function __toString() {
			return __CLASS__;
		}
	}
 
	if ($argv[1])
		new Rss($argv[1],(is_int($argv[2])?$argv[2]:600),0);
?>
Processing your request, Please wait....

No Responses to “Command Line RSS Feed Monitor”

Comments (Your Comments)

Leave a Reply

You must be logged in to post a comment.