public static function _transfer($source, $destination, $syncTaskId = 0, $ignoreUpdate = false, $whitelist = array()) { // Stats $stats = array('playlists' => 0, 'videos' => 0, 'subscriptions' => 0); // Get source data $playlists = PlaylistsModel::withVideos($source['username']['id'], $syncTaskId); $destinationPlaylists = PlaylistsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId))->toArray(); $destinationNames = static::_getColumn($destinationPlaylists, 'title'); $subscriptions = SubscriptionsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId))->toArray(); $syncedVideos = VideosModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId))->column('video_link'); $syncedSubscriptions = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'destination_id' => $destination['username']['id'], 'kind' => static::$kind['subscription'], 'status' => MigratedDataModel::STATUS_ACTIVE))->column('identifier'); $counter = 0; $batches = array(); $batch = md5(time()); // Add data to destionation Youtube if ($playlists) { foreach ($playlists as $playlist) { if (intVal($playlist['videos_count']) > 0) { if ($counter == 998) { $batch = md5(time()); $counter = 0; } // Create Playlist $payload = array(); $payload['status']['privacyStatus'] = 'private'; // Whitelisting used for share feature $whiteListed = true; if ($whitelist) { if (isset($whitelist['playlists']) && $whitelist['playlists']) { if (!in_array($playlist['youtube_playlist_id'], $whitelist['playlists'])) { $whiteListed = false; } } } // New if (!in_array($playlist['title'], array_keys($destinationNames)) && $whiteListed || $ignoreUpdate) { // Create Playlist $payload['snippet']['title'] = $playlist['title']; $newPlaylist = \Rest::postJSON(static::$endpoints['playlists'] . '?part=status,snippet', $payload, $destination); $stats['playlists']++; if (isset($newPlaylist['result']['error'])) { d($newPlaylist); } $syncPlaylist = MigratedDataModel::create(); $syncPlaylist->source_id = $source['username']['id']; $syncPlaylist->destination_id = $destination['username']['id']; $syncPlaylist->task_id = 0; $syncPlaylist->sync_task_id = $syncTaskId; $syncPlaylist->table = PlaylistsModel::$schema['table']; $syncPlaylist->table_id = $playlist['id']; $syncPlaylist->kind = static::$kind['playlist']; $syncPlaylist->identifier = $playlist['youtube_playlist_id']; $syncPlaylist->name = $playlist['title']; $syncPlaylist->created = date(DATE_TIME); $syncPlaylist->status = MigratedDataModel::STATUS_ACTIVE; $syncPlaylist->save(); // Update playlist with new id if (!$ignoreUpdate) { $oldPlaylist = PlaylistsModel::first($playlist['id']); $oldPlaylist->new_youtube_id = $newPlaylist['id']; $oldPlaylist->save(); } // Existing } else { $play = PlaylistsModel::first(array('user_id' => $destination['username']['id'], 'title' => $playlist['title']))->toArray(); $newPlaylist['id'] = $play['youtube_playlist_id']; } $counter++; // Add videos if ($playlist['videos']) { foreach ($playlist['videos'] as $k => $video) { if (!in_array($video['video_link'], $syncedVideos) || $ignoreUpdate) { if ($counter == 998) { $batch = md5(time()); $counter = 0; } $counter++; $payload = array(); $payload['snippet']['playlistId'] = $newPlaylist['id']; $payload['snippet']['resourceId']['kind'] = static::$kind['video']; $payload['snippet']['resourceId']['videoId'] = $video['video_link']; $payload = json_encode($payload); $batches[$batch][$newPlaylist['id']][] = <<<EOD POST https://www.googleapis.com/youtube/v3/playlistItems?part=snippet Content-Type: application/json Authorization: Bearer {$destination['credentials']['access_token']} {$payload} EOD; $stats['videos']++; $syncPlaylist = MigratedDataModel::create(); $syncPlaylist->source_id = $source['username']['id']; $syncPlaylist->destination_id = $destination['username']['id']; $syncPlaylist->task_id = 0; $syncPlaylist->sync_task_id = $syncTaskId; $syncPlaylist->table = VideosModel::$schema['table']; $syncPlaylist->table_id = $video['id']; $syncPlaylist->kind = static::$kind['video']; $syncPlaylist->identifier = $video['video_link']; $syncPlaylist->name = $video['title']; $syncPlaylist->created = date(DATE_TIME); $syncPlaylist->status = MigratedDataModel::STATUS_ACTIVE; $syncPlaylist->save(); } } } } } } // Add subscriptions if ($subscriptions) { foreach ($subscriptions as $subscription) { // Whitelisting used for share feature $whiteListed = true; if ($whitelist) { if (isset($whitelist['subscriptions']) && $whitelist['subscriptions']) { if (!in_array($subscription['channel_link'], $whitelist['subscriptions'])) { $whiteListed = false; } } } if (!in_array($subscription['channel_link'], $syncedSubscriptions) && $whiteListed || $ignoreUpdate) { if ($counter == 998) { $batch = md5(time()); $counter = 0; } $counter++; $payload = array(); $payload['snippet']['resourceId']['kind'] = static::$kind['channel']; $payload['snippet']['resourceId']['channelId'] = $subscription['channel_link']; $payload = json_encode($payload); $batches[$batch][$subscription['channel_link']][] = <<<EOD POST https://www.googleapis.com/youtube/v3/subscriptions?part=snippet Content-Type: application/json Authorization: Bearer {$destination['credentials']['access_token']} {$payload} EOD; $stats['subscriptions']++; $syncPlaylist = MigratedDataModel::create(); $syncPlaylist->source_id = $source['username']['id']; $syncPlaylist->destination_id = $destination['username']['id']; $syncPlaylist->task_id = 0; $syncPlaylist->sync_task_id = $syncTaskId; $syncPlaylist->table = SubscriptionsModel::$schema['table']; $syncPlaylist->table_id = $subscription['id']; $syncPlaylist->kind = static::$kind['subscription']; $syncPlaylist->identifier = $subscription['channel_link']; $syncPlaylist->name = $subscription['title']; $syncPlaylist->created = date(DATE_TIME); $syncPlaylist->status = MigratedDataModel::STATUS_ACTIVE; $syncPlaylist->save(); } } } if ($batches) { foreach ($batches as $key => $batch) { for ($i = 0; $i < 500; $i++) { $body = ''; $y = md5($i); foreach ($batch as $k => $set) { $keys = $k . '-' . $i; if (isset($set[$i])) { $body .= <<<EOD --{$y} Content-Type: application/http Content-Transfer-Encoding: binary MIME-Version: 1.0 Content-ID:{$keys} {$set[$i]} EOD; } } if ($body) { $body .= "--" . $y . "--"; $destination['boundary'] = $y; $insert = \Rest::postRaw('https://www.googleapis.com/batch', $body, $destination); } } } } return $stats; }