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

Commit cb7398c

Browse files
authored
Merge pull request #68 from danielpuent3/feature/video-update
Video Update Functionality
2 parents 6a9461c + fe0848f commit cb7398c

File tree

2 files changed

+98
-23
lines changed

2 files changed

+98
-23
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ return $youtube->getThumbnailUrl();
110110

111111
**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.
112112

113+
# Updating a Video
114+
115+
To update a video, you simply need to pass the **videoId** of the video you wish to update and specify your video information.
116+
117+
Here's an example:
118+
119+
```php
120+
$video = Youtube::update($videoId, [
121+
'title' => 'My Awesome Video',
122+
'description' => 'You can also specify your video description here.',
123+
'tags' => ['foo', 'bar', 'baz'],
124+
'category_id' => 10
125+
], $privacy);
126+
127+
return $video->getVideoId();
128+
```
129+
130+
Note: This request is explicit. Any params left out of the request will be removed.
131+
113132
# Deleting a Video
114133

115134
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:

src/Youtube.php

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ public function __construct($app, Google_Client $client)
7474
* Upload the video to YouTube
7575
*
7676
* @param string $path
77-
* @param array $data
77+
* @param array $data
7878
* @param string $privacyStatus
7979
* @return self
80+
* @throws Exception
8081
*/
8182
public function upload($path, array $data = [], $privacyStatus = 'public')
8283
{
@@ -87,22 +88,7 @@ public function upload($path, array $data = [], $privacyStatus = 'public')
8788
$this->handleAccessToken();
8889

8990
try {
90-
// Setup the Snippet
91-
$snippet = new \Google_Service_YouTube_VideoSnippet();
92-
93-
if (array_key_exists('title', $data)) $snippet->setTitle($data['title']);
94-
if (array_key_exists('description', $data)) $snippet->setDescription($data['description']);
95-
if (array_key_exists('tags', $data)) $snippet->setTags($data['tags']);
96-
if (array_key_exists('category_id', $data)) $snippet->setCategoryId($data['category_id']);
97-
98-
// Set the Privacy Status
99-
$status = new \Google_Service_YouTube_VideoStatus();
100-
$status->privacyStatus = $privacyStatus;
101-
102-
// Set the Snippet & Status
103-
$video = new \Google_Service_YouTube_Video();
104-
$video->setSnippet($snippet);
105-
$video->setStatus($status);
91+
$video = $this->getVideo($data, $privacyStatus);
10692

10793
// Set the Chunk Size
10894
$chunkSize = 1 * 1024 * 1024;
@@ -155,11 +141,47 @@ public function upload($path, array $data = [], $privacyStatus = 'public')
155141
}
156142

157143
/**
158-
* Set a Custom Thumbnail for the Upload
144+
* Update the video on YouTube
159145
*
160-
* @param string $imagePath
146+
* @param string $id
147+
* @param array $data
148+
* @param string $privacyStatus
149+
* @return self
150+
* @throws Exception
151+
*/
152+
public function update($id, array $data = [], $privacyStatus = 'public')
153+
{
154+
$this->handleAccessToken();
155+
156+
if (!$this->exists($id)) {
157+
throw new Exception('A video matching id "'. $id .'" could not be found.');
158+
}
159+
160+
try {
161+
$video = $this->getVideo($data, $privacyStatus, $id);
162+
163+
$status = $this->youtube->videos->update('status,snippet', $video);
164+
165+
// Set ID of the Updated Video
166+
$this->videoId = $status['id'];
167+
168+
// Set the Snippet from Updated Video
169+
$this->snippet = $status['snippet'];
170+
} catch (\Google_Service_Exception $e) {
171+
throw new Exception($e->getMessage());
172+
} catch (\Google_Exception $e) {
173+
throw new Exception($e->getMessage());
174+
}
175+
176+
return $this;
177+
}
178+
179+
/**
180+
* Set a Custom Thumbnail for the Upload
161181
*
182+
* @param string $imagePath
162183
* @return self
184+
* @throws Exception
163185
*/
164186
public function withThumbnail($imagePath)
165187
{
@@ -207,9 +229,9 @@ public function withThumbnail($imagePath)
207229
/**
208230
* Delete a YouTube video by it's ID.
209231
*
210-
* @param int $id
211-
*
232+
* @param int $id
212233
* @return bool
234+
* @throws Exception
213235
*/
214236
public function delete($id)
215237
{
@@ -222,6 +244,39 @@ public function delete($id)
222244
return $this->youtube->videos->delete($id);
223245
}
224246

247+
/**
248+
* @param $data
249+
* @param $privacyStatus
250+
* @param null $id
251+
* @return \Google_Service_YouTube_Video
252+
*/
253+
private function getVideo($data, $privacyStatus, $id = null)
254+
{
255+
// Setup the Snippet
256+
$snippet = new \Google_Service_YouTube_VideoSnippet();
257+
258+
if (array_key_exists('title', $data)) $snippet->setTitle($data['title']);
259+
if (array_key_exists('description', $data)) $snippet->setDescription($data['description']);
260+
if (array_key_exists('tags', $data)) $snippet->setTags($data['tags']);
261+
if (array_key_exists('category_id', $data)) $snippet->setCategoryId($data['category_id']);
262+
263+
// Set the Privacy Status
264+
$status = new \Google_Service_YouTube_VideoStatus();
265+
$status->privacyStatus = $privacyStatus;
266+
267+
// Set the Snippet & Status
268+
$video = new \Google_Service_YouTube_Video();
269+
if ($id)
270+
{
271+
$video->setId($id);
272+
}
273+
274+
$video->setSnippet($snippet);
275+
$video->setStatus($status);
276+
277+
return $video;
278+
}
279+
225280
/**
226281
* Check if a YouTube video exists by it's ID.
227282
*
@@ -273,8 +328,9 @@ public function getThumbnailUrl()
273328
/**
274329
* Setup the Google Client
275330
*
276-
* @param \Google_Client $client
277-
* @return \Google_Client $client
331+
* @param Google_Client $client
332+
* @return Google_Client $client
333+
* @throws Exception
278334
*/
279335
private function setup(Google_Client $client)
280336
{

0 commit comments

Comments
 (0)