Beispiel #1
0
    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;
    }
Beispiel #2
0
 public static function detail()
 {
     $taskId = (int) $_POST['id'];
     $task = TasksModel::details($taskId);
     if ($task['services']) {
         foreach ($task['services'] as $service) {
             if ($service['id'] == $_POST['type']) {
                 $_service = $service;
             }
         }
     }
     $kinds = array();
     $graphData = array();
     switch ($_service['name']) {
         case 'Youtube':
             $kinds = array(Youtube::$kind['playlist'], Youtube::$kind['subscription']);
             // Graph data
             $graphData['source']['playlists'] = PlaylistsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id']))->toArray();
             $graphData['source']['subscriptions'] = SubscriptionsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id']))->toArray();
             $graphData['destination']['playlists'] = PlaylistsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id']))->toArray();
             $graphData['destination']['subscriptions'] = SubscriptionsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id']))->toArray();
             break;
         case 'Contacts':
             $kinds = array(Contacts::$kind['contact']);
             // Graph data
             $graphData['source']['contacts'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id'], 'entity_type' => Contacts::$kind['contact']))->toArray();
             $graphData['destination']['contacts'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id'], 'entity_type' => Contacts::$kind['contact']))->toArray();
             break;
         case 'Tasks':
             $kinds = array(Tasks::$kind['list']);
             // Graph data
             $graphData['source']['lists'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id'], 'entity_type' => Tasks::$kind['list']))->toArray();
             $graphData['destination']['lists'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id'], 'entity_type' => Tasks::$kind['list']))->toArray();
             break;
         case 'Calendar':
             $kinds = array(Calendar::$kind['calendar']);
             // Graph data
             $graphData['source']['calendars'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id'], 'entity_type' => Calendar::$kind['calendar']))->toArray();
             $graphData['destination']['calendars'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id'], 'entity_type' => Calendar::$kind['calendar']))->toArray();
             break;
     }
     // Migrated Data
     $migratedData = array();
     $migrated = MigratedDataModel::all(array('sync_task_id' => $taskId, 'kind' => $kinds))->toArray();
     if ($migrated) {
         foreach ($migrated as $migrate) {
             switch ($migrate['kind']) {
                 case Youtube::$kind['playlist']:
                     $play = PlaylistsModel::first($migrate['table_id'])->toArray();
                     $migratedData['playlists'][] = $play;
                     $migratedData['playlistsGraph'][$migrate['source_id']][] = $play;
                     $migratedData['playlistsIds'][] = $play['id'];
                     break;
                 case Youtube::$kind['subscription']:
                     $subs = SubscriptionsModel::first($migrate['table_id'])->toArray();
                     $migratedData['subscriptions'][] = $subs;
                     $migratedData['subscriptionsGraph'][$migrate['source_id']][] = $subs;
                     $migratedData['subscriptionsIds'][] = $subs['id'];
                     break;
                 case Contacts::$kind['contact']:
                     $contact = BackupsModel::first($migrate['table_id'])->toArray();
                     $migratedData['contacts'][] = $contact;
                     $migratedData['contactsGraph'][$migrate['source_id']][] = $contact;
                     $migratedData['contactsIds'][] = $contact['id'];
                     break;
                 case Tasks::$kind['list']:
                     $contact = BackupsModel::first($migrate['table_id'])->toArray();
                     $migratedData['lists'][] = $contact;
                     $migratedData['listsGraph'][$migrate['source_id']][] = $contact;
                     $migratedData['listsIds'][] = $contact['id'];
                     break;
                 case Calendar::$kind['calendar']:
                     $calendar = CalendarsModel::first($migrate['table_id'])->toArray();
                     $migratedData['calendars'][] = $calendar;
                     $migratedData['calendarsGraph'][$migrate['source_id']][] = $calendar;
                     $migratedData['calendarsIds'][] = $calendar['id'];
                     break;
             }
         }
     }
     echo \Render::view('migrate/details/' . strtolower($_service['name']), array('task' => $task, 'service' => $_service, 'migratedData' => $migratedData, 'graphData' => $graphData), 'return');
 }