You are here

function scald_youtube_video_get_info in Scald YouTube 7

Returns meta data on a YouTube video that are not found on the v3 API.

Unfortunately the YouTube API doesn't provide original video dimensions see: http://code.google.com/p/gdata-issues/issues/detail?id=1083 We need both oembed data and parsing the web page to have author information and video dimensions.

2 calls to scald_youtube_video_get_info()
scald_youtube_feed in ./scald_youtube.module
Analyze a YouTube RSS feed to extract videos information.
scald_youtube_video_oembed in ./scald_youtube.module
Analyze OEmbed response for a given video ID.

File

./scald_youtube.module, line 525
Defines a YouTube provider for Scald.

Code

function scald_youtube_video_get_info($id) {
  static $cache = array();
  if (isset($cache[$id])) {
    return $cache[$id];
  }
  $info = array();
  $url = SCALD_YOUTUBE_WEB . '?v=' . $id;
  $options = array(
    'headers' => array(
      'User-Agent' => NULL,
    ),
  );
  $response = drupal_http_request($url, $options);
  if ($response->code >= 200 && $response->code < 400 && !empty($response->data)) {

    // Initialize default values.
    $info = scald_youtube_create_default_info($id);
    $data = $response->data;

    // DOMDocument does not work well with UTF-8, we need to use HTML entities
    // to be safe.
    if (function_exists('mb_convert_encoding')) {
      $data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
    }
    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = FALSE;
    @$dom
      ->loadHTML($data);
    foreach ($dom
      ->getElementsByTagName('meta') as $meta) {
      $content = $meta
        ->getAttribute('content');
      switch ($meta
        ->getAttribute('name')) {
        case 'keywords':
          $info->tags = explode(', ', $content);
          break;
        case 'title':
          $info->title = $content;
          break;
      }
      switch ($meta
        ->getAttribute('property')) {
        case 'og:image':
          $info->thumbnail = array(
            'src' => $content,
          );
          break;
        case 'og:video:width':
          $info->width = $content;
          $info->video_width = $content;
          break;
        case 'og:video:height':
          $info->height = $content;
          $info->video_height = $content;
          break;
      }
    }
    if (empty($info->title)) {
      $info = array();
    }
  }
  $cache[$id] = $info;
  return $info;
}