Location: PHPKode > projects > Nzbirc > nzbirc-1.4/modules/ed.php
<?php

/**************************************************
 * NZBirc v1
 * Copyright (c) 2006 Harry Bragg
 * tiberious.org
 * Module: ed
 **************************************************
 *
 * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

class Net_SmartIRC_module_ed extends Net_SmartIRC_module_base
{

	// default module variable
	var $name = 'ed';
	var $version = 'v0.1';
	var $description = 'Newzbin.com editor command, to retrieve information about a post from its filename';

	/**
	 * Newzbin editor module
	 *
	 * STARTCONFIG	 
	 * @var array publicCommand  - Lists the commands that trigger this module, and return publicly
	 * @var array privateCommand - Lists the commands that trigger this module privatly (notice you)
	 * ENDCONFIG
	 */
	var $_config = array(
		'publicCommand' => array( '^ed', '^!ed' ),
		'privateCommand' => array( '^@ed' )
	);

	/**
	 * @var array
	 * @access private
	 */	
	var $_def = array(
		'general' => array(
			/**
			 * matches[1] = show
			 *        [2] = series
			 *        [3] = episode
			 *        [4] = extra info
			 */		
			'/^(.+?)(?<!the)(?:\.|\s|\s-\s|\(|\[|\_|\-)(?:\(|\[|\_|\-)?(?:s|series)?\s?([0-9]+?),?\s?\.?(?:e|x|ep|episode)\s?([0-9]{2})(?![0-9]+)(?:\.|\s\-\s|\s|\)|\]|\_|\-)?(.*)$/i',
			),
		'addPart' => array(
			'from' => '/\((\d+)\)$/i',
			'to' => '(Part $1)'
			),
		'attributes' => array( 
			'Source' => array(
				'CAM' => '/cam/i',
				'Screener' => '/(dvd[.-]?scr|screener)/i',
				'TeleCine' => '/\.tc\./i',
				'R5 Retail' => '/r5/i',
				'TeleSync' => '/\.ts\./i',
				'VHS' => '/vhs/i',
				'HDTV' => '/hdtv/i',
				'DVD' => '/dvd/i',
				'TV Cap' => '/(pdtv|dsr)/i',
				'HD-DVD' => '/hd[-.]?dvd/i',
				'Blu-ray' => '/(blu[-.]?ray|bd[-.]?(rom|rip))/i'
				),
			'Format' => array(
				'XviD' => '/xvid/i',
				'x264' => '/x264/i',
				'H.264' => '/(h\.?264|avc)/i',
				'HD .TS' => '/\.ts(?!\.)/i',
				'SVCD' => '/svcd/i',
				'DivX' => '/divx/i',
				'WMV' => '/wmv/i',
				'ratDVD' => '/ratDVD/i'
				),
			'Res' => array( // these arnt part of newzbin.com, but its nice to know
				'Normal' => '!/(hr|720p|\.?720\.|1080i|1080p|\.ts)/i', // custom ! preceeding the regex to denote it is a negative regex
				'HR' => '/hr/i',
				'720p' => '/(720p|\.?720)/i',
				'1080i' => '/1080i/i',
				'1080p' => '/1080p/i'
				),
			'Audio' => array( // also not implemented but nice
				'AC3/DD' => '/(ac3|dd[25]\.?[01]|5\.1)/i',
				'dts' => '/dts/i',
				'MP3' => '/mp3/i',
				'AAC' => '/aac/i',
				'Ogg' => '/ogg/i'
				)
			),
		'filmMatch' => array(
			'/dvd.+/i'
			),
		'siteAttributes' => array(
			'genre' => array(
				'Action' => 'Action/Adv',
				'Adventure' => 'Action/Adv'
				),
			'class' => array(
				'Animation' => 'Animation',
				'Reality' => 'Reality',
				'Documentary' => 'Docu'
				)
			)
		);	

	/**
	 * If there is an error, store it in here
	 *
	 * @var bool
	 * @access private
	 */
	var $_error;
	
	/**
     * Template
     */
    var $_template = array(
        'tv' => '{name} - {series}x{sepnum} - {title} | {url} | {extra} | Genre: {genre}',
		'film' => '{title} ({year}) | {url} | {extra} | Genre: {genre}',
        'extra' => '{attr}: {prop}',
        'help' => '{command} filename - Looks at the filename or general post infomation from usenet and if it is TV determines; show, series, episode, title, url if Film determines: title, year, url and also gets: source, format, genre, resolution and audio information',
		'error' => '**Error:** {errormsg}',		
    );

	function _registerCommands() 
	{
		$this->_setHandler( '.*', $this, 'ed' );
	}

	/*****************************************************
	 * Bot Calls
	 *****************************************************/

	function ed( &$irc, &$data, $notice = false )
	{
		$query = $data->strippedMessage;

		if ( strlen( $query ) > 0 )
		{					
			// help
			if ( $query == 'help' )
			{
				$this->parseTemplate( $msg, 'help', array( 'command' => $data->trigger ) );
			}
			else
			{
				if ( ( $ed = $this->doQuery( $irc, $query ) ) === false )
				{
					$this->parseTemplate( $msg, 'error', array( 'errormsg' => $this->_error ) );
				}
				else
				{
					if ( isset( $ed['series'] ) )
					{
						$this->parseTemplate( $msg, 'tv', $ed );
					}
					else
					{
						$this->parseTemplate( $msg, 'film', $ed );
					}
				}
			}
		}
		else
		{
			$this->parseTemplate( $msg, 'help', array( 'command' => $data->trigger ) );
		}
		
		if ( isset( $msg ) )
		{
			$irc->_modules['func']->reply( $irc, $data, $msg, $notice );
		}
	}

	/*****************************************************
	 * Main functions
	 *****************************************************/
	
	function doQuery( &$irc, $string )
	{
		foreach( $this->_def['general'] as $reg )
		{
			if ( preg_match( $reg, $string, $matches ) )
			{
				$matched = true;
				return $this->tvQuery( $string, $matches );
			}
		}
		if (!$matched)
		{
			return $this->filmQuery( $string );
		}
	}
	
	function tvQuery( $string, $matches )
	{
		global $irc;
		
		// using tv rage to parse the information
		
		$showquery = str_replace( '.', ' ', $matches[1] );
		
		if ( ( $show = $irc->_modules['tvrage']->getFShow( $showquery ) ) !== false )
		{		
			if ( ( $ep = $irc->_modules['tvrage']->getFEpisode( $show['tvrage'], $matches[2], $matches[3] ) ) !== false )
			{		
				// search for episode properties
				
				foreach( $this->_def['attributes'] as $attr => $array )
				{
					foreach( $array as $id => $reg )
					{
					 	if ( substr( $reg, 0, 1 ) == '!' ) 
					 	{
							// denote a negative regex
							if ( ! preg_match( substr( $reg, 1 ), $matches[4] ) )
							{
								$out[$attr][] = $id;
							}							
						}
						else
						{
							if ( preg_match( $reg, $matches[4] ) )
							{
								$out[$attr][] = $id;
							}
						}
					}
				}
				
				if ( !isset( $out['Source'] ) )
				{
					$out['Source'][] = 'TV Cap';
				}
				
				// prepare output
				foreach( $out as $attr => $aitem )
				{					
				    $ep['extra'][] = array(
                        'attr' => $attr,
                        'prop' => implode( ', ', $aitem ) );
                    
                    unset( $prop );
				}
				
				$ep['seperator'] = ' | ';
				
				$ep['name'] = $show['name'];
				$ep['sepnum'] = sprintf( '%02d', $ep['sepnum'] );
				$ep['genre'] = explode( ' | ', $show['genre'] );
				$ep['title'] = preg_replace( $this->_def['addPart']['from'], $this->_def['addPart']['to'], $ep['title'] );
				
				$class = explode( ' | ', $show['class'] );
				
				foreach( $ep['genre'] as $id => $gen )
				{
					if ( isset( $this->_def['siteAttributes']['genre'][$gen] ) )
					{
						if ( !in_array( $this->_def['siteAttributes']['genre'][$gen], $ep['genre'] ) )
							$ep['genre'][] = $this->_def['siteAttributes']['genre'][$gen];
						unset( $ep['genre'][$id] );
					}
				}
				
				foreach( $class as $id => $cl )
				{
					if ( isset( $this->_def['siteAttributes']['class'][$cl] ) )
					{
						if ( !in_array( $this->_def['siteAttributes']['class'][$cl], $ep['genre'] ) )
							$ep['genre'][] = $this->_def['siteAttributes']['class'][$cl];
					}
				}
				
				sort( $ep['genre'] );
				$ep['genre'] = implode( ', ', $ep['genre'] );
				
				return $ep;
			}
			else
			{
				$this->_error = sprintf( 'Invalid episode: %dx%02d for show: %s', $matches[2], $matches[3], $showquery );
				return false;
			}
		}
		else
		{
			$this->_error = 'Invalid show name: '.$showquery;
			return false;
		}
		
	}
	
	function filmQuery( $string )
	{
		global $irc;
		
		// get attributes first
		foreach( $this->_def['attributes'] as $attr => $array )
		{
			foreach( $array as $id => $reg )
			{
				if ( substr( $reg, 0, 1 ) == '!' ) 
				{
					// denote a negative regex
					if ( !preg_match( substr( $reg, 1 ), $string ) )
					{
						$out[$attr][] = $id;
					}							
				}
				else
				{
					if ( preg_match( $reg, $string ) )
					{
						$out[$attr][] = $id;
					}
				}
			}
		}
		
		$fTitle = $string;
		
		foreach( $this->_def['attributes'] as $attr => $array )
		{
			foreach( $array as $id => $reg )
			{
				if ( substr( $reg, 0, 1 ) != '!' ) 
				{
					$reg = substr( $reg, 0, -2 ).'.+/i';
					$fTitle = preg_replace( $reg, '', $fTitle );
				}
			}
		}
		
		$fTitle = preg_replace( $this->_def['filmMatch'], '', $fTitle );
		
		$fTitle = trim( str_replace( array( '.' ), ' ', $fTitle ) );
		
		while( $fTitle != $old )
		{
			if ( ( $film = $irc->_modules['imdb']->getSFilm( $fTitle ) ) !== false )
			{
				// prepare output
				foreach( $out as $attr => $aitem )
				{				
					$film['extra'][] = array(
						'attr' => $attr,
						'prop' => implode( ', ', $aitem ) );
					
					unset( $prop );
				}
				
				$film['seperator'] = ' | ';
				
				foreach( $film['genre'] as $id => $gen )
				{
					if ( isset( $this->_def['siteAttributes']['genre'][$gen] ) )
					{
						if ( !in_array( $this->_def['siteAttributes']['genre'][$gen], $film['genre'] ) )
							$film['genre'][] = $this->_def['siteAttributes']['genre'][$gen];
						unset( $film['genre'][$id] );
					}
				}			
				
				sort( $film['genre'] );
				$film['genre'] = implode( ', ', $film['genre'] );
				
				return $film;
			}
			$old = $fTitle;			
			$fTitle = preg_replace( '/\s+\S+$/i', '', $fTitle );
		}
		$this->_error = 'Could not find a matching film';
		return false;
	}
}

?>
Return current item: Nzbirc