Skip to content

Instantly share code, notes, and snippets.

@markjames
Created April 26, 2010 20:52
Show Gist options
  • Save markjames/379906 to your computer and use it in GitHub Desktop.
Save markjames/379906 to your computer and use it in GitHub Desktop.
<?php
require( 'persistent.class.php' );
class Episode extends Persistent {
public $series;
public $season;
public $number;
public $airdate;
public $title;
public $description;
protected $detailURL;
public function __construct() {
$args = func_get_args();
$this->series = '';
$this->season = '';
$this->number = 0;
$this->airdate = 0;
$this->title = '';
$this->description = '';
switch( count($args) ) {
case 1: // String version
$this->split($args[0]);
break;
case 2: // Series/Number
list($this->series,$this->number) = $args;
break;
case 3: // Series/Season/Number
list($this->series,$this->season,$this->number) = $args;
break;
}
}
public function is_released() {
return mktime() > $this->airdate;
}
public function toString( $format = '%1$s - S%2$02dE%3$02d - %4$s', $dateformat='Ymd' ) {
return sprintf( $format, $this->series, $this->season, $this->number, $this->title, date( $dateformat, $this->airdate ), $this->description );
}
public function __toString() {
return $this->toString();
}
public function split( $in ) {
$matches = array();
// Attempt format with dots/spaces and S##E##
$expression = '~^(?:\[\w+\][ .]*)?([-A-Za-z0-9. ]+?)[. \[](S\d\d)(E\d\d)[. \[]?~i';
if( preg_match( $expression, $in, $matches ) ) {
$this->series = ucwords(strtolower(str_replace( '.', ' ', $matches[1] )));
$this->season = intval(str_ireplace( 'S', '', $matches[2] ));
$this->number = intval(str_ireplace( 'E', '', $matches[3] ));
return true;
}
// Attempt format with dots/spaces and production code
$expression = '~^([-A-Za-z0-9. ]+?)[. \[]+(\d+?)[x.]?(\d+?)[. \]]~i';
if( preg_match( $expression, $in, $matches ) ) {
$this->series = ucwords(strtolower(str_replace( '.', ' ', $matches[1] )));
$this->season = intval($matches[2]);
$this->number = intval(str_ireplace( 'x', '', $matches[3] ));
return true;
}
// Simple text format
$expression = '~^([-A-Za-z0-9. ]+?)\s+(S|Season)?\s*?(\d+)\s*?(Ep|Episode)?\s*?(\d+)~i';
if( preg_match( $expression, $in, $matches ) ) {
$this->series = ucwords(strtolower(str_replace( '.', ' ', $matches[1] )));
$this->season = $matches[3];
$this->number = $matches[5];
return true;
}
return false;
}
public function save( $dir='./data/' ) {
return parent::save( $dir.preg_replace('~\W~', ' ', $this->toString('%1$s S%2$02dE%3$02d')).'.txt' );
}
public function load( $dir='./data/' ) {
return parent::load( $dir.preg_replace('~\W~', ' ', $this->toString('%1$s S%2$02dE%3$02d')).'.txt' );
}
public function cached( $dir='./data/' ) {
return file_exists( $dir.preg_replace('~\W~', ' ', $this->toString('%1$s S%2$02dE%3$02d')).'.txt' );
}
public function load_from_epguides() {
if( empty($this->series) ) {
throw new Exception('Show not set');
}
$contents = file_get_contents( 'http://epguides.com/' . preg_replace( '~\W~', '', $this->series ) );
if( $contents === false ) {
$contents = file_get_contents( 'http://epguides.com/' . str_ireplace('The','',preg_replace( '~\W~', '', $this->series )));
}
$matches = array();
if( preg_match( sprintf("~^\s*\d+[.]\s+%d[-]\s*?%2d\s+(\S*?)\s+(.*?)[<].*?href=\"(.*?)\".*?[>](.*?)[<]~mi", $this->season, $this->number), $contents, $matches ) ) {
$this->productioncode = $matches[1];
$this->airdate = strtotime(trim($matches[2]));
$this->detailURL = $matches[3];
$this->title = $matches[4];
} else if( preg_match( sprintf("~<li>\s*%d[-]\s*?%2d\s+(\S*?)\s+(.*?)[<].*?href=\"(.*?)\".*?[>](.*?)[<]~mi", $this->season, $this->number), $contents, $matches ) ) {
$this->productioncode = $matches[1];
$this->airdate = strtotime(trim($matches[2]));
$this->detailURL = $matches[3];
$this->title = $matches[4];
} else {
return false;
}
$detailcontents = file_get_contents( $this->detailURL );
//$detailcontents = '';
$expression = '~<title>(.*?)</title>~si';
$seriesmatches = array();
if( preg_match( $expression, $detailcontents, $seriesmatches ) ) {
$this->series = trim(trim(trim(str_replace('- TV.com','',strip_tags($seriesmatches[1]))), ':'));
}
$expression = '~<p class="deck">(.*?)<a~si';
$detailmatches = array();
if( preg_match( $expression, $detailcontents, $detailmatches ) ) {
$this->description = trim(strip_tags($detailmatches[1]));
} else {
$expression = '~<div id="main-col">\s+<div>(.*?)(?:<div)~si';
$detailmatches = array();
if( preg_match( $expression, $detailcontents, $detailmatches ) ) {
$this->description = trim(strip_tags($detailmatches[1]));
}
}
return true;
}
public static function get( $title ) {
$episode = new Episode( $title );
if( $episode->cached() ) {
$episode->load();
} else {
$episode->load_from_epguides();
$episode->save();
}
return $episode;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment