diff --git a/README.md b/README.md index 167883a..759306c 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,25 @@ return $youtube->getThumbnailUrl(); **Please note, the maxiumum filesize for the thumbnail is 2MB**. Setting a thumbnail will not work if you attempt to use a thumbnail that exceeds this size. +# Updating a Video + +To update a video, you simply need to pass the **videoId** of the video you wish to update and specify your video information. + +Here's an example: + +```php +$video = Youtube::update($videoId, [ + 'title' => 'My Awesome Video', + 'description' => 'You can also specify your video description here.', + 'tags' => ['foo', 'bar', 'baz'], + 'category_id' => 10 +], $privacy); + +return $video->getVideoId(); +``` + +Note: This request is explicit. Any params left out of the request will be removed. + # Deleting a Video If you would like to delete a video, which of course is uploaded to your authorized channel, you will also have the ability to delete it: diff --git a/src/Youtube.php b/src/Youtube.php index b462170..8abc8ca 100644 --- a/src/Youtube.php +++ b/src/Youtube.php @@ -74,9 +74,10 @@ public function __construct($app, Google_Client $client) * Upload the video to YouTube * * @param string $path - * @param array $data + * @param array $data * @param string $privacyStatus * @return self + * @throws Exception */ public function upload($path, array $data = [], $privacyStatus = 'public') { @@ -87,22 +88,7 @@ public function upload($path, array $data = [], $privacyStatus = 'public') $this->handleAccessToken(); try { - // Setup the Snippet - $snippet = new \Google_Service_YouTube_VideoSnippet(); - - if (array_key_exists('title', $data)) $snippet->setTitle($data['title']); - if (array_key_exists('description', $data)) $snippet->setDescription($data['description']); - if (array_key_exists('tags', $data)) $snippet->setTags($data['tags']); - if (array_key_exists('category_id', $data)) $snippet->setCategoryId($data['category_id']); - - // Set the Privacy Status - $status = new \Google_Service_YouTube_VideoStatus(); - $status->privacyStatus = $privacyStatus; - - // Set the Snippet & Status - $video = new \Google_Service_YouTube_Video(); - $video->setSnippet($snippet); - $video->setStatus($status); + $video = $this->getVideo($data, $privacyStatus); // Set the Chunk Size $chunkSize = 1 * 1024 * 1024; @@ -155,11 +141,47 @@ public function upload($path, array $data = [], $privacyStatus = 'public') } /** - * Set a Custom Thumbnail for the Upload + * Update the video on YouTube * - * @param string $imagePath + * @param string $id + * @param array $data + * @param string $privacyStatus + * @return self + * @throws Exception + */ + public function update($id, array $data = [], $privacyStatus = 'public') + { + $this->handleAccessToken(); + + if (!$this->exists($id)) { + throw new Exception('A video matching id "'. $id .'" could not be found.'); + } + + try { + $video = $this->getVideo($data, $privacyStatus, $id); + + $status = $this->youtube->videos->update('status,snippet', $video); + + // Set ID of the Updated Video + $this->videoId = $status['id']; + + // Set the Snippet from Updated Video + $this->snippet = $status['snippet']; + } catch (\Google_Service_Exception $e) { + throw new Exception($e->getMessage()); + } catch (\Google_Exception $e) { + throw new Exception($e->getMessage()); + } + + return $this; + } + + /** + * Set a Custom Thumbnail for the Upload * + * @param string $imagePath * @return self + * @throws Exception */ public function withThumbnail($imagePath) { @@ -207,9 +229,9 @@ public function withThumbnail($imagePath) /** * Delete a YouTube video by it's ID. * - * @param int $id - * + * @param int $id * @return bool + * @throws Exception */ public function delete($id) { @@ -222,6 +244,39 @@ public function delete($id) return $this->youtube->videos->delete($id); } + /** + * @param $data + * @param $privacyStatus + * @param null $id + * @return \Google_Service_YouTube_Video + */ + private function getVideo($data, $privacyStatus, $id = null) + { + // Setup the Snippet + $snippet = new \Google_Service_YouTube_VideoSnippet(); + + if (array_key_exists('title', $data)) $snippet->setTitle($data['title']); + if (array_key_exists('description', $data)) $snippet->setDescription($data['description']); + if (array_key_exists('tags', $data)) $snippet->setTags($data['tags']); + if (array_key_exists('category_id', $data)) $snippet->setCategoryId($data['category_id']); + + // Set the Privacy Status + $status = new \Google_Service_YouTube_VideoStatus(); + $status->privacyStatus = $privacyStatus; + + // Set the Snippet & Status + $video = new \Google_Service_YouTube_Video(); + if ($id) + { + $video->setId($id); + } + + $video->setSnippet($snippet); + $video->setStatus($status); + + return $video; + } + /** * Check if a YouTube video exists by it's ID. * @@ -273,8 +328,9 @@ public function getThumbnailUrl() /** * Setup the Google Client * - * @param \Google_Client $client - * @return \Google_Client $client + * @param Google_Client $client + * @return Google_Client $client + * @throws Exception */ private function setup(Google_Client $client) {