PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 175 lines | 125 code | 21 blank | 29 comment | 13 complexity | f74ec3f5eb88f5b81b29ec8bed00998c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: YouTube API
  4. Description: Check links to YouTube videos using the YouTube API.
  5. Version: 1.1
  6. Author: Janis Elsts
  7. ModuleID: youtube-checker
  8. ModuleCategory: checker
  9. ModuleContext: on-demand
  10. ModuleLazyInit: true
  11. ModuleClassName: blcYouTubeChecker
  12. ModulePriority: 100
  13. ModuleCheckerUrlPattern: @^http://([\w\d]+\.)*youtube\.[^/]+/watch\?.*v=[^/#]@i
  14. */
  15. class blcYouTubeChecker extends blcChecker {
  16. var $youtube_developer_key = 'AI39si4OM05fWUMbt1g8hBdYPRTGpNbOWVD0-7sKwShqZTOpKigo7Moj1YGk7dMk95-VWB1Iue2aiTNJb655L32-QGM2xq_yVQ';
  17. var $api_grace_period = 0.3; //How long to wait between YouTube API requests.
  18. var $last_api_request = 0; //Timestamp of the last request.
  19. function can_check($url, $parsed){
  20. return true;
  21. }
  22. function check($url){
  23. //Throttle API requests to avoid getting blocked due to quota violation.
  24. $delta = microtime_float() - $this->last_api_request;
  25. if ( $delta < $this->api_grace_period ) {
  26. usleep(($this->api_grace_period - $delta) * 1000000);
  27. }
  28. $result = array(
  29. 'final_url' => $url,
  30. 'redirect_count' => 0,
  31. 'timeout' => false,
  32. 'broken' => false,
  33. 'log' => "<em>(Using YouTube API)</em>\n\n",
  34. 'result_hash' => '',
  35. );
  36. //Extract the video ID from the URL
  37. $components = @parse_url($url);
  38. parse_str($components['query'], $query);
  39. $video_id = $query['v'];
  40. //Fetch video data from the YouTube API
  41. $api_url = 'http://gdata.youtube.com/feeds/api/videos/' . $video_id . '?key=' . urlencode($this->youtube_developer_key);
  42. $conf = blc_get_configuration();
  43. $args = array( 'timeout' => $conf->options['timeout'], );
  44. $start = microtime_float();
  45. $response = wp_remote_get($api_url, $args);
  46. $result['request_duration'] = microtime_float() - $start;
  47. $this->last_api_request = $start;
  48. //Placeholders for video restriction data
  49. $state_name = $state_reason = '';
  50. //Got anything?
  51. if ( is_wp_error($response) ){
  52. $result['log'] .= "Error.\n" . $response->get_error_message();
  53. //WP doesn't make it easy to distinguish between different internal errors.
  54. $result['broken'] = true;
  55. $result['http_code'] = 0;
  56. } else {
  57. $result['http_code'] = intval($response['response']['code']);
  58. switch($result['http_code']){
  59. case 404 : //Not found
  60. $result['log'] .= __('Video Not Found', 'broken-link-checker');
  61. $result['broken'] = true;
  62. $result['http_code'] = 0;
  63. $result['status_text'] = __('Video Not Found', 'broken-link-checker');
  64. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  65. break;
  66. case 403 : //Forbidden. Usually means that the video has been removed. Body contains details.
  67. $result['log'] .= $response['body'];
  68. $result['broken'] = true;
  69. $result['http_code'] = 0;
  70. $result['status_text'] = __('Video Removed', 'broken-link-checker');
  71. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  72. break;
  73. case 400 : //Bad request. Usually means that the video ID is incorrect. Body contains details.
  74. $result['log'] .= $response['body'];
  75. $result['broken'] = true;
  76. $result['http_code'] = 0;
  77. $result['status_text'] = __('Invalid Video ID', 'broken-link-checker');
  78. $result['status_code'] = BLC_LINK_STATUS_WARNING;
  79. break;
  80. case 200 : //Video exists, but may be restricted. Check for <yt:state> tags.
  81. //See http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_yt:state
  82. //Can we count on an XML parser being installed? No, probably not.
  83. //Back to our makeshift tag "parser" we go.
  84. $state = blcUtility::extract_tags($response['body'], 'yt:state', false);
  85. if ( empty($state) ){
  86. //Phew, no restrictions.
  87. $result['log'] .= __("Video OK", 'broken-link-checker');
  88. $result['status_text'] = __('OK', 'link status', 'broken-link-checker');
  89. $result['status_code'] = BLC_LINK_STATUS_OK;
  90. $result['http_code'] = 0;
  91. } else {
  92. //Get the state name and code and append them to the log
  93. $state = reset($state);
  94. $state_name = $state['attributes']['name'];
  95. $state_reason = isset($state['attributes']['reasonCode'])?$state['attributes']['reasonCode']:'';
  96. $result['result_hash'] = 'youtube_api|' . $state_name . '|' . $state_reason;
  97. $result['log'] .= sprintf(
  98. __('Video status : %s%s', 'broken-link-checker'),
  99. $state_name,
  100. $state_reason ? ' ['.$state_reason.']':''
  101. );
  102. //A couple of restricted states are not that bad
  103. $state_ok = ($state_name == 'processing') || //Video still processing; temporary.
  104. (
  105. $state_name == 'restricted' &&
  106. $state_reason == 'limitedSyndication' //Only available in browser
  107. );
  108. if ( $state_ok ) {
  109. $result['broken'] = false;
  110. $result['status_text'] = __('OK', 'link status', 'broken-link-checker');
  111. $result['status_code'] = BLC_LINK_STATUS_OK;
  112. $result['http_code'] = 0;
  113. } else {
  114. $result['broken'] = true;
  115. $result['status_text'] = __('Video Restricted', 'broken-link-checker');
  116. $result['status_code'] = BLC_LINK_STATUS_WARNING;
  117. $result['http_code'] = 0;
  118. }
  119. }
  120. //Add the video title to the log, purely for information.
  121. //http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_media:title
  122. $title = blcUtility::extract_tags($response['body'], 'media:title', false);
  123. if ( !empty($title) ){
  124. $result['log'] .= "\n\nTitle : \"" . $title[0]['contents'] . '"';
  125. }
  126. break;
  127. default:
  128. $result['log'] .= $result['http_code'] . $response['response']['message'];
  129. $result['log'] .= "\n" . __('Unknown YouTube API response received.');
  130. break;
  131. }
  132. }
  133. //The hash should contain info about all pieces of data that pertain to determining if the
  134. //link is working.
  135. $result['result_hash'] = implode('|', array(
  136. 'youtube',
  137. $result['http_code'],
  138. $result['broken']?'broken':'0',
  139. $result['timeout']?'timeout':'0',
  140. $state_name,
  141. $state_reason
  142. ));
  143. return $result;
  144. }
  145. }
  146. ?>