You are here

function asset_youtube_sync in Asset 5

Same name and namespace in other branches
  1. 6 asset_youtube/asset_youtube.module \asset_youtube_sync()
1 call to asset_youtube_sync()
asset_youtube_asset_type in asset_youtube/asset_youtube.module

File

asset_youtube/asset_youtube.module, line 231

Code

function asset_youtube_sync() {
  global $user;
  $videos = $favorites = 0;
  if (!$user->asset_youtube['username']) {
    drupal_set_message('You must first enter your youtube user name.');
    return;
  }
  asset_check_directory($user->name, FILE_CREATE_DIRECTORY);
  $yt_dir = $user->name . '/My YouTube';
  asset_check_directory($yt_dir, FILE_CREATE_DIRECTORY);

  // Get user's videos
  $result = asset_youtube_call('youtube.videos.list_by_user');
  if (is_array($result->document->video_list[0]->video)) {
    $dir = $yt_dir . '/My Videos';
    if (asset_check_directory($dir, FILE_CREATE_DIRECTORY)) {
      foreach ($result->document->video_list[0]->video as $video) {
        asset_youtube_save_asset($video, $dir);
        $videos++;
      }
    }
    else {
      drupal_set_message('Error creating <em>My YouTube Videos</em> folder.', 'error');
    }
  }

  // Get user's favorites
  $result = asset_youtube_call('youtube.users.list_favorite_videos');
  if (is_array($result->document->video_list[0]->video)) {
    $dir = $yt_dir . '/My Favorites';
    if (asset_check_directory($dir, FILE_CREATE_DIRECTORY)) {
      foreach ($result->document->video_list[0]->video as $video) {
        asset_youtube_save_asset($video, $dir);
        $favorites++;
      }
    }
    else {
      drupal_set_message('Error creating <em>My YouTube Videos</em> folder.', 'error');
    }
  }
  return "saved {$videos} videos and {$favorites} favorites";
}