Advertisement
Hauke

Sked schedule parser

Oct 12th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Sked schedule parser algorithm
  4.  * Developed by Hauke Marquardt
  5.  *
  6.  * License: CC BY-SA
  7.  * Uses: http://sourceforge.net/projects/simplehtmldom/
  8.  */
  9.  
  10. class Day
  11. {
  12.     public $name;
  13.     public $date;
  14.     public $startCol;
  15.     public $endCol;
  16.    
  17.     public function __construct($_startCol = 0, $_endCol = 0) {
  18.         $this->startCol = $_startCol;
  19.         $this->endCol = $_endCol;
  20.     }
  21.    
  22.     public function dateAsTimestamp() {
  23.         return DateTime::createFromFormat('d.m.Y h:i:s', $this->date . '00:00:00')->getTimestamp();
  24.     }
  25. }
  26.  
  27. class Event
  28. {
  29.     public $startRow;
  30.     public $rowspan;
  31.     public $description;
  32.     public $startTime;
  33.     public $endTime;
  34.     public $day;
  35.     public $isImportant;
  36.    
  37.     public function __construct($_startRow = 0, $_rowspan = 0, $_description = '') {
  38.         $this->startRow = $_startRow;
  39.         $this->rowspan = $_rowspan;
  40.         $this->description = htmlentities($_description);
  41.        
  42.         $this->startTime = ($this->startRow - 1) * 5 * 60 + 7 * 60 * 60;
  43.         $this->endTime = $this->startTime + $this->rowspan * 5 * 60;
  44.     }
  45. }
  46.  
  47. class SkedScheduleParser {
  48.    
  49.     public $url = null;
  50.     public $days = null;
  51.     public $events = null;
  52.    
  53.     private $html = null;
  54.     private $curl = null;
  55.    
  56.     public function __construct($_url) {
  57.         $this->url = utf8_decode($_url);
  58.         $this->curl = new Curl();
  59.     }
  60.    
  61.     public function loadHTML() {
  62.         $this->html = str_get_html($this->curl->get($this->url));
  63.     }
  64.    
  65.     public function parseTable($tableNumber = 0) {
  66.        
  67.         $table = $this->html->find('table', $tableNumber);
  68.        
  69.         $this->days = array();
  70.        
  71.         $cols = $table->find('colgroup', 0)->find('col');
  72.         $newDay = false;
  73.         $tmpDay = new Day();
  74.         // block first 2 and last 2 colums
  75.         for($i = 2; $i < count($cols) - 2; $i++) {
  76.             if($cols[$i]->width == 5 && !$newDay) {
  77.                 $tmpDay->startCol = $i;
  78.                 $newDay = true;
  79.             }
  80.             else if($cols[$i]->width == 5 && $newDay) {
  81.                 $tmpDay->endCol = $i;
  82.                 $this->days[] = new Day($tmpDay->startCol, $tmpDay->endCol);
  83.                 $newDay = false;
  84.                
  85.                 // end loop if sunday reached
  86.                 if(count($this->days) == 7) {
  87.                     break;
  88.                 }
  89.             }
  90.         }
  91.        
  92.         $daysCol = $table->find('tr', 0)->find('td');
  93.         // block first 2 and last 2 colums
  94.         $colIterator = 0;
  95.         for($i = 0; $i < count($daysCol); $i++) {
  96.            
  97.             foreach($this->days as &$day) {
  98.                 if($day->startCol == $colIterator) {
  99.                     $day->name = trim(substr($daysCol[$i]->plaintext, 0, strpos($daysCol[$i]->plaintext, ',')));
  100.                     $day->date = trim(substr($daysCol[$i]->plaintext, strrpos($daysCol[$i]->plaintext, ',') + 1, strlen($daysCol[$i]->plaintext)));
  101.                 }
  102.             }
  103.            
  104.             if(isset($daysCol[$i]->colspan)) {
  105.                 $colIterator += intval($daysCol[$i]->colspan);
  106.             }
  107.             else {
  108.                 $colIterator ++;
  109.             }
  110.         }
  111.        
  112.         // initialize row iterator for every colum
  113.         $rowIterator = array();
  114.         for($i = 0; $i < count($cols); $i++) {
  115.             $rowIterator[$i] = 0;
  116.         }
  117.        
  118.         // initialize array for all events
  119.         $this->events = array();
  120.         foreach($this->days as $day) {
  121.             $this->events[] = array();
  122.         }
  123.        
  124.         // reset colIterator
  125.         $tableRows = $table->find('tr');
  126.         $currentRowIndex = 0;
  127.         foreach($tableRows as $tableRow) {
  128.             $tableRowCols = $tableRow->find('td');
  129.            
  130.             foreach($tableRowCols as $tableRowCol) {
  131.                 $rowspan = 1;
  132.                 if(isset($tableRowCol->rowspan)) {
  133.                     $rowspan = intval($tableRowCol->rowspan);
  134.                 }
  135.                
  136.                 $colspan = 1;
  137.                 if(isset($tableRowCol->colspan)) {
  138.                     $colspan = intval($tableRowCol->colspan);
  139.                 }
  140.                
  141.                 for($i = 0; $i < count($rowIterator); $i++) {
  142.                     if($rowIterator[$i] <= $currentRowIndex) {
  143.                         for($o = 0; $o < $colspan; $o++) {
  144.                             $rowIterator[$i + $o] += $rowspan;
  145.                         }
  146.                        
  147.                         // find events
  148.                         if(isset($tableRowCol->id) && isset($tableRowCol->class) && $tableRowCol->class == 'v') {
  149.                             for($d = 0; $d < count($this->days); $d++) {
  150.                                 if($this->days[$d]->startCol <= $i && $this->days[$d]->endCol >= $i) {
  151.                                     $this->events[$d][] = new Event($currentRowIndex,
  152.                                         $rowspan,
  153.                                         $tableRowCol->innertext
  154.                                     );
  155.                                     break;
  156.                                 }
  157.                             }
  158.                         }
  159.                         break;
  160.                     }
  161.                 }
  162.             }
  163.             $currentRowIndex++;
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement