<?php
require_once 'CurlGetPage.class.php';
/*
* Copyright (c) 2009, Simon Paarlberg (hide@address.com) PGPkey:CA83E278
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Simon Paarlberg ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Simon Paarlberg BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
* Usage:
*
* $epg = new Epguides();
* var_dump( $epg->getShow('AmericanDad') );
*
*/
/**
* Class for getting information from epguides.com
* @author Simon Paarlberg (CA83E278)
*/
class Epguides extends CurlGetPage {
/**
*
* @param $name string, ID of the TV show at epguides.com
* @return array
*/
public function getShow($name) {
if(empty($name))
return false;
/** Getting the raw HTML from epguides.com */
$html = $this->getPage("http://epguides.com/{$name}/");
if(strpos($html,'HTTP Error 404')!==false || empty($html))
return array('ERROR' => 'The show could not be found!');
/** Getting the some header information like show name etc. */
$data = $this->getEpData($html);
/** Removes all but the stuff in the <pre> tags */
$this->trimPreList($html);
/** Removes all unwanted lines in the input */
$this->removeWrapData($html);
$data['episodes'] = $this->processToList($html);
return $data;
}
/**
* Returns the header data like show title etc.
* @param $html string, the raw html
* @return array
*/
private function getEpData(&$html) {
$temp = substr($html, strpos($html,'<h1>')+4);
$temp = substr($temp, 0, strpos($temp,'</h1>'));
$title = strip_tags($temp);
$link = '';
if(strlen($temp)!=strlen($title)) {
$link = substr($temp, strpos($temp,'href="')+6);
$link = substr($link, 0, strpos($link,'">'));
}
return array(
'title' => $title,
'link' => $link,
);
}
/**
* Removes all but the stuff in the <pre> tags
* @param $html string, the raw html
* @return null
*/
private function trimPreList(&$html) {
if(empty($html))
return;
$html = substr($html, strpos($html, '<div id="eplist">'));
$html = substr($html, strpos($html, '<pre>')+5);
$html = substr($html, 0, strpos($html, '</pre>'));
$html = trim($html);
}
/**
* Removes all unwanted lines in the input
* @param $html string, the raw html
* @return null
*/
private function removeWrapData(&$html) {
if(empty($html))
return;
$list = explode("\n", $html);
if(count($list)==0)
return;
$text = '';
foreach($list as $entry) {
if(empty($entry))
continue;
if(strpos($entry,'<a ')===false && strlen($entry)<110)
continue;
$text .= $entry . "\n";
}
$html = $text;
}
/**
* Proccess the raw data line (entry) by line into a multilevel array
* @param $html string, raw html
* @return array
*/
private function processToList($html) {
if(empty($html))
return array();
$list = explode("\n", $html);
if(count($list)==0)
return array();
$array = array();
foreach($list as $entry) {
$a = $this->processEntry($entry);
if(!empty($a['prodNo']))
$array[] = $a;
}
return $array;
}
/**
* Splits every line up into smaller bits of information and returns
* them in a nice array
* @param $line string, each line of the <pre> data
* @return array
*/
private function processEntry($line) {
if(empty($line))
return;
$season = trim(substr($line,6,2));
$episode = trim(substr($line,9,2));
$prodNo = trim(substr($line,11,13));
if(empty($prodNo))
return;
$origAirDate = trim(substr($line,27,10));
// Cutting away the first columns
$line = substr($line,37);
$link = substr($line,strpos($line,'href="')+6);
$link = trim(substr($link,0,strpos($link,'">')));
$title = substr($line,strpos($line,'">')+2);
$title = trim(substr($title,0,strpos($title,'<')));
return array(
'epCode' => $this->makeEpCode($season,$episode),
'title' => $title,
'origAirDate' => strtotime($origAirDate)+0,
'prodNo' => $prodNo,
'season' => $season,
'episode' => $episode,
'link' => $link,
);
}
/**
* Small function for processing the Episode code (ex. S01E02)
* @param $season string, holding the value of the season (S,P,[0-9])
* @param $episode int, holding the number of the episode ([0-9])
* @return string
*/
private function makeEpCode($season,$episode) {
if(empty($season) || empty($episode))
return;
// Incase of special episodes
if($season=='S')
$season='0';
// The pilot is often the first episode in the first season
if($season=='P' && $episode=1)
$season='1';
elseif($season=='P')
$season='0';
if(strlen($season)==1)
$season = '0' . $season;
if(strlen($episode)==1)
$episode = '0' . $episode;
return "S{$season}E{$episode}";
}
}