Skip to content
This repository has been archived by the owner on Aug 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #68 from danielpuent3/feature/video-update
Browse files Browse the repository at this point in the history
Video Update Functionality
  • Loading branch information
joedawson authored Apr 20, 2018
2 parents 6a9461c + fe0848f commit cb7398c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 23 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
102 changes: 79 additions & 23 deletions src/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
{
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit cb7398c

Please sign in to comment.