<commit_before><?php

class YoutubePlugin extends AbstractPlugin {

	/*
	
	How do we extract direct links to YouTube videos?
	
	find .js file url for html5 player inside the video page that's embedded at:
	<script>var ytplayer = ytplayer......
	
	current url:
	http://s.ytimg.com/yts/jsbin/html5player-en_US-vfldudhuW/html5player.js
	
	*/
	
	
	function vn($a, $b){
		$c = $a[0];
		$a[0] = $a[$b % strlen($a)];
		$a[$b] = $c;
		return $a;
	}
	
	function sig_decipher($sig){

		$a = $this->vn($sig, 61);
		
		$a = strrev($a);
		$a = substr($a, 1);

		$a = $this->vn($a, 31);
		$a = $this->vn($a, 36);

		$a = substr($a, 1);
		
        return $a;
    }
	
	private function get_youtube_links($html){

		if(preg_match('@url_encoded_fmt_stream_map["\']:\s*["\']([^"\'\s]*)@', $html, $matches)){
		
			$parts = explode(",", $matches[1]);
			
			//var_dump($parts); exit;
			
			foreach($parts as $p){
				$query = str_replace('\u0026', '&', $p);
				parse_str($query, $arr);
				
				$url = $arr['url'];
				
				if(isset($arr['sig'])){
					$url = $url.'&signature='.$arr['sig'];
				
				} else if(isset($arr['signature'])){
					$url = $url.'&signature='.$arr['signature'];
				
				} else if(isset($arr['s'])){
				
					// this is probably a VEVO/ads video... signature must be decrypted first!
					$signature = $this->sig_decipher($arr['s']);
					
					$url = $url.'&signature='.$signature;
				}
				
				$result[$arr['itag']] = $url;
			}
			
			return $result;
		}
		
		return false;
	}
	
	private function find_first_available($links, $itags){
	
		foreach($itags as $itag){
		
			if(isset($links[$itag])){
				return $links[$itag];
			}
		}
		
		return false;
	}
	
	public function onCompleted(FilterEvent $event){
	
		$response = $event->getResponse();
		$output = $response->getContent();
		
		$url = $event->getRequest()->getUri();
		
		if(contains($url, "youtube.com")){
		
			// do this on all youtube pages
			$output = preg_replace('@masthead-positioner">@', 'masthead-positioner" style="position:static;">', $output, 1);
			$output = preg_replace('#<img[^>]*data-thumb=#s','<img alt="Thumbnail" src=', $output);
			
			
			if(contains($url, "youtube.com/watch")){
			
				$links = $this->get_youtube_links($output);
				
				// the only ones supported by flowplayer
				$flv_itags = array(5, 34, 35);
				$mp4_itags = array(18, 22, 37, 38, 82, 84);
				$webm_itags = array(43, 44, 46, 100, 102);
				
				//var_dump($links);
				
				
				global $config;
				
				$html5 = $config->get("youtube.html5_player");
				
				
				if($html5){
				
					// find mp4
					$mp4_url = $this->find_first_available($links, $mp4_itags);
					$mp4_url = proxify_url($mp4_url);
					
					//var_dump($mp4_url);
					
					$player = '<video width="100%" height="100%" controls autoplay>
									<source src="'.$mp4_url.'" type="video/mp4">
								Your browser does not support the video tag.
							</video>';
					
				} else {
				
					$vid_url = $this->find_first_available($links, $flv_itags);
					$player = vid_player($vid_url, 640, 390);
				}
				
				// remove the 
				$output = str_replace('<div id="theater-background" class="player-height"></div>', '', $output);
				
				// replace youtube player div block with our own
				$output = preg_replace('#<div id="player-api"([^>]*)>.*<div class="clear"#s', 
				'<div id="player-api"$1>'.$player.'</div><div class="clear"', $output, 1);
			}
			
			$response->setContent($output);
		}
	}

}

?><commit_msg>fixed youtube plugin - vevo videos now work just fine<commit_after><?php

class YoutubePlugin extends AbstractPlugin {

	protected $url_pattern = 'youtube.com';
	
	/*
	
	How do we extract direct links to YouTube videos?
	
	find html5player.js file that's embedded inside any video page source
	
	current js file is located at:
	
	//s.ytimg.com/yts/jsbin/html5player-en_US-vfl20EdcH/html5player.js
	
	*/
	
	
	function vn($a, $b){
		$c = $a[0];
		$a[0] = $a[$b % strlen($a)];
		$a[$b] = $c;
		return $a;
	}
	
	function sig_decipher($sig){

		$a = vn($sig, 12);
		$a = vn($a, 18);
		
		$a = substr($a, 1);
		
		$a = vn($a, 60);
		
		return $a;
    }
	
	private function get_youtube_links($html){

		if(preg_match('@url_encoded_fmt_stream_map["\']:\s*["\']([^"\'\s]*)@', $html, $matches)){
		
			$parts = explode(",", $matches[1]);
			
			//var_dump($parts); exit;
			
			foreach($parts as $p){
				$query = str_replace('\u0026', '&', $p);
				parse_str($query, $arr);
				
				$url = $arr['url'];
				
				if(isset($arr['sig'])){
					$url = $url.'&signature='.$arr['sig'];
				
				} else if(isset($arr['signature'])){
					$url = $url.'&signature='.$arr['signature'];
				
				} else if(isset($arr['s'])){
				
					// this is probably a VEVO/ads video... signature must be decrypted first!
					$signature = $this->sig_decipher($arr['s']);
					
					$url = $url.'&signature='.$signature;
				}
				
				$result[$arr['itag']] = $url;
			}
			
			return $result;
		}
		
		return false;
	}
	
	private function find_first_available($links, $itags){
	
		foreach($itags as $itag){
		
			if(isset($links[$itag])){
				return $links[$itag];
			}
		}
		
		return false;
	}
	
	public function onCompleted(FilterEvent $event){
	
		$response = $event->getResponse();
		$output = $response->getContent();
		
		// do this on all youtube pages
		$output = preg_replace('@masthead-positioner">@', 'masthead-positioner" style="position:static;">', $output, 1);
		
		// replace future thumbnails with src=
		$output = preg_replace('#<img[^>]*data-thumb=#s','<img alt="Thumbnail" src=', $output);
		
		$links = $this->get_youtube_links($output);
		
		// we must be on a video page
		if($links){
		
			// the only ones supported by flowplayer
			$flv_itags = array(5, 34, 35);
			
			// supported by html5 player
			$mp4_itags = array(18, 22, 37, 38, 82, 84);
			
			// not supported by any player at the moment
			$webm_itags = array(43, 44, 46, 100, 102);
			
			//var_dump($links);
			
			
			global $config;
			
			$html5 = $config->get("youtube.html5_player");
			
			if($html5){
			
				// find mp4
				$mp4_url = $this->find_first_available($links, $mp4_itags);
				$mp4_url = proxify_url($mp4_url);
				
				//var_dump($mp4_url);
				
				$player = '<video width="100%" height="100%" controls autoplay>
								<source src="'.$mp4_url.'" type="video/mp4">
							Your browser does not support the video tag.
						</video>';
				
			} else {
			
				$vid_url = $this->find_first_available($links, $flv_itags);
				$player = vid_player($vid_url, 640, 390);
			}
			
			// this div blocks our player controls
			$output = str_replace('<div id="theater-background" class="player-height"></div>', '', $output);
			
			// replace youtube player div block with our own
			$output = preg_replace('#<div id="player-api"([^>]*)>.*<div class="clear"#s', 
			'<div id="player-api"$1>'.$player.'</div><div class="clear"', $output, 1);
		}
			
		$response->setContent($output);
	}

}

?>
{'fraction_non_alphanumeric': 0.19616050808314087, 'fraction_numerical': 0.021651270207852194, 'mean_word_length': 2.939169982944855, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2941447', 'n_tokens_mistral': 3240, 'n_tokens_neox': 2459, 'n_words': 528}