使用谷歌镜像Api客户端检查与时间线项目关联的附件的状态。根据视频的长度,视频可能需要几分钟才能播放。奇怪的是,如果你按照302的重定向,你最终会进入Picasa域,考虑到这是视频,这有点奇怪,但这确实表明谷歌在移交视频之前正在对你的视频进行一些预处理。
这段代码是我编写的排队过程的一部分,用于处理向谷歌查询时间线项目的处理状态。来自谷歌的回调被插入到我的队列服务器中,并由工作人员拾取,该工作人员每X秒主动检查一次状态,以确定媒体是否准备就绪,如果超过循环数,则只需重新安排作业由另一个工作人员拾取。主动检查而不是在回调中返回非200代码是非常有益的,因为谷歌会对他们的api调用进行指数级回退,所以对于需要一些时间才能进入正确状态的视频,你可能会在谷歌的回调之间等待很长时间。
公共函数运行($data){
$timeline_item_id=$data['timeline_iitem_id'];
try{
$glassUtils = new Google_Glass_Utilities();
$mirrorClient = new Google_Glass_MirrorClient();
$client = $mirrorClient->get_google_api_client();
$client->setAccessToken($data['credentials']);
$mirror_service = new Google_MirrorService($client);
echo "\nTIMELINE ITEM ID: " . $data['timeline_item_id'] . "\n";
$timeline_item = $mirror_service->timeline->get($timeline_item_id);
$caption = $timeline_item->getText();
}catch (Exception $e){
echo "ERROR";
echo $e->getMessage();
return true;
}
//Blocker that waits for the item to be in the active state
//Should be faster than waiting for extra callbacks from Google
try{
$checkCount = 1;
while(true){
$blnProceed = true;
echo "Check " . $checkCount . " of " . $this->maxSearchChecks . " for attachments on timeline item " . $timeline_item_id . "\n";
$attachments = $mirror_service->timeline_attachments->listTimelineAttachments($timeline_item_id);
foreach ($attachments->getItems() as $attachment) {
echo "Attachment content type: " . $attachment->getContentType() . "\n";
//echo "Attachment content URL: " . $attachment->getContentUrl();
if($attachment->getIsProcessingContent() && $attachment->getContentType() == 'video/mp4'){
$blnProceed = false;
}
}
if(!$blnProceed){
echo "Attachments were not ready\n";
if($checkCount == $this->maxSearchChecks){
echo "Job hit max checks, requeueing\n";
$task = new BackgroundTask();
$task->addTask('GlassVideoImport', $saveData);
$task->submit();
GearmanPearManager::$LOG[] = "Success";
return true;
}else{
$checkCount += 1;
echo "Sleeping for " . $this->sleepSecs . " seconds before recheck\n";
sleep($this->sleepSecs);
}
}else{
echo "GOT THE TIMELINE ITEM AND ATTACHMENT IS READY\n\n\n\n";
//Continue
break;
}
}
}catch (Exception $e){
echo "ERROR";
echo $e->getMessage();
return true;
}
//Got here so now go get the attachment
try{
$attachment = $mirror_service->timeline_attachments->get($data['timeline_item_id'], $data['timeline_attachment_id']);
$attachmentBinary = $this->downloadAttachment($data['timeline_item_id'], $attachment);
//Do something with the binary of the attachment
GearmanPearManager::$LOG[] = "Success";
return true;
}
catch( Exception $e ) {
echo $e->getMessage() . "\n";
@unlink($fileRes['localFile']);
GearmanPearManager::$LOG[] = "Failure";
return true;
}
}
private function downloadAttachment($itemId, $attachment) {
$request = new Google_HttpRequest(
$attachment->getContentUrl(), 'GET', null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
return null;
}
}