You are here

function media_youtube_emfield_data in Media: YouTube 6

3 calls to media_youtube_emfield_data()
emvideo_youtube_data in providers/emvideo/youtube.inc
hook emfield_PROVIDER_data
media_youtube_raw_url in ./media_youtube.module
Return the URL to the raw flash.
_media_youtube_update_fetch_metadata in ./media_youtube.install
Batch function to retrieve the most recent data from providers.

File

./media_youtube.module, line 525
Embedded Video Field provider file for YouTube.com.

Code

function media_youtube_emfield_data($video_id) {
  $data = array();

  // Create some 'field' version control.
  $data['emvideo_youtube_version'] = $data['emvideo_data_version'] = MEDIA_YOUTUBE_DATA_VERSION;

  // Store the raw data from YouTube's API.
  $raw = media_youtube_request_metadata($video_id);
  if (media_youtube_variable_get('store_raw_metadata')) {
    $data['raw'] = $raw;
  }

  // Store the video's duration.
  if (isset($raw['MEDIA:GROUP']['YT:DURATION'][1]['SECONDS'])) {
    $data['duration'] = intval($raw['MEDIA:GROUP']['YT:DURATION'][1]['SECONDS']);
  }
  else {
    $data['duration'] = 0;
  }

  // Gather info about the item's raw flash video.
  // RSS / MRSS feeds with the item would have enough info.
  // Alternatively try getting the minimum from an HTTP get.
  // Get info from a youtube playlist.
  $proto = _media_youtube_protocol();
  if (strpos($video_id, 'PLAYLIST_') === 0) {
    $playlist_id = substr($video_id, 9);
    $url = $proto . 'youtube.com/p/' . $playlist_id;
    $data['playlist'] = 1;

    // Get the large thumbnail of the first video.
    // Use the Youtube Google API to get this data.
    $api_url = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist_id;
    $result = drupal_http_request($api_url);
    if ($result->code == 200) {
      $parser = drupal_xml_parser_create($result->data);
      $vals = array();
      $index = array();
      xml_parse_into_struct($parser, $result->data, $vals, $index);
      xml_parser_free($parser);
      if (count($vals)) {
        foreach ($vals as $val) {
          if ($val['tag'] == 'MEDIA:THUMBNAIL' && $val['attributes']['HEIGHT'] >= 240) {
            $data['thumbnail']['url'] = $val['attributes']['URL'];
            break;
          }
        }
      }
    }
  }
  else {
    $url = $proto . 'youtube.com/v/' . $video_id;
    $data['playlist'] = 0;

    // Get the large thumbnail.
    $data['thumbnail']['url'] = $proto . 'img.youtube.com/vi/' . $video_id . '/0.jpg';
  }
  $response = emfield_request_header('youtube', $url);
  if ($response->code == 200) {

    // Don't give the 303 path.
    $data['flash']['url'] = $url;
    $data['flash']['size'] = $response->headers['Content-Length'];
    $data['flash']['mime'] = $response->headers['Content-Type'];
  }
  return $data;
}