You are here

function emvideo_youtube_data in Embedded Media Field 6

hook emfield_PROVIDER_data

provides an array to be serialised and made available with $item elsewhere

2 calls to emvideo_youtube_data()
emvideo_youtube_duration in contrib/emvideo/providers/youtube.inc
hook emvideo_PROVIDER_duration($item) Returns the duration of the video in seconds.
emvideo_youtube_rss in contrib/emvideo/providers/youtube.inc
hook emfield_PROVIDER_rss

File

contrib/emvideo/providers/youtube.inc, line 207
This include processes youtube.com media files for use by emfield.module.

Code

function emvideo_youtube_data($field, $item) {
  $data = array();

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

  // Store the raw data from YouTube's API.
  $data['raw'] = emvideo_youtube_request($item['value']);

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

  // 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.
  if (strpos($item['value'], 'PLAYLIST_') === 0) {
    $playlist_id = substr($item['value'], 9);
    $url = 'http://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 = 'http://youtube.com/v/' . $item['value'];
    $data['playlist'] = 0;

    // Get the large thumbnail.
    $data['thumbnail']['url'] = 'http://img.youtube.com/vi/' . $item['value'] . '/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;
}