kishanjasani /
SociaManager
| 1 | <?php |
||
| 2 | //Gearman Library |
||
| 3 | require_once 'LibGearman.php'; |
||
| 4 | |||
| 5 | require __DIR__ . '/vendor/autoload.php'; |
||
| 6 | /** |
||
| 7 | * Background Job Processor For Move Album To drive |
||
| 8 | * @category php |
||
| 9 | * @package SocialManager |
||
| 10 | * @author Kishan Jasani <[email protected]> |
||
| 11 | * @license https://github.com/kishanjasani/SociaManager |
||
| 12 | */ |
||
| 13 | class BackgroundWorker { |
||
| 14 | |||
| 15 | public $main_arr = array(); |
||
| 16 | |||
| 17 | public function worker() { |
||
| 18 | $gearman = new LibGearman(); |
||
| 19 | $worker = $gearman->gearman_worker(); |
||
| 20 | $gearman->add_worker_function('moveToDrive', 'BackgroundWorker::moveToDrive'); |
||
| 21 | $gearman->add_worker_function('getAlbum', 'BackgroundWorker::getAlbum'); |
||
| 22 | |||
| 23 | while ($gearman->work()) { |
||
| 24 | if (!$worker->returnCode()) { |
||
| 25 | echo date('c') . " worker done successfully \n"; |
||
| 26 | } |
||
| 27 | if ($worker->returnCode() != GEARMAN_SUCCESS) { |
||
| 28 | echo "return_code: " . $gearman->current('worker')->returnCode() . "\n"; |
||
| 29 | break; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Remove the album directory |
||
| 36 | * |
||
| 37 | * @param String $accessToken access token of facebook |
||
| 38 | * @param String $albumId album id to store data into drive |
||
| 39 | * @param String $albumName album name |
||
| 40 | * @param String $fb facebook object |
||
| 41 | * @param String $drive drive object |
||
| 42 | * @param String $parentFolderId ParentId |
||
| 43 | * |
||
| 44 | * @return "" |
||
| 45 | */ |
||
| 46 | |||
| 47 | public static function moveToDrive($job = null) { |
||
| 48 | $data = unserialize($job->workload()); |
||
| 49 | global $main_arr; |
||
| 50 | $fb = new Facebook\Facebook( |
||
| 51 | [ |
||
| 52 | 'app_id' => 'XXXXXXXXXX', |
||
| 53 | 'app_secret' => 'XXXXXXXXXX', |
||
| 54 | 'default_graph_version' => 'v2.2' |
||
| 55 | ] |
||
| 56 | ); |
||
| 57 | $client = new Google_Client(); |
||
| 58 | $client->setAuthConfigFile(__DIR__ . '/client_secret.json'); |
||
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
| 59 | $client->setAccessType("online"); |
||
| 60 | $client->setIncludeGrantedScopes(true); |
||
| 61 | $client->addScope(Google_Service_Drive::DRIVE); |
||
| 62 | $client->setAccessToken($data['google_access_token']); |
||
| 63 | $fileMetadata = new Google_Service_Drive_DriveFile( |
||
| 64 | array( |
||
| 65 | 'name' => $data['albumName'], |
||
| 66 | 'mimeType' => 'application/vnd.google-apps.folder', |
||
| 67 | 'parents' => array($data['parentFolderId'])) |
||
| 68 | ); |
||
| 69 | $drive = new Google_Service_Drive($client); |
||
| 70 | $SubFolder = $drive->files->create( |
||
| 71 | $fileMetadata, |
||
| 72 | array('fields' => 'id') |
||
| 73 | ); |
||
| 74 | |||
| 75 | $request_albums_photo = $fb->get($data['albumId'] ."/photos?fields=images&limit=100", $data['accessToken']); |
||
| 76 | $arr_alb = $request_albums_photo->getGraphEdge(); |
||
| 77 | $i = 0; |
||
| 78 | $resultAlbum = self::getAlbum($fb, $arr_alb, $i); |
||
| 79 | |||
| 80 | $count = 1; |
||
| 81 | foreach ($resultAlbum as $images) { |
||
| 82 | $url = $images['images']; |
||
| 83 | $img = $count.".jpeg"; |
||
| 84 | $folderId = $SubFolder->id; |
||
| 85 | $fileMetadata = new Google_Service_Drive_DriveFile( |
||
| 86 | array( |
||
| 87 | 'name' => $img, |
||
| 88 | 'parents' => array($folderId)) |
||
| 89 | ); |
||
| 90 | try { |
||
| 91 | $fileContent = file_get_contents($url); |
||
| 92 | $file = $drive->files->create( |
||
| 93 | $fileMetadata, |
||
| 94 | array( |
||
| 95 | 'data' => $fileContent, 'mimeType' => 'image/jpeg', |
||
| 96 | 'uploadType' => 'multipart', 'fields' => 'id') |
||
| 97 | ); |
||
| 98 | }catch (Exception $e) { |
||
| 99 | print "An error occurred: " . $e->getMessage(); |
||
| 100 | } |
||
| 101 | |||
| 102 | $count++; |
||
| 103 | } |
||
| 104 | $main_arr = array(); |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function getAlbum($fb,$arr_alb,$i) |
||
| 108 | { |
||
| 109 | global $main_arr; |
||
| 110 | foreach ($arr_alb as $graphNode) { |
||
| 111 | $main_arr[$i]['images'] = $graphNode['images'][0]['source']; |
||
| 112 | $i++; |
||
| 113 | } |
||
| 114 | $arr_alb = $fb->next($arr_alb); |
||
| 115 | if(!empty($arr_alb)) { |
||
| 116 | self::getAlbum($fb, $arr_alb, $i); |
||
| 117 | } |
||
| 118 | return $main_arr; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | $background = new BackgroundWorker(); |
||
| 123 | $background->worker(); |
||
| 124 |