Automatically populating YouTube playlist upon new Embedded Video Field submission

Here's a little action I wrote recently, to automatically populate a specific YouTube playlist as new videos are being posted to a Drupal site, using modules Trigger or Rules. The same concept can also be used retroactively on existing content, using Views Bulk Operations: select all existing video nodes and apply the action to them. The action avoids adding the same video more than once.

To run this code, you will need to put the Zend Gdata library somewhere in your PHP include_path.

<?php
function module_hook_info() {
  return array(
   
'add_youtube_playlist' => array(
     
'type' => 'node',
     
'description' => t('Add video to YouTube "XYZ" playlist'),
     
'configurable' => FALSE,
    ),
  );
}

function
add_youtube_playlist($node, $context) {
  if (
$node->type != 'video_ad' || !isset($node->field_embed_video[0]) || $node->field_embed_video[0]['provider'] != 'youtube') return;
 
$video_id = $node->field_embed_video[0]['value'];

  static
$playlist = NULL;   
  require_once(
'Zend/Gdata/ClientLogin.php');
  require_once(
'Zend/Gdata/YouTube.php');
 
$authenticationUrl = 'https://www.google.com/youtube/accounts/ClientLogin';
 
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
   
YOUTUBE_USERNAME,
   
YOUTUBE_PASSWORD,
   
'youtube',
   
NULL,
   
YOUR_APPLICATION_NAME,
   
NULL,
   
NULL,
   
$authenticationUrl
 
);
 
$yt = new Zend_Gdata_YouTube($httpClient, YOUR_APPLICATION_NAME, YOUR_APPLICATION_NAME, YOUTUBE_DEVELOPER_KEY);
 
$postUrl = 'http://gdata.youtube.com/feeds/api/playlists/' . YOUTUBE_PLAYLIST_ID;
 
 
// Check in playlist
 
if (!is_array($playlist)) {
   
$playlist = array();
   
set_time_limit(0);
   
$playlistVideoFeed = $yt->getPlaylistVideoFeed($postUrl);
   
$playlistVideoFeed = $yt->retrieveAllEntriesForFeed($playlistVideoFeed); 
    foreach (
$playlistVideoFeed as $playlistVideoEntry) {
     
$parsedUrl = parse_url($playlistVideoEntry->getVideoWatchPageUrl());
     
parse_str($parsedUrl['query'], $parsedQuery);
     
$playlist[] = $parsedQuery['v'];
    }
  }
  if (
in_array($video_id, $playlist)) return;

 
// Add to playlist
 
try {
   
$videoEntryToAdd = $yt->getVideoEntry($video_id);
   
$newPlaylistListEntry = $yt->newPlaylistListEntry($videoEntryToAdd->getDOM());
   
$yt->insertEntry($newPlaylistListEntry, $postUrl);
   
$playlist[] = $video_id;
  }
  catch (
Exception $e) {
   
watchdog(YOUR_APPLICATION_NAME, 'Could not add video %vid to playlist: %error.',
      array(
'%vid' => $video_id, '%error' => $e->getMessage()),
     
WATCHDOG_ERROR
   
);
  }
}
?>

Assumptions:

  • The node type is called video_ad.
  • The video field name is called field_embed_video.
  • You need to define the following values:
    • YOUTUBE_USERNAME
    • YOUTUBE_PASSWORD
    • YOUTUBE_DEVELOPER_KEY
    • YOUTUBE_PLAYLIST_ID (found in the embed code of your playlist page)
    • YOUR_APPLICATION_NAME (any string)

Exercises to the reader:

  • Given a node, automatically find the embedded video field.
  • Generalize this approach to a configurable action that asks the user for a YouTube account credentials and allows to choose the playlist to populate.

Comments

Hey, i was looking to do this kind of thing with youtube and drupal. thanks for sharing the code you did, it will help me a lot!

Great code to submit a Video, i've used it..

Can you tell me how i get the last id of a created Playlist (PlaylistId )? the variable of this PlaylistId is part of the object _httpClient->last_response->headers->Location ...but it is protected! heres my Code:

<?php
   $newPlaylist
= $youTubeService->newPlaylistListEntry();

     
$newPlaylist->description = $youTubeService->newDescription()->setText($GET['playlistdesc'].' ');
     
$newPlaylist->title = $youTubeService->newTitle()->setText($GET['playlistname'].' ');
     
// post the new playlist
     
$postLocation = 'http://gdata.youtube.com/feeds/api/users/USER_NAME/playlists';
   
      try {
       
$youTubeService->insertEntry($newPlaylist, $postLocation);
   
      } catch (
Zend_Gdata_App_Exception $e) {
        echo
$e->getMessage();
      }
?>

Not sure... Did you try $newPlaylist->getPlaylistId()?

Cool! I did something similar with Flickr's API recently. I didn't go the actions and triggers route, though it wouldn't be hard to integrate some of this with the custom module I wrote - check it! http://drupal.org/project/flickr_imagefield