Skip to content

Instantly share code, notes, and snippets.

@rococodogs
Created September 4, 2014 18:28
Show Gist options
  • Save rococodogs/7026816fe33aef53dc23 to your computer and use it in GitHub Desktop.
Save rococodogs/7026816fe33aef53dc23 to your computer and use it in GitHub Desktop.
cron'd php script to scrape our Ricoh printer's page counts to keep a running tally on our end
{
"require": {
"sunra/php-simple-html-dom-parser": "1.5"
}
}
<?php
require "vendor/autoload.php";
use Sunra\PhpSimple\HtmlDomParser;
/**
* database credentials, pdo style
* ex: `mysql:host=localhost;dbname=printbot`
*/
define("DBINFO", "");
define("DBUSER", "");
define("DBPASS", "");
/**
* add the printer's ip address
*/
define("IP_ADDRESS", "");
/**
* label for the printer
*/
define("PRINTER_LABEL", "Black and White Printer");
/* ********* */
$cgi_path = "/web/guest/en/websys/status/getUnificationCounter.cgi";
$selector = ".staticProp";
$dom = HtmlDomParser::file_get_html("http://" . IP_ADDRESS . $cgi_path);
$els = $dom->find($selector);
$bingo = $els[2];
$str = "";
foreach($bingo->childNodes() as $td) {
$str .= htmlspecialchars_decode($td->plaintext);
}
$ex = explode(":", $str);
$today_count = $ex[1];
/**
* get the last count from the db
*/
$pdo = new PDO(DBINFO, DBUSER, DBPASS);
$stmt = $pdo->prepare("select total from tally_hourly order by time desc limit 1");
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$last_count = $res[0]['total'];
$page_count = $today_count - $last_count;
/**
* and send the latest count
*/
$stmt_b = $pdo->prepare("insert into tally_hourly(page_count, total, label) values (:pc, :total, :label)");
$stmt_b->execute(array("pc" => $page_count, "total" => $today_count, "label" => PRINTER_LABEL));
$stmt->closeCursor();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment