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.
 
 
 

127 lines
6.3 KiB

<?php
namespace App\Services;
use App\GooglePhoto;
use Google\Auth\Credentials\UserRefreshCredentials;
use Google\Auth\OAuth2;
use Google\Photos\Library\V1\PhotosLibraryClient;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class GooglePhotoSyncService {
private $map = [
"/Users/shixuesen/Documents/local/xg/zz" => "/Users/shixuesen/Documents/sync/image/芝芝Booty",
"/Users/shixuesen/Documents/local/xg/wyc" => "/Users/shixuesen/Documents/sync/image/王雨纯",
"/Users/shixuesen/Documents/local/xg/ycc" => "/Users/shixuesen/Documents/sync/image/杨晨晨",
"/Users/shixuesen/Documents/local/xg/hlr" => "/Users/shixuesen/Documents/sync/image/黄乐然",
"/Users/shixuesen/Documents/local/xg/jrq" => "/Users/shixuesen/Documents/sync/image/姜仁卿",
"/Users/shixuesen/Documents/local/xg/azu" => "/Users/shixuesen/Documents/sync/image/是阿朱啊",
"/Users/shixuesen/Documents/local/xg/xq" => "/Users/shixuesen/Documents/sync/image/小琪",
"/Users/shixuesen/Documents/local/xg/小雪" => "/Users/shixuesen/Documents/sync/image/小雪",
"/Users/shixuesen/OneDrive/Pictures/instagram/Likes_new" => "/Users/shixuesen/Documents/sync/image/instagram/Likes",
"/Users/shixuesen/OneDrive/Pictures/instagram/boram__jj" => "/Users/shixuesen/Documents/sync/image/instagram/boram__jj",
"/Users/shixuesen/OneDrive/Pictures/instagram/cho_hyunyoung" => "/Users/shixuesen/Documents/sync/image/instagram/cho_hyunyoung",
"/Users/shixuesen/OneDrive/Pictures/instagram/cxxsomi" => "/Users/shixuesen/Documents/sync/image/instagram/cxxsomi",
"/Users/shixuesen/OneDrive/Pictures/instagram/inkyung97" => "/Users/shixuesen/Documents/sync/image/instagram/inkyung97",
"/Users/shixuesen/OneDrive/Pictures/instagram/chen_01_24" => "/Users/shixuesen/Documents/sync/image/instagram/chen_01_24",
"/Users/shixuesen/OneDrive/Pictures/instagram/mobe_carrie0223" => "/Users/shixuesen/Documents/sync/image/instagram/mobe_carrie0223",
"/Users/shixuesen/OneDrive/Pictures/instagram/na0912mi" => "/Users/shixuesen/Documents/sync/image/instagram/na0912mi",
"/Users/shixuesen/OneDrive/Pictures/instagram/sejinming" => "/Users/shixuesen/Documents/sync/image/instagram/sejinming",
];
public function syncImageFiles()
{
$service = new FileService();
$service->copySelectedFilesToMappedDirectory($this->map);
}
public function syncDBToRedis()
{
$list = GooglePhoto::paginate(100);
do {
foreach ($list as $item) {
Redis::connection()->sadd("google_photo", $item["photo_id"]);
}
$list = GooglePhoto::paginate(100, ["*"],"", $list->currentPage() + 1);
} while($list->isNotEmpty());
}
public function loadSynchronizedPhotoFromGooglePhoto($credentials,PhotosLibraryClient $photosLibraryClient)
{
$options["pageSize"] = 100;
if (Redis::connection()->get("google_photo_token") != null) {
$options["pageToken"] = Redis::connection()->get("google_photo_token");
}
// $options["pageToken"] = "CkgKQnR5cGUuZ29vZ2xlYXBpcy5jb20vZ29vZ2xlLnBob3Rvcy5saWJyYXJ5LnYxLkxpc3RNZWRpYUl0ZW1zUmVxdWVzdBICCGQSogFBSF91UTQyZWo2TWdQb3cyZDVOOHdubldId0JwakJ0cUJwbHk4akV3Qm8tZ2xvTWpuWlE3M1kzYWJhOFpBeUluSmdaODdnbDhHdXBtYVpuZkRiNnVLY085Qmd4Q3V3bXdhS2ZDTWpKUDhEMndVSG16LXpHdzgtMFpUUzNpY2w4TnZSWklodWRHeXM0RUF1aDE4OGw0djhTTHppUUpQOFV6REEaG3FFZDZPNjc0UlBjbnhNZkoyUUowNG5qQXpxYw";
$pagedResponse = $photosLibraryClient->listMediaItems($options);
$page = $pagedResponse->getPage();
do {
$num = $page->getPageElementCount();
$iterator = $page->getIterator();
for ($i = 0; $i < $num; $i++) {
$photo = $iterator->current();
if (Redis::connection()->sismember("google_photo", $photo->getId())) {
$iterator->next();
continue;
}
Log::info("file not in db " . $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())]
);
Redis::connection()->sadd("google_photo", $photo->getId());
$iterator->next();
}
$nextToken = $page->getNextPageToken();
Redis::connection()->set("google_photo_token", $nextToken);
Log::info("page token " . $nextToken);
} while ($page->hasNextPage() && $page = $page->getNextPage(100));
}
public function getAuthToken($code, array $scopes, $redirectURI)
{
$clientSecretJson = json_decode(
file_get_contents('client_secret_1000190146810-j9mohjt8m4m8j8sj6hupl1bghp6o1pdn.apps.googleusercontent.com.json'),
true
)['web'];
// dump($clientSecretJson);exit;
$clientId = $clientSecretJson['client_id'];
$clientSecret = $clientSecretJson['client_secret'];
$oauth2 = new OAuth2([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
// Where to return the user to if they accept your request to access their account.
// You must authorize this URI in the Google API Console.
'redirectUri' => $redirectURI,
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'scope' => $scopes,
'expiry' => 7200
]);
$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();
$refreshToken = $authToken['access_token'];
// The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when
// they expire.
$credentials = new UserRefreshCredentials(
$scopes,
[
'client_id' => $clientId,
'client_secret' => $clientSecret,
'refresh_token' => $refreshToken
]
);
session(['credentials' => $credentials]);
}
}