You are here

function media_youtube_upload_video in Media: YouTube 6

Upload a video to YouTube through Media Mover.

1 call to media_youtube_upload_video()
media_youtube_media_mover in ./media_youtube.module
Implementation of media_mover hook

File

includes/media_youtube.media_mover.inc, line 11
Functions to implement Media Mover behavior for Media: YouTube.

Code

function media_youtube_upload_video($file, $configuration) {
  _media_youtube_set_include_path();
  $path = media_youtube_zend_path();
  Zend_Loader::loadClass('Zend_Gdata_ClientLogin', $path);
  Zend_Loader::loadClass('Zend_Gdata_YouTube', $path);
  Zend_Loader::loadClass('Zend_Gdata_App_Exception', $path);
  Zend_Loader::loadClass('Zend_Gdata_App_HttpException', $path);
  $title = check_plain($configuration['media_youtube_default_title']);
  $description = check_plain($configuration['media_youtube_default_description']);
  $tags = '';
  $node = NULL;
  if ($file['nid']) {
    $node = node_load($file['nid']);
    if ($node) {
      $title = check_plain($node->title);
      $description_field = $configuration['media_youtube_description_field'];
      if ($configuration['media_youtube_description_field'] == 'body') {
        $clean_description = check_plain($node->body);
      }
      else {
        if ($node->{$configuration['media_youtube_description_field']}) {
          $clean_description = check_plain($node->{$configuration['media_youtube_description_field']}[0]['value']);
        }
      }
      if ($clean_description) {
        $description = $clean_description;
      }
      $tags = array();
      if ($node->taxonomy) {
        foreach ($node->taxonomy as $term) {
          if ($configuration['media_youtube_vocabs'][$term->vid]) {
            $ntags = explode(' ', $term->name);
            $tags = array_merge($tags, $ntags);
          }
        }
      }
      $tags = array_filter($tags, '_media_youtube_filter_tags');
      $tags = check_plain(implode(', ', $tags));
    }
    else {
      watchdog('media_youtube', 'File !file belongs to node !nid which does not exist anymore.', array(
        '!file' => $filepath,
        '!nid' => $file['nid'],
      ), WATCHDOG_ERROR);
    }
  }
  if ($tags == '') {
    $tags = check_plain($configuration['media_youtube_default_tags']);
  }
  $username = media_youtube_variable_get('youtube_username');
  $password = media_youtube_variable_get('youtube_password');
  $devkey = media_youtube_variable_get('api_key');
  $category = check_plain($configuration['media_youtube_category']);
  $filepath = media_mover_api_config_current_file($file);
  $fileinfo = pathinfo($filepath);
  try {
    $httpclient = Zend_Gdata_ClientLogin::getHttpClient($username, $password, 'youtube', NULL, MEDIA_YOUTUBE_APP_ID, NULL, NULL, MEDIA_YOUTUBE_AUTH_URL);
  } catch (Exception $e) {
    watchdog('media_youtube', 'Cannot authenticate. Error: @error', array(
      '@error' => strip_tags($e
        ->getMessage()),
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  $youtube = new Zend_Gdata_YouTube($httpclient, MEDIA_YOUTUBE_APP_ID, NULL, $devkey);
  $upload = new Zend_Gdata_YouTube_VideoEntry();
  $filesource = $youtube
    ->newMediaFileSource($filepath);
  $filesource
    ->setContentType('video/avi');
  $filesource
    ->setSlug($filepath);
  $upload
    ->setMediaSource($filesource);
  $upload
    ->setVideoTitle($title);
  $upload
    ->setVideoDescription($description);
  $upload
    ->setVideoCategory($category);
  $upload
    ->setVideoTags($tags);
  try {
    $video = $youtube
      ->insertEntry($upload, MEDIA_YOUTUBE_UPLOAD_URL, 'Zend_Gdata_YouTube_VideoEntry');
    $url = media_youtube_video_url($video
      ->getVideoId());
    $proto = _media_youtube_protocol();
    watchdog('media_youtube', 'Uploaded file !file to Youtube: !video', array(
      '!file' => $fileinfo['basename'],
      '!video' => l($url, $url),
    ), WATCHDOG_NOTICE, l($title, $proto . 'www.youtube.com/watch?v=' . $story_id, array(
      'absolute' => TRUE,
    )));
    return $url;
  } catch (Zend_Gdata_App_HttpException $httpException) {
    watchdog('media_youtube', 'Upload failed. Error: @error. Exception: @exception.', array(
      '@error' => strip_tags($httpException
        ->getMessage()),
      '@exception' => strip_tags($httpException
        ->getRawResponseBody()),
    ), WATCHDOG_ERROR, $node ? l($node->title, 'node/' . $node->nid) : NULL);
    return FALSE;
  } catch (Zend_Gdata_App_Exception $e) {
    watchdog('media_youtube', print_r($file, TRUE));
    watchdog('media_youtube', 'Upload failed. Error: @error.', array(
      '@error' => strip_tags($e
        ->getMessage()),
    ), WATCHDOG_ERROR, $node ? l($node->title, 'node/' . $node->nid) : NULL);
    return FALSE;
  }
}