You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
4.1 KiB

<?php
namespace App\Http\Controllers;
use App\GooglePhoto;
use Google\ApiCore\ApiException;
use Google\Photos\Library\V1\PhotosLibraryClient;
use Google\Photos\Types\Album;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class GooglePhotoController extends Controller {
public function connect(Request $request)
{
return connectWithGooglePhotos($request,
['https://www.googleapis.com/auth/photoslibrary'],
config('app.albums_authentication_redirect_url')
);
}
public function index()
{
$credentials = session("credentials");
// $credentials = null;
if ($credentials == null || $credentials == "") {
return view("albums/connect");
}
$photosLibraryClient = new PhotosLibraryClient(['credentials' => $credentials]);
try {
// $options['proxy'] = 'http://127.0.0.1:1087';
// $pagedResponse = $photosLibraryClient->listAlbums();
// $iterator = $pagedResponse->iterateAllElements();
// for ($i = 0; $i < 300; $i++) {
// $album = $iterator->current();
// Log::info("album name is" . $album->getTitle() . " id is " . $album->getId());
// $iterator->next();
// }
// exit;
$options['pageSize'] = 100;
$pagedResponse = $photosLibraryClient->listMediaItems($options);
// $album = new Album();
// $album->setTitle("ycc_gc");
// $album = $photosLibraryClient->createAlbum($album);
// Log::info($album->getId());
// echo 111;
// var_dump($pagedResponse->iterateAllElements());exit;
$iterator = $pagedResponse->iterateAllElements();
$albums = [];
$yccImages = [];
while (true) {
for ($i = 0; $i < 100000; $i++) {
if ($i < 5600) {
$iterator->next();
}
$photo = $iterator->current();
// Log::info($photo);
Log::info($photo->getFilename());
GooglePhoto::firstOrCreate(["photo_id" => $photo->getId()],
["photo_id" => $photo->getId(),
"filename" => $photo->getFilename(),
"product_url" => $photo->getProductUrl(),
"creation_time" => date("Y-m-d H:i:s", $photo->getMediaMetadata()->getCreationTime()->getSeconds())]
);
// exit;
// dump($photo);
// if (strstr($photo->getFilename(), "ycc")) {
// $yccImages[] = $photo->getId();
// }
$iterator->next();
}
// if (count($yccImages) > 1) {
// $yccImages = array_slice($yccImages, 89);
// Log::info( "prepare add " . count($yccImages) . " images to ycc");
// Log::info("imageIDs", $yccImages);
// try {
// $photosLibraryClient->batchAddMediaItemsToAlbum("AN5jk27_faHwGa9qT0SALztx3zxTk4HUiqhVIDjxHQBxZ59v1ljIyA8Klsn2ZaxlFrRJwBAPPdmZ", $yccImages);
// } catch (ApiException $e) {
// Log::error($e->getMessage());
// }
// Log::info( "add " . count($yccImages) . " images to ycc");
// $yccImages = [];
// }
// break;
}
//// echo $album->getProductUrl();
// $albums[] = $album;
// $iterator->next();
// }
// exit;
// By using iterateAllElements, pagination is handled for us.
return view("albums.index", ['albums' => $pagedResponse->iterateAllElements()]);
// echo $templates->render(
// 'albums::index',
// ['albums' => $pagedResponse->iterateAllElements()]);
// echo $templates->render(
// 'albums::index',
// ['mediaItems' => $albums]
// );
// );
} catch (ApiException $e) {
// echo $templates->render('error', ['exception' => $e]);
view("error");
}
}
}