Experimental project

This is a sandbox project, which contains experimental code for developer use only.

Youtube API for Drupal 7.x. Update project: https://drupal.org/project/youtube_api using https://drupal.org/project/gauth.

Requires: https://code.google.com/p/google-api-php-client/downloads/detail?name=go...

Here is some example code. This is a rule I'm using with VBO. The videos are part of organic groups.

function dma_youtube_upload($node, $file) {

  // If this node is in a organic group use the group as the playlist

  list(,, $bundle) = entity_extract_ids('node', $node);
  $og_field = reset(array_keys(og_get_group_audience_fields('node', $bundle)));
  $wrapper = entity_metadata_wrapper('node', $node);
  $group_node = reset($wrapper->{$og_field}->value());

  $user_id = $group_node ? $group_node->uid : $node->uid;

  // Auth to youtube ...
  $account_id = gauth_account_load_by_uid($user_id);
  if (!$account_id) {
    drupal_set_message(t('User has no associated youtube account.'), 'error');
    return;
  }
  if (!gauth_account_is_authenticated($account_id)) {
    drupal_set_message(t('Youtube account is not authenticated.'), 'error');
    return;
  }
  $langcode = 'und';
  
  // Create playlist ...
  if ($group_node) {
    if (!property_exists($group_node, 'playlist_id')) {
      $params = array(
        'snippet' => array(
          'title' => $group_node->title,
          'description' => drupal_html_to_text($group_node->body[$langcode][0]['value']),
        ),
        'status' => array(
          'privacyStatus' => $group_node->status ? 'public' : 'private',
        ),
      );
      $playlist = youtube_api_playlist_insert($params, $account_id);

      if (array_key_exists('id', $playlist)) {
        db_insert('dma_youtube_playlist')->fields(array(
          'nid' => $group_node->nid,
          'playlist_id' => $playlist['id'],
        ))->execute();
        $group_node->playlist_id = $playlist['id'];
      } else {
        drupal_set_message(t('Youtube unable to create playlist.'), 'error');
        return;
      }
    }
    $playlist_id = $group_node->playlist_id;
  }
  // Upload video ....
  $tids = array();

  foreach($node->taxonomy_vocabulary_19[$langcode] as $delta => $item) {
    $tids[$item['tid']] = $item['tid'];
  }
  $terms = taxonomy_term_load_multiple($tids);

  $tags = array();
  foreach ($terms as $delta => $term) {
    $tags[] = trim($term->name);
  }

  // TODO allow for selecting category.
  $categoryId = '22';
  $params = array(
    'snippet' => array(
      'title' => $node->title,
      'description' => drupal_html_to_text($node->body[$langcode][0]['value']),
      'tags' => $tags,
      'categoryId' => $categoryId,
    ),
    'status' => array(
      'privacyStatus' => $node->status ? 'public' : 'private',
      'license'       => 'creativeCommon',
      'embeddable'    => true,
      'publicStatsViewable' => true,
    ),
  );
  $filepath = drupal_realpath($file->uri);
  $video = youtube_api_video_insert($filepath, $params, $account_id);
  if(!array_key_exists('id', $video)) {
    return;
  }

  // Add video to playlist ...
  if ($playlist_id) {
    youtube_api_playlist_item_insert($video['id'], $playlist_id, array(), $account_id);
  }
  // Create derivitive ....

}

Project information

  • Project categories: Media
  • Created by darrick on , updated