quickpress /wp-content/plugins/broken-link-checker/modules/extras/youtube.php

Language PHP Lines 175
MD5 Hash f74ec3f5eb88f5b81b29ec8bed00998c Estimated Cost $2,789 (why?)
Repository https://bitbucket.org/lgorence/quickpress.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/*
Plugin Name: YouTube API
Description: Check links to YouTube videos using the YouTube API.
Version: 1.1
Author: Janis Elsts

ModuleID: youtube-checker
ModuleCategory: checker
ModuleContext: on-demand
ModuleLazyInit: true
ModuleClassName: blcYouTubeChecker
ModulePriority: 100

ModuleCheckerUrlPattern: @^http://([\w\d]+\.)*youtube\.[^/]+/watch\?.*v=[^/#]@i
*/

class blcYouTubeChecker extends blcChecker {
	var $youtube_developer_key = 'AI39si4OM05fWUMbt1g8hBdYPRTGpNbOWVD0-7sKwShqZTOpKigo7Moj1YGk7dMk95-VWB1Iue2aiTNJb655L32-QGM2xq_yVQ';
	var $api_grace_period = 0.3; //How long to wait between YouTube API requests.
	var $last_api_request = 0;   //Timestamp of the last request.
	
	function can_check($url, $parsed){
		return true;
	}
	
	function check($url){
		//Throttle API requests to avoid getting blocked due to quota violation.
		$delta = microtime_float() - $this->last_api_request; 
		if ( $delta < $this->api_grace_period ) {
			usleep(($this->api_grace_period - $delta) * 1000000);
		}
		
		$result = array(
			'final_url' => $url,
			'redirect_count' => 0,
			'timeout' => false,
			'broken' => false,
			'log' => "<em>(Using YouTube API)</em>\n\n",
			'result_hash' => '',
		);
		
		//Extract the video ID from the URL
		$components = @parse_url($url);
		parse_str($components['query'], $query);
		$video_id = $query['v'];
		
		//Fetch video data from the YouTube API
		$api_url = 'http://gdata.youtube.com/feeds/api/videos/' . $video_id . '?key=' . urlencode($this->youtube_developer_key);
		$conf = blc_get_configuration();
		$args = array( 'timeout' => $conf->options['timeout'], );
		
		$start = microtime_float();
		$response = wp_remote_get($api_url, $args);
		$result['request_duration'] = microtime_float() - $start;
		$this->last_api_request = $start;
		
		//Placeholders for video restriction data
		$state_name = $state_reason = '';
		
		//Got anything?
		if ( is_wp_error($response) ){
			$result['log'] .= "Error.\n" . $response->get_error_message();
			//WP doesn't make it easy to distinguish between different internal errors.
        	$result['broken'] = true;
        	$result['http_code'] = 0;
		} else {
			$result['http_code'] = intval($response['response']['code']);
			
			switch($result['http_code']){
				case 404 : //Not found
					$result['log'] .= __('Video Not Found', 'broken-link-checker');
					$result['broken'] = true;
					$result['http_code'] = 0;
					$result['status_text'] = __('Video Not Found', 'broken-link-checker');
					$result['status_code'] = BLC_LINK_STATUS_ERROR;
					break;
					
				case 403 : //Forbidden. Usually means that the video has been removed. Body contains details.
					$result['log'] .= $response['body'];
					$result['broken'] = true;
					$result['http_code'] = 0;
					$result['status_text'] = __('Video Removed', 'broken-link-checker');
					$result['status_code'] = BLC_LINK_STATUS_ERROR;
					break;
					
				case 400 : //Bad request. Usually means that the video ID is incorrect. Body contains details.
					$result['log'] .= $response['body'];
					$result['broken'] = true;
					$result['http_code'] = 0;
					$result['status_text'] = __('Invalid Video ID', 'broken-link-checker');
					$result['status_code'] = BLC_LINK_STATUS_WARNING;
					break;
					
				case 200 : //Video exists, but may be restricted. Check for <yt:state> tags.
					//See http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_yt:state
				
					//Can we count on an XML parser being installed? No, probably not.
					//Back to our makeshift tag "parser" we go.
					$state = blcUtility::extract_tags($response['body'], 'yt:state', false);
					if ( empty($state) ){
						//Phew, no restrictions.
						$result['log'] .= __("Video OK", 'broken-link-checker');
						$result['status_text'] = __('OK', 'link status', 'broken-link-checker');
						$result['status_code'] = BLC_LINK_STATUS_OK;
						$result['http_code'] = 0;
					} else {
						
						//Get the state name and code and append them to the log
						$state = reset($state);
						$state_name = $state['attributes']['name'];
						$state_reason = isset($state['attributes']['reasonCode'])?$state['attributes']['reasonCode']:'';
						
						$result['result_hash'] = 'youtube_api|' . $state_name . '|' . $state_reason; 
						
						$result['log'] .= sprintf(
							__('Video status : %s%s', 'broken-link-checker'),
							$state_name,
							$state_reason ? ' ['.$state_reason.']':''
						);
						
						//A couple of restricted states are not that bad
						$state_ok = ($state_name == 'processing') ||    //Video still processing; temporary. 
						            (
									    $state_name == 'restricted' &&  
						                $state_reason == 'limitedSyndication' //Only available in browser
						            );
            			
            			if ( $state_ok ) {
            				$result['broken'] = false;
            				$result['status_text'] = __('OK', 'link status', 'broken-link-checker');
							$result['status_code'] = BLC_LINK_STATUS_OK;
							$result['http_code'] = 0;
            			} else {
            				$result['broken'] = true;
            				$result['status_text'] = __('Video Restricted', 'broken-link-checker');
							$result['status_code'] = BLC_LINK_STATUS_WARNING;
							$result['http_code'] = 0;
            			}
					}
					
					//Add the video title to the log, purely for information.
					//http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_media:title
					$title = blcUtility::extract_tags($response['body'], 'media:title', false);
					if ( !empty($title) ){
						$result['log'] .= "\n\nTitle : \"" . $title[0]['contents'] . '"';
					}
					 
					break;
				
				default:
					$result['log'] .= $result['http_code'] . $response['response']['message'];
					$result['log'] .= "\n" . __('Unknown YouTube API response received.'); 
					break;
			}			
		}
		
		//The hash should contain info about all pieces of data that pertain to determining if the 
		//link is working.  
        $result['result_hash'] = implode('|', array(
        	'youtube',
			$result['http_code'],
			$result['broken']?'broken':'0', 
			$result['timeout']?'timeout':'0',
			$state_name,
			$state_reason
		));
        
        return $result;
	}

}

?>
Back to Top