So the other day, I published some JS code to change the look of the ARM-Koji Hosts page. It assumed that each client’s time was set to Eastern time zone settings (really very bad practice!). Since I have access to a PHP-based Apache server, I wanted to see how many lines it took me to implement the same.
<?php # get the koji host-status web page $u = "http://arm.koji.fedoraproject.org/koji/hosts"; $hostpage = file_get_contents($u); $hostpage = str_replace("\t", "", $hostpage); $hostpage = str_replace("\r", "", $hostpage); $hostpage = str_replace("\n", "", $hostpage); # prep the page to be processed for host names and times $tabldata = $hostpage; $tabldata = str_replace("<tr", "\n<tr", $tabldata); $tabldata = str_replace("</tr>", "</tr>\n", $tabldata); $tabldata = explode("\n", $tabldata); # store the host names and times in a new list $hostlist = array(); $begindex = -1; for ($x = 0; $x < count($tabldata); $x += 1) { if (preg_match("/^.*<a href=\"hostinfo\?hostID=([0-9]+)\">([^<]+)<\/a>.*>([0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+).*$/", $tabldata[$x], $machlist)) { $hosttime = preg_replace("/[^0-9]/", "", $machlist[3]); $templist = array(0, $machlist[1], $machlist[2], $machlist[3], $hosttime); array_push($hostlist, $templist); } if (preg_match("/^.*<a href=\"hostinfo\?hostID=[0-9]+\">[^<]+<\/a>.*$/", $tabldata[$x])) { $begindex = $x; $tabldata[$x] = ""; } } # loop thru the list and mark/sort the ones that up/down $timesecs = time(); $utcsdate = gmdate("YmdHis", $timesecs); $utcstime = (intval($utcsdate) - (15 * 60)); for ($x = 0; $x < count($hostlist); $x += 1) { if (intval($hostlist[$x][4]) <= $utcstime) { $hostlist[$x][0] = 0; $hostlist[$x][2] = ("<b><font color='red'>".$hostlist[$x][2]."</font></b>"); } else { $hostlist[$x][0] = 1; $hostlist[$x][2] = ("<b><font color='green'>".$hostlist[$x][2]."</font></b>"); } } sort($hostlist); $headlist = array("row-odd", "row-even"); for ($x = 0; $x < count($hostlist); $x += 1) { $c = $headlist[$x % 2]; $i = $hostlist[$x][1]; $n = $hostlist[$x][2]; $t = $hostlist[$x][3]; $tabldata[$begindex] .= ("<tr class='".$c."'><td>".$i."</td><td><a href='".$u."/hostinfo?hostID=".$i."'>".$n."</a></td><td>.</td><td>.</td><td>.</td><td>".$t."</td></tr>\n"); } echo (join("\n", $tabldata)."\n"); ?>
Why not use the XML-RPC interface rather than scraping the HTML?