Youtube Loader

Hello guys, yesterday i have write this class for personal utility and now i want relase this, so i hope this will be useful for someone besides me


<?php
/*
 * YoutubeLoader class by Sam
 */

class YoutubeLoader{
    
    //current simple_html_dom instance
    private $html;
    //channel id
    private $channel = false;
    //channel name
    public $channel_name;
    //videos id array
    private $videos = array();
    
    //construct loader
    public function __construct(){
       if(!class_exists('simple_html_dom_nodes'))
         require_once 'lib/simple_html_dom.php';
    }
    
    /*
     * Connect to a channel name
     * @param $name string of the channel
     * Fatal error if don't exist
     */
    public function LoadChannel($name){
        $this->channel_name = $name;
        $this->channel = file_get_html('http://www.youtube.com/user/'.$name)->find('.upper-left-section',1)->children(0)->children(0)->getAttribute('data-subscription-value');
    }
    
    /*
     * Load a video and return with the current id of $videos for be embed by $this->embedVideo(), order select is ascending
     * @param $num int of the video number
     */
    public function getVideo($num=1){
        $num--;
            
        $this->checkChannel();
        $num_view = ceil($num/30)<1 ? 1 : ceil($num/30);
        $this->html = str_get_html($this->recursiveCheck('http://www.youtube.com/channel_ajax?action_load_more_videos=1&amp;flow=grid&amp;view=0&amp;sort=da&amp;channel_id='.$this->channel.'&amp;paging='.$num_view));
        $num = $num>30 ? $num-(30*($num_view-1)) : $num;
        if($num<0) die('Invalid number video');
        $this->videos[] = $this->getId($num);
        
        return $this->getActualVideo();
        
    }
    
    /*
     * Embed a video that is loaded
     * @param $result the key on the videos array
     * @param $width int of the iframe width
     * @param $height int of the iframe height
     */
    public function embedVideo($result,$width=200,$height=150){
        if(in_array($result,$this->videos) && is_int($width) && is_int($height))
           return "<iframe src='".$result."' style='width:$width;height:$height;border:0px;'></iframe>";
         else
            return 'Error on embed video, errate source or not existent/loaded video';
    }
    
    private function recursiveCheck($st,$tm=0){
        $json = json_decode(file_get_contents($st),true);
        $load = next($json);
          if($load=='SUCCESS')
          {
              if($tm>10) die('Cant\\' load video');
              $tm++;
              return $this->recursiveCheck($st,$tm);
          }
          else
              return $load;
    }
    
    private function checkChannel(){
        if($this->channel==false) die('Must load a valid channel before get videos');
    }
    
    private function getId($n){
        return str_replace('/watch?v=','http://www.youtube.com/embed/',$this->html->find('li[class=channels-content-item]', $n)->find('span[class=context-data-item]',0)->find('a',0)->href);
    }
    
    /*
     * Load the end video of a channel and return with the current id of videos
     */
    public function getEndVideo(){
          
        $this->checkChannel();
        $this->html = str_get_html($this->recursiveCheck('http://www.youtube.com/channel_ajax?action_load_more_videos=1&amp;flow=grid&amp;view=0&amp;sort=dd&amp;channel_id='.$this->channel.'&amp;paging=1'));
        $this->videos[] = $this->getId(0);
        
        return $this->getActualVideo();
                
    }
    
    /*
     * Add video id at $videos array and return the id
     * @param $id id of the video
     */
    public function addVideo($id){
        $this->videos[] = $id;
        return $this->getActualVideo();
    }
    
    /*
     * Reset $videos
     */
    public function clearVideos(){
        $this->videos = array();
    }
    
    /*
     * Delete a video from $videos
     * @param $id id to search and delete
     */
    public function deleteVideo($id){
        unset($this->videos[array_search($id,$this->videos)]);
    }
    
    /*
     * Get the number of subscribers
     */
    public function getSubscribers(){
        $this->checkChannel();
        return file_get_html('http://www.youtube.com/user/'.$this->channel_name.'/')->find('span[class=stat-value]',0);
    }
    
    /*
     * Get the end feed
     */
    public function getEndFeed(){
        $this->checkChannel();
        return file_get_html('http://www.youtube.com/user/'.$this->channel_name.'/feed')->find('div[class=feed-item-post]',0);
    }
    
    
    /*
     * Try to get the video with the name title
     * @param $title the title of the video
     */
    public function tryGetVideoWithTitle($title,$message='No videos found<br>'){
        
       if(!empty($title)){
                
            $this->checkChannel();
            $url = 'http://www.youtube.com/user/'.$this->channel_name.'/videos?query='.urlencode($title);
            if(strstr(file_get_contents($url),'channels-content-item')){
                
            $this->html = file_get_html($url);
            $this->videos[] = current(explode('&',$this->getId(0)));
            
            return $this->getActualVideo();
            
            }else echo $message;
            
            return false;
            
        }else echo 'Error, compile the title for find video';
    }
    
    /*
     * Return the end video loaded
     */
    public function getActualVideo(){
        return end($this->videos);
    }
}
?>


//Basic example

include 'class.youloader.php';

$youtube = new YoutubeLoader();


$youtube->LoadChannel('Google');


echo $youtube->embedVideo($youtube->getEndVideo(),400,300);

The class work with simple_html_dom class (it’s required)