Example #1
0
 /**
  * Upload Video.
  *
  * @param \Userdesk\Submission\Classes\SubmissionVideoItem $item;
  *
  * @return \Userdesk\Submission\Classes\SubmissionResult;
  */
 public function uploadVideo(SubmissionVideoItem $item)
 {
     $google = $this->providerFromToken($this->token);
     try {
         $youtube = new \Google_Service_YouTube($google);
         $videoPath = $item->getVideo();
         if (!empty($videoPath)) {
             // Create a snipet with title, description, tags and category id
             $snippet = new \Google_Service_YouTube_VideoSnippet();
             $snippet->setTitle($item->getTitle());
             $snippet->setDescription($item->getDescription());
             $snippet->setTags(explode(',', $item->getKeywords()));
             // Numeric video category. See
             // https://developers.google.com/youtube/v3/docs/videoCategories/list
             $snippet->setCategoryId("22");
             $status = new \Google_Service_YouTube_VideoStatus();
             $status->privacyStatus = "public";
             // Create a YouTube video with snippet and status
             $video = new \Google_Service_YouTube_Video();
             $video->setSnippet($snippet);
             $video->setStatus($status);
             // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
             // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
             $chunkSizeBytes = 1 * 1024 * 1024;
             $google->setDefer(true);
             $insertRequest = $youtube->videos->insert("status,snippet", $video);
             // Create a MediaFileUpload with resumable uploads
             $media = new \Google_Http_MediaFileUpload($google, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
             $media->setFileSize(filesize($videoPath));
             // Create a video insert request
             $uploadStatus = false;
             // Read file and upload chunk by chunk
             $handle = fopen($videoPath, "rb");
             while (!$uploadStatus && !feof($handle)) {
                 $chunk = fread($handle, $chunkSizeBytes);
                 $uploadStatus = $media->nextChunk($chunk);
             }
             fclose($handle);
             $google->setDefer(false);
             if (!empty($uploadStatus) && !empty($uploadStatus->id)) {
                 $url = "http://www.youtube.com/watch?v=" . $uploadStatus->id;
                 return new SubmissionResult(true, '', $url);
             }
             return new SubmissionResult(false, 'Video Upload Failed');
         }
     } catch (\Google_ServiceException $e) {
         throw new InvalidUploadException($e->getMessage());
     } catch (\Google_Exception $e) {
         throw new InvalidUploadException($e->getMessage());
     } catch (Exception $e) {
         throw new InvalidUploadException($e->getMessage());
     }
 }
 /**
  * Upload Video.
  *
  * @param \Userdesk\Submission\Classes\SubmissionVideoItem $item;
  *
  * @return \Userdesk\Submission\Classes\SubmissionResult;
  */
 public function uploadVideo(SubmissionVideoItem $item)
 {
     $facebook = $this->providerFromToken();
     $params = array('content_category' => 'OTHER', 'source' => $facebook->videoToUpload($item->getVideo()), 'title' => $item->getTitle(), 'thumb' => $facebook->fileToUpload($item->getThumb()));
     try {
         $userId = $facebook->getUser();
         $response = $facebook->post('/' . $userId . '/videos', $params);
     } catch (FacebookResponseException $e) {
         throw new InvalidUploadException($e->getMessage());
     } catch (FacebookSDKException $e) {
         throw new InvalidUploadException($e->getMessage());
     } catch (Exception $e) {
         throw new InvalidUploadException($e->getMessage());
     }
     $graphNode = $response->getGraphNode();
     if (!empty($graphNode['id'])) {
         $url = "https://www.facebook.com/photo.php?v=" . $graphNode['id'];
         return new SubmissionResult(true, '', $url);
     }
     return new SubmissionResult(false, 'Video Upload Failed');
 }
Example #3
0
 /**
  * Upload Video.
  *
  * @param \Userdesk\Submission\Classes\SubmissionVideoItem $item;
  *
  * @return \Userdesk\Submission\Classes\SubmissionResult;
  */
 public function uploadVideo(SubmissionVideoItem $item)
 {
     $tw = $this->providerFromToken();
     $video = file_get_contents($item->getVideo());
     $media_data = $tw->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array('command' => 'INIT', 'total_bytes' => strlen($video), 'media_type' => $item->getType()));
     $media = json_decode($media_data, true);
     $media_id = $media['media_id'];
     if ($media_id) {
         $tw->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array('command' => 'APPEND', 'segment_index' => 0, 'media_id' => $media_id, 'media_data' => base64_encode($video)));
         $tw->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array('command' => 'FINALIZE', 'media_id' => $media_id));
         $response = $tw->request('statuses/update.json', 'POST', array('status' => $item->getTitle(), 'media_ids' => $media_id));
         $status = json_decode($response, true);
         if (!empty($status['id']) && !empty($status['user'])) {
             $url = sprintf('https://twitter.com/%s/status/%s', $status['user']['screen_name'], $status['id']);
             return new SubmissionResult(true, '', $url);
         }
         return new SubmissionResult(false, 'Video Upload Failed');
     } else {
         throw new InvalidUploadException($e->getMessage());
     }
 }