Location: PHPKode > projects > VideoDB > videodb/engines/youtube.php
<?php
/**
 * youtube.com trailer search
 *
 * Search trailers on youtube.com
 *
 * @package Engines
 * @author  Andreas Goetz   <hide@address.com>
 * @author  Adam Benson	    <hide@address.com>
 * @link    http://www.youtube.com  YouTube
 * @version $Id: youtube.php,v 1.3 2009/02/04 20:26:33 andig2 Exp $
 */

require_once './core/functions.php';
require_once './core/httpclient.php';

function youtubeHasTrailer($title)
{
	return count(youtubeSearchTrailer($title)) > 0;
}

function normalize($str)
{
	return preg_replace('/[^a-zA-Z0-9\s]/', '', $str);
}

function youtubeSearchTrailer($title)
{
	global $config;
	
	$trailers = array();
	
	if ($config['youtubekey'])
	{
		$title	= normalize($title);
		
		$trailerquery	= $title." trailer";
#		dump($trailerquery);
		
        $youtubeurl     = "http://gdata.youtube.com/feeds/api/videos?client=".YOUTUBE_CLIENT_ID."&key=".$config['youtubekey']."&v=2&".
                          "q=".urlencode($trailerquery)."&start-index=1&max-results=20";

        $resp = httpClient($youtubeurl, true);      
		if (!$resp['success']) return false;
				
		$xml 		= simplexml_load_string($resp['data']);
#		dump($xml);

		$tags		= explode(' ', $trailerquery);
#		dump($tags);

		foreach ($xml->entry as $trailer)
		{
			// Now, as the YouTube API doesn't allow us to search by title, do some extra filtering on the results...
			$tagsMatch	= true;
#			dump($trailer);

/* 
            // not sure this produces good results- disable filtering for now
			foreach($tags as $tag)
			{
				// make sure to normalize result title, too
				$ttitle	= normalize($trailer->title);

				if (stripos($ttitle, $tag) === false)
				{
#					dump($title, $tag);
					$tagsMatch	= false;
					break;
				}
			}			
			if ($tagsMatch) $trailers[] = $trailer->id;
*/
            
            $trailers[] = (string) $trailer->content['src'];
			
			if (count($trailers) >= 10) break;
		}
	}

	return $trailers;
}

?>
Return current item: VideoDB