Location: PHPKode > scripts > TVTome > tvtome/class_tvtome.php
<?php
/**************************************************************************************
 * Class: TVTome
 **************************************************************************************
 * Version:         0.1
 * Released:        04-01-2003 (NOT an April fools joke, however)
 * Author:          tsigo <hide@address.com>
 **************************************************************************************
 * Description:
 *      A class to parse info for a particular show from TVTome.com
 *      Right now this is limited to ranking episodes of the show based on user voting
 *          functionality that already exists in TVTome, but has no way of ordering
 *          them by rank within the site.
 * 
 *      I'll gladly extend its functionality as people find a need for this class.
 **************************************************************************************
 * Changes:
 *      2003-04-01: tsigo
 *          - Original revision, written in about 30 minutes out of boredom (the best
 *            origins!)
 **************************************************************************************
 * TODO:
 *      - Just about everything, but it does what *I* needed it to do.  E-mail me if you
 *        need this extended.
 **************************************************************************************
 * Usage:
 *      Provide the name of the show you want (the TVTome name, i.e. no spaces) to the
 *      'new' construct.
 * 
 *      $tvtome = new TVTome('Seinfeld'); $tvtome = new TVTome('Shield'); 
 *      $tvtome = new TVTome('CurbYourEnthusiasm'), etc.
 * 
 *      You can also set a new show with $tvtome->set_show('Show');
 * 
 *      To rank episodes, just set the show and call $tvtome->rank(), it returns a sorted array
 *      of rank => episode name pairs.
 **************************************************************************************
 * License:
 *      The GNU General Public License (GPL) http://www.opensource.org/licenses/gpl-license.html
 *      Basically do whatever you want with it, just leave these comments here.
 * 
 *      Suggestions/bug reports to hide@address.com
 **************************************************************************************
 * Notes:
 *      Go easy on the TVTome.com servers - don't loop the rankings for 
 *      The Simpsons 1000 times :)
 **************************************************************************************/

define('EI_EPISODENO',  1);
define('EI_FIRSTAIRED', 2);
define('EI_PRODCODE',   4);
define('EI_WRITER',     8);
define('EI_DIRECTORY', 16);
define('EI_RATING',    32);
define('EI_VOTES',     64);
define('EI_RANK',     128);

class TVTome
{
    var $socket   = NULL;
    var $show     = '';
    var $eplist   = array();
    var $rankings = array();
    var $seasons  = 0;
    
    function tvtome($show = '')
    {
        if ( !empty($show) )
        {
            $this->show = $show;
            
            $this->connect();
        }
    }
    
    function connect()
    {
        if ( is_null($this->socket) )
        {
            $this->socket = fsockopen('tvtome.com', 80, $errno, $error);
            if ( !$this->socket )
            {
                return 'Connection failed.';
            }
        }
    }
    
    function disconnect()
    {
        fclose($this->socket);
        $this->socket = NULL;
        
        return;
    }
    
    function getep($link, $info)
    {
        $this->connect();
        
        $ep_info = array();
        
        fputs($this->socket, "GET " . $link . " HTTP/1.1\r\nHost: tvtome.com\r\nConnection: close\r\n\r\n");
        while ( !feof($this->socket) )
        {
            $content = fgets($this->socket, 512);
            
            // Get requested info
            if ( $info & EI_RATING )
            {
                $pattern = '#<td ALIGN=LEFT nowrap>Average Rating\:</td><td>(.+)</td>#';
                preg_match($pattern, $content, $matches);
                if ( !empty($matches[1]) )
                {
                    $ep_info['rating'] = $matches[1];
                }
            }
            
            if ( $info & EI_VOTES )
            {
                $pattern = '#<td ALIGN=LEFT nowrap>Number of Votes Cast\:</td><td>(.+)</td>#';
                preg_match($pattern, $content, $matches);
                if ( !empty($matches[1]) )
                {
                    $ep_info['votes'] = $matches[1];
                }
            }
            
            if ( $info & EI_RANK )
            {
                $pattern = '#<td ALIGN=LEFT nowrap>Episode Rank\:</td><td>(.+)</td>#';
                preg_match($pattern, $content, $matches);
                if ( !empty($matches[1]) )
                {
                    $ep_info['rank'] = $matches[1];
                }
            }
        }
        
        $this->disconnect();
        
        return $ep_info;
    }
    
    function eplist()
    {
        $this->connect();
        
        fputs($this->socket, "GET /" . $this->show . "/eplist.html HTTP/1.1\r\nHost: tvtome.com\r\nConnection: close\r\n\r\n");
        while ( !feof($this->socket) )
        {
            $content = fgets($this->socket, 512);

            // Season            
            $pattern = '#<tr><td colspan="6"><a name=".+"><b>Season ([0-9]+)</b></a></td></tr>#';
            if ( preg_match($pattern, $content, $matches) )
            {
                $this->seasons = $matches[1];
            }
            
            // Episode
            $pattern = '#<tr align="center"><td align="right" class="small">([0-9]+)\.</td><td nowrap class="small"> ([0-9]+\-[0-9]+)</td><td class="small"\>([0-9]+)</td><td align="right" class="small" nowrap>([0-9A-Za-z\-]+)</td><td>.+</td><td class="small" nowrap align="left"><a href="(.+)">(.+)</a></td></tr>#';
            if ( preg_match($pattern, $content, $matches) )
            {
                $this->eplist[ $matches[1] ] = array(
                    'order' => $matches[1],
                    'number' => $matches[2],
                    'prodno' => $matches[3],
                    'airdate' => $matches[4],
                    'link' => $matches[5],
                    'title' => $matches[6]);
            }
        }
        
        $this->disconnect();
        
        if ( sizeof($this->eplist) == 0 )
        {
            return 'Show ' . $this->show . ' not found at TVTome';
        }
        
        return true;
    }
    
    function rank()
    {
        // Populate the eplist if we need to
        if ( sizeof($this->eplist) == 0 )
        {
            $this->eplist();
        }
        
        foreach ( $this->eplist as $episode )
        {
            $ep_info = $this->getep($episode['link'], EI_RANK);
            
            if ( isset($ep_info['rank']) )
            {
                $this->rankings[ intval($ep_info['rank']) ] = $episode['title'];
            }
        }
        
        ksort($this->rankings);
        
        return $this->rankings;
    }
    
    function set_show($show)
    {
        $this->show = ( is_string($show) ) ? $show : '';
    }
    
    function reset()
    {
        $this->show = '';
        $this->eplist = array();
        $this->rankings = array();
        $this->seasons = 0;
        $this->socket = NULL;
    }
}
?>
Return current item: TVTome