|
| 1 | +#!/usr/bin/php |
| 2 | +<?php |
| 3 | + |
| 4 | +// Use http_proxy environment variable |
| 5 | +stream_context_set_default(array('http' => array( |
| 6 | + 'proxy' => str_replace("http://", "tcp://", getenv('http_proxy')), |
| 7 | + 'request_fulluri' => true |
| 8 | +))); |
| 9 | + |
| 10 | +$quotes = new BashOrgQuotes(); |
| 11 | +$quote = $quotes->getQuote(); |
| 12 | +$quote = $quotes::cleanupQuote($quote); |
| 13 | +echo $quote."\n"; |
| 14 | + |
| 15 | +class Quotes { |
| 16 | + |
| 17 | + private $quotefile = ''; // Quote cache location |
| 18 | + private $quotes = array(); // Quotes |
| 19 | + |
| 20 | + function __construct() { |
| 21 | + $this->quotefile = getenv('HOME')."/.bashqdb"; |
| 22 | + $this->quotefp = fopen($this->quotefile,"c+"); |
| 23 | + // Only one instance accessing the cache at a time |
| 24 | + if (!flock($this->quotefp, LOCK_EX)) |
| 25 | + throw new Exception('Could not lock quote cache'); |
| 26 | + $this->cacheRead(); |
| 27 | + } |
| 28 | + |
| 29 | + function __destruct() { |
| 30 | + $this->fillIfEmpty(); |
| 31 | + $this->cacheWrite(); |
| 32 | + flock($this->quotefp, LOCK_UN); |
| 33 | + fclose($this->quotefp); |
| 34 | + } |
| 35 | + |
| 36 | + function cacheRead() { |
| 37 | + $cache = file($this->quotefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT); |
| 38 | + if ($cache) { |
| 39 | + $this->quotes = $cache; |
| 40 | + } else { |
| 41 | + //throw new Exception('Could not read quote cache'); |
| 42 | + $this->quotes = array(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + function cacheWrite() { |
| 47 | + if (FALSE === file_put_contents($this->quotefile, implode("\n", $this->quotes))) { |
| 48 | + throw new Exception('Could not write quote cache'); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + function fillIfEmpty() { |
| 53 | + if (!$this->quotes) |
| 54 | + $this->quotes = $this->fetchQuotes(); |
| 55 | + } |
| 56 | + |
| 57 | + function getQuote() { |
| 58 | + $quote = array_pop($this->quotes); |
| 59 | + $this->fillIfEmpty(); |
| 60 | + if (!$quote) { // This is not good enough error handling |
| 61 | + $quote = array_pop($this->quotes); |
| 62 | + $this->fillIfEmpty(); |
| 63 | + } |
| 64 | + return $quote; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class BashOrgQuotes extends Quotes { |
| 69 | + function fetchQuotes() { // */p[@class='qt'] |
| 70 | + $matches = array(); |
| 71 | + $source = file_get_contents("http://bash.org/?random1"); |
| 72 | + // @todo Handle failure |
| 73 | + preg_match_all("/<p class=\"qt\">(.+?)<\/p>/si", $source, $matches); |
| 74 | + $quotes = array(); |
| 75 | + foreach ($matches[1] as $match) { |
| 76 | + $quotes[] = str_replace("\n","",$match); |
| 77 | + } |
| 78 | + return $quotes; |
| 79 | + } |
| 80 | + |
| 81 | + static function cleanupQuote($quote) { |
| 82 | + $quote = html_entity_decode($quote); |
| 83 | + $quote = str_replace("<br />","\n",$quote); |
| 84 | + return $quote; |
| 85 | + } |
| 86 | +} |
| 87 | +// http://www.qdb.us/api |
| 88 | +?> |
0 commit comments