[ZF-6295] Generic:
[zend.git] / library / Zend / Gdata / YouTube.php
blob6abdafd025dc0f7ddbbe7b01535f4e777765330f
1 <?php
3 /**
4 * Zend Framework
6 * LICENSE
8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
16 * @category Zend
17 * @package Zend_Gdata
18 * @subpackage YouTube
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /**
24 * @see Zend_Gdata_Media
26 require_once 'Zend/Gdata/Media.php';
28 /**
29 * @see Zend_Gdata_YouTube_VideoEntry
31 require_once 'Zend/Gdata/YouTube/VideoEntry.php';
33 /**
34 * @see Zend_Gdata_YouTube_VideoFeed
36 require_once 'Zend/Gdata/YouTube/VideoFeed.php';
38 /**
39 * @see Zend_Gdata_YouTube_CommentFeed
41 require_once 'Zend/Gdata/YouTube/CommentFeed.php';
43 /**
44 * @see Zend_Gdata_YouTube_PlaylistListFeed
46 require_once 'Zend/Gdata/YouTube/PlaylistListFeed.php';
48 /**
49 * @see Zend_Gdata_YouTube_SubscriptionFeed
51 require_once 'Zend/Gdata/YouTube/SubscriptionFeed.php';
53 /**
54 * @see Zend_Gdata_YouTube_ContactFeed
56 require_once 'Zend/Gdata/YouTube/ContactFeed.php';
58 /**
59 * @see Zend_Gdata_YouTube_PlaylistVideoFeed
61 require_once 'Zend/Gdata/YouTube/PlaylistVideoFeed.php';
63 /**
64 * @see Zend_Gdata_YouTube_ActivityFeed
66 require_once 'Zend/Gdata/YouTube/ActivityFeed.php';
68 /**
69 * @see Zend_Gdata_YouTube_InboxFeed
71 require_once 'Zend/Gdata/YouTube/InboxFeed.php';
74 /**
75 * Service class for interacting with the YouTube Data API.
76 * @link http://code.google.com/apis/youtube/
78 * @category Zend
79 * @package Zend_Gdata
80 * @subpackage YouTube
81 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
82 * @license http://framework.zend.com/license/new-bsd New BSD License
84 class Zend_Gdata_YouTube extends Zend_Gdata_Media
87 const AUTH_SERVICE_NAME = 'youtube';
88 const CLIENTLOGIN_URL = 'https://www.google.com/youtube/accounts/ClientLogin';
90 const STANDARD_TOP_RATED_URI = 'http://gdata.youtube.com/feeds/standardfeeds/top_rated';
91 const STANDARD_MOST_VIEWED_URI = 'http://gdata.youtube.com/feeds/standardfeeds/most_viewed';
92 const STANDARD_RECENTLY_FEATURED_URI = 'http://gdata.youtube.com/feeds/standardfeeds/recently_featured';
93 const STANDARD_WATCH_ON_MOBILE_URI = 'http://gdata.youtube.com/feeds/standardfeeds/watch_on_mobile';
95 const STANDARD_TOP_RATED_URI_V2 =
96 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';
97 const STANDARD_MOST_VIEWED_URI_V2 =
98 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
99 const STANDARD_RECENTLY_FEATURED_URI_V2 =
100 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';
101 const STANDARD_WATCH_ON_MOBILE_URI_V2 =
102 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile';
104 const USER_URI = 'http://gdata.youtube.com/feeds/api/users';
105 const VIDEO_URI = 'http://gdata.youtube.com/feeds/api/videos';
106 const PLAYLIST_REL = 'http://gdata.youtube.com/schemas/2007#playlist';
107 const USER_UPLOADS_REL = 'http://gdata.youtube.com/schemas/2007#user.uploads';
108 const USER_PLAYLISTS_REL = 'http://gdata.youtube.com/schemas/2007#user.playlists';
109 const USER_SUBSCRIPTIONS_REL = 'http://gdata.youtube.com/schemas/2007#user.subscriptions';
110 const USER_CONTACTS_REL = 'http://gdata.youtube.com/schemas/2007#user.contacts';
111 const USER_FAVORITES_REL = 'http://gdata.youtube.com/schemas/2007#user.favorites';
112 const VIDEO_RESPONSES_REL = 'http://gdata.youtube.com/schemas/2007#video.responses';
113 const VIDEO_RATINGS_REL = 'http://gdata.youtube.com/schemas/2007#video.ratings';
114 const VIDEO_COMPLAINTS_REL = 'http://gdata.youtube.com/schemas/2007#video.complaints';
115 const ACTIVITY_FEED_URI = 'http://gdata.youtube.com/feeds/api/events';
116 const FRIEND_ACTIVITY_FEED_URI =
117 'http://gdata.youtube.com/feeds/api/users/default/friendsactivity';
120 * The URI of the in-reply-to schema for comments in reply to
121 * other comments.
123 * @var string
125 const IN_REPLY_TO_SCHEME =
126 'http://gdata.youtube.com/schemas/2007#in-reply-to';
129 * The URI of the inbox feed for the currently authenticated user.
131 * @var string
133 const INBOX_FEED_URI =
134 'http://gdata.youtube.com/feeds/api/users/default/inbox';
137 * The maximum number of users for which activity can be requested for,
138 * as enforced by the API.
140 * @var integer
142 const ACTIVITY_FEED_MAX_USERS = 20;
145 * The suffix for a feed of favorites.
147 * @var string
149 const FAVORITES_URI_SUFFIX = 'favorites';
152 * The suffix for the user's upload feed.
154 * @var string
156 const UPLOADS_URI_SUFFIX = 'uploads';
159 * The suffix for a feed of video responses.
161 * @var string
163 const RESPONSES_URI_SUFFIX = 'responses';
166 * The suffix for a feed of related videos.
168 * @var string
170 const RELATED_URI_SUFFIX = 'related';
173 * The suffix for a feed of messages (inbox entries).
175 * @var string
177 const INBOX_URI_SUFFIX = 'inbox';
180 * Namespaces used for Zend_Gdata_YouTube
182 * @var array
184 public static $namespaces = array(
185 array('yt', 'http://gdata.youtube.com/schemas/2007', 1, 0),
186 array('georss', 'http://www.georss.org/georss', 1, 0),
187 array('gml', 'http://www.opengis.net/gml', 1, 0),
188 array('media', 'http://search.yahoo.com/mrss/', 1, 0)
192 * Create Zend_Gdata_YouTube object
194 * @param Zend_Http_Client $client (optional) The HTTP client to use when
195 * when communicating with the Google servers.
196 * @param string $applicationId The identity of the app in the form of
197 * Company-AppName-Version
198 * @param string $clientId The clientId issued by the YouTube dashboard
199 * @param string $developerKey The developerKey issued by the YouTube dashboard
201 public function __construct($client = null,
202 $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
203 $developerKey = null)
205 $this->registerPackage('Zend_Gdata_YouTube');
206 $this->registerPackage('Zend_Gdata_YouTube_Extension');
207 $this->registerPackage('Zend_Gdata_Media');
208 $this->registerPackage('Zend_Gdata_Media_Extension');
210 // NOTE This constructor no longer calls the parent constructor
211 $this->setHttpClient($client, $applicationId, $clientId, $developerKey);
215 * Set the Zend_Http_Client object used for communication
217 * @param Zend_Http_Client $client The client to use for communication
218 * @throws Zend_Gdata_App_HttpException
219 * @return Zend_Gdata_App Provides a fluent interface
221 public function setHttpClient($client,
222 $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
223 $developerKey = null)
225 if ($client === null) {
226 $client = new Zend_Http_Client();
228 if (!$client instanceof Zend_Http_Client) {
229 require_once 'Zend/Gdata/App/HttpException.php';
230 throw new Zend_Gdata_App_HttpException(
231 'Argument is not an instance of Zend_Http_Client.');
234 if ($clientId != null) {
235 $client->setHeaders('X-GData-Client', $clientId);
238 if ($developerKey != null) {
239 $client->setHeaders('X-GData-Key', 'key='. $developerKey);
242 return parent::setHttpClient($client, $applicationId);
246 * Retrieves a feed of videos.
248 * @param mixed $location (optional) The URL to query or a
249 * Zend_Gdata_Query object from which a URL can be determined
250 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
251 * specified URL.
253 public function getVideoFeed($location = null)
255 if ($location == null) {
256 $uri = self::VIDEO_URI;
257 } else if ($location instanceof Zend_Gdata_Query) {
258 $uri = $location->getQueryUrl();
259 } else {
260 $uri = $location;
262 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
266 * Retrieves a specific video entry.
268 * @param mixed $videoId The ID of the video to retrieve.
269 * @param mixed $location (optional) The URL to query or a
270 * Zend_Gdata_Query object from which a URL can be determined.
271 * @param boolean $fullEntry (optional) Retrieve the full metadata for the
272 * entry. Only possible if entry belongs to currently authenticated
273 * user. An exception will be thrown otherwise.
274 * @throws Zend_Gdata_App_HttpException
275 * @return Zend_Gdata_YouTube_VideoEntry The video entry found at the
276 * specified URL.
278 public function getVideoEntry($videoId = null, $location = null,
279 $fullEntry = false)
281 if ($videoId !== null) {
282 if ($fullEntry) {
283 return $this->getFullVideoEntry($videoId);
284 } else {
285 $uri = self::VIDEO_URI . "/" . $videoId;
287 } else if ($location instanceof Zend_Gdata_Query) {
288 $uri = $location->getQueryUrl();
289 } else {
290 $uri = $location;
292 return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
296 * Retrieves a video entry from the user's upload feed.
298 * @param mixed $videoID The ID of the video to retrieve.
299 * @throws Zend_Gdata_App_HttpException
300 * @return Zend_Gdata_YouTube_VideoEntry|null The video entry to be
301 * retrieved, or null if it was not found or the user requesting it
302 * did not have the appropriate permissions.
304 public function getFullVideoEntry($videoId)
306 $uri = self::USER_URI . "/default/" .
307 self::UPLOADS_URI_SUFFIX . "/$videoId";
308 return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
312 * Retrieves a feed of videos related to the specified video ID.
314 * @param string $videoId The videoId of interest
315 * @param mixed $location (optional) The URL to query or a
316 * Zend_Gdata_Query object from which a URL can be determined
317 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
318 * specified URL.
320 public function getRelatedVideoFeed($videoId = null, $location = null)
322 if ($videoId !== null) {
323 $uri = self::VIDEO_URI . "/" . $videoId . "/" .
324 self::RELATED_URI_SUFFIX;
325 } else if ($location instanceof Zend_Gdata_Query) {
326 $uri = $location->getQueryUrl();
327 } else {
328 $uri = $location;
330 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
334 * Retrieves a feed of video responses related to the specified video ID.
336 * @param string $videoId The videoId of interest
337 * @param mixed $location (optional) The URL to query or a
338 * Zend_Gdata_Query object from which a URL can be determined
339 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
340 * specified URL.
342 public function getVideoResponseFeed($videoId = null, $location = null)
344 if ($videoId !== null) {
345 $uri = self::VIDEO_URI . "/" . $videoId . "/" .
346 self::RESPONSES_URI_SUFFIX;
347 } else if ($location instanceof Zend_Gdata_Query) {
348 $uri = $location->getQueryUrl();
349 } else {
350 $uri = $location;
352 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
356 * Retrieves a feed of comments related to the specified video ID.
358 * @param string $videoId The videoId of interest
359 * @param mixed $location (optional) The URL to query or a
360 * Zend_Gdata_Query object from which a URL can be determined
361 * @return Zend_Gdata_YouTube_CommentFeed The feed of videos found at the
362 * specified URL.
364 public function getVideoCommentFeed($videoId = null, $location = null)
366 if ($videoId !== null) {
367 $uri = self::VIDEO_URI . "/" . $videoId . "/comments";
368 } else if ($location instanceof Zend_Gdata_Query) {
369 $uri = $location->getQueryUrl();
370 } else {
371 $uri = $location;
373 return parent::getFeed($uri, 'Zend_Gdata_YouTube_CommentFeed');
377 * Retrieves a feed of comments related to the specified video ID.
379 * @param mixed $location (optional) The URL to query or a
380 * Zend_Gdata_Query object from which a URL can be determined
381 * @return Zend_Gdata_YouTube_CommentFeed The feed of videos found at the
382 * specified URL.
384 public function getTopRatedVideoFeed($location = null)
386 $standardFeedUri = self::STANDARD_TOP_RATED_URI;
388 if ($this->getMajorProtocolVersion() == 2) {
389 $standardFeedUri = self::STANDARD_TOP_RATED_URI_V2;
392 if ($location == null) {
393 $uri = $standardFeedUri;
394 } else if ($location instanceof Zend_Gdata_Query) {
395 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
396 if (!isset($location->url)) {
397 $location->setFeedType('top rated');
400 $uri = $location->getQueryUrl();
401 } else {
402 $uri = $location;
404 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
409 * Retrieves a feed of the most viewed videos.
411 * @param mixed $location (optional) The URL to query or a
412 * Zend_Gdata_Query object from which a URL can be determined
413 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
414 * specified URL.
416 public function getMostViewedVideoFeed($location = null)
418 $standardFeedUri = self::STANDARD_MOST_VIEWED_URI;
420 if ($this->getMajorProtocolVersion() == 2) {
421 $standardFeedUri = self::STANDARD_MOST_VIEWED_URI_V2;
424 if ($location == null) {
425 $uri = $standardFeedUri;
426 } else if ($location instanceof Zend_Gdata_Query) {
427 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
428 if (!isset($location->url)) {
429 $location->setFeedType('most viewed');
432 $uri = $location->getQueryUrl();
433 } else {
434 $uri = $location;
436 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
440 * Retrieves a feed of recently featured videos.
442 * @param mixed $location (optional) The URL to query or a
443 * Zend_Gdata_Query object from which a URL can be determined
444 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
445 * specified URL.
447 public function getRecentlyFeaturedVideoFeed($location = null)
449 $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI;
451 if ($this->getMajorProtocolVersion() == 2) {
452 $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI_V2;
455 if ($location == null) {
456 $uri = $standardFeedUri;
457 } else if ($location instanceof Zend_Gdata_Query) {
458 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
459 if (!isset($location->url)) {
460 $location->setFeedType('recently featured');
463 $uri = $location->getQueryUrl();
464 } else {
465 $uri = $location;
467 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
471 * Retrieves a feed of videos recently featured for mobile devices.
472 * These videos will have RTSP links in the $entry->mediaGroup->content
474 * @param mixed $location (optional) The URL to query or a
475 * Zend_Gdata_Query object from which a URL can be determined
476 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
477 * specified URL.
479 public function getWatchOnMobileVideoFeed($location = null)
481 $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI;
483 if ($this->getMajorProtocolVersion() == 2) {
484 $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI_V2;
487 if ($location == null) {
488 $uri = $standardFeedUri;
489 } else if ($location instanceof Zend_Gdata_Query) {
490 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
491 if (!isset($location->url)) {
492 $location->setFeedType('watch on mobile');
495 $uri = $location->getQueryUrl();
496 } else {
497 $uri = $location;
499 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
503 * Retrieves a feed which lists a user's playlist
505 * @param string $user (optional) The username of interest
506 * @param mixed $location (optional) The URL to query or a
507 * Zend_Gdata_Query object from which a URL can be determined
508 * @return Zend_Gdata_YouTube_PlaylistListFeed The feed of playlists
510 public function getPlaylistListFeed($user = null, $location = null)
512 if ($user !== null) {
513 $uri = self::USER_URI . '/' . $user . '/playlists';
514 } else if ($location instanceof Zend_Gdata_Query) {
515 $uri = $location->getQueryUrl();
516 } else {
517 $uri = $location;
519 return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistListFeed');
523 * Retrieves a feed of videos in a particular playlist
525 * @param mixed $location (optional) The URL to query or a
526 * Zend_Gdata_Query object from which a URL can be determined
527 * @return Zend_Gdata_YouTube_PlaylistVideoFeed The feed of videos found at
528 * the specified URL.
530 public function getPlaylistVideoFeed($location)
532 if ($location instanceof Zend_Gdata_Query) {
533 $uri = $location->getQueryUrl();
534 } else {
535 $uri = $location;
537 return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistVideoFeed');
541 * Retrieves a feed of a user's subscriptions
543 * @param string $user (optional) The username of interest
544 * @param mixed $location (optional) The URL to query or a
545 * Zend_Gdata_Query object from which a URL can be determined
546 * @return Zend_Gdata_YouTube_SubscriptionListFeed The feed of subscriptions
548 public function getSubscriptionFeed($user = null, $location = null)
550 if ($user !== null) {
551 $uri = self::USER_URI . '/' . $user . '/subscriptions';
552 } else if ($location instanceof Zend_Gdata_Query) {
553 $uri = $location->getQueryUrl();
554 } else {
555 $uri = $location;
557 return parent::getFeed($uri, 'Zend_Gdata_YouTube_SubscriptionFeed');
561 * Retrieves a feed of a user's contacts
563 * @param string $user (optional) The username of interest
564 * @param mixed $location (optional) The URL to query or a
565 * Zend_Gdata_Query object from which a URL can be determined
566 * @return Zend_Gdata_YouTube_ContactFeed The feed of contacts
568 public function getContactFeed($user = null, $location = null)
570 if ($user !== null) {
571 $uri = self::USER_URI . '/' . $user . '/contacts';
572 } else if ($location instanceof Zend_Gdata_Query) {
573 $uri = $location->getQueryUrl();
574 } else {
575 $uri = $location;
577 return parent::getFeed($uri, 'Zend_Gdata_YouTube_ContactFeed');
581 * Retrieves a user's uploads
583 * @param string $user (optional) The username of interest
584 * @param mixed $location (optional) The URL to query or a
585 * Zend_Gdata_Query object from which a URL can be determined
586 * @return Zend_Gdata_YouTube_VideoFeed The videos uploaded by the user
588 public function getUserUploads($user = null, $location = null)
590 if ($user !== null) {
591 $uri = self::USER_URI . '/' . $user . '/' .
592 self::UPLOADS_URI_SUFFIX;
593 } else if ($location instanceof Zend_Gdata_Query) {
594 $uri = $location->getQueryUrl();
595 } else {
596 $uri = $location;
598 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
602 * Retrieves a user's favorites
604 * @param string $user (optional) The username of interest
605 * @param mixed $location (optional) The URL to query or a
606 * Zend_Gdata_Query object from which a URL can be determined
607 * @return Zend_Gdata_YouTube_VideoFeed The videos favorited by the user
609 public function getUserFavorites($user = null, $location = null)
611 if ($user !== null) {
612 $uri = self::USER_URI . '/' . $user . '/' .
613 self::FAVORITES_URI_SUFFIX;
614 } else if ($location instanceof Zend_Gdata_Query) {
615 $uri = $location->getQueryUrl();
616 } else {
617 $uri = $location;
619 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
623 * Retrieves a user's profile as an entry
625 * @param string $user (optional) The username of interest
626 * @param mixed $location (optional) The URL to query or a
627 * Zend_Gdata_Query object from which a URL can be determined
628 * @return Zend_Gdata_YouTube_UserProfileEntry The user profile entry
630 public function getUserProfile($user = null, $location = null)
632 if ($user !== null) {
633 $uri = self::USER_URI . '/' . $user;
634 } else if ($location instanceof Zend_Gdata_Query) {
635 $uri = $location->getQueryUrl();
636 } else {
637 $uri = $location;
639 return parent::getEntry($uri, 'Zend_Gdata_YouTube_UserProfileEntry');
643 * Helper function for parsing a YouTube token response
645 * @param string $response The service response
646 * @throws Zend_Gdata_App_Exception
647 * @return array An array containing the token and URL
649 public static function parseFormUploadTokenResponse($response)
651 // Load the feed as an XML DOMDocument object
652 @ini_set('track_errors', 1);
653 $doc = new DOMDocument();
654 $success = @$doc->loadXML($response);
655 @ini_restore('track_errors');
657 if (!$success) {
658 require_once 'Zend/Gdata/App/Exception.php';
659 throw new Zend_Gdata_App_Exception(
660 "Zend_Gdata_YouTube::parseFormUploadTokenResponse - " .
661 "DOMDocument cannot parse XML: $php_errormsg");
663 $responseElement = $doc->getElementsByTagName('response')->item(0);
665 $urlText = null;
666 $tokenText = null;
667 if ($responseElement != null) {
668 $urlElement =
669 $responseElement->getElementsByTagName('url')->item(0);
670 $tokenElement =
671 $responseElement->getElementsByTagName('token')->item(0);
673 if ($urlElement && $urlElement->hasChildNodes() &&
674 $tokenElement && $tokenElement->hasChildNodes()) {
676 $urlText = $urlElement->firstChild->nodeValue;
677 $tokenText = $tokenElement->firstChild->nodeValue;
681 if ($tokenText != null && $urlText != null) {
682 return array('token' => $tokenText, 'url' => $urlText);
683 } else {
684 require_once 'Zend/Gdata/App/Exception.php';
685 throw new Zend_Gdata_App_Exception(
686 'Form upload token not found in response');
691 * Retrieves a YouTube token
693 * @param Zend_Gdata_YouTube_VideoEntry $videoEntry The video entry
694 * @param string $url The location as a string URL
695 * @throws Zend_Gdata_App_Exception
696 * @return array An array containing a token and URL
698 public function getFormUploadToken($videoEntry,
699 $url='http://gdata.youtube.com/action/GetUploadToken')
701 if ($url != null && is_string($url)) {
702 // $response is a Zend_Http_response object
703 $response = $this->post($videoEntry, $url);
704 return self::parseFormUploadTokenResponse($response->getBody());
705 } else {
706 require_once 'Zend/Gdata/App/Exception.php';
707 throw new Zend_Gdata_App_Exception(
708 'Url must be provided as a string URL');
713 * Retrieves the activity feed for users
715 * @param mixed $usernames A string identifying the usernames for which to
716 * retrieve activity for. This can also be a Zend_Gdata_Query
717 * object from which a URL can be determined.
718 * @throws Zend_Gdata_App_VersionException if using version less than 2.
719 * @return Zend_Gdata_YouTube_ActivityFeed
721 public function getActivityForUser($username)
723 if ($this->getMajorProtocolVersion() == 1) {
724 require_once 'Zend/Gdata/App/VersionException.php';
725 throw new Zend_Gdata_App_VersionException('User activity feeds ' .
726 'are not available in API version 1.');
729 $uri = null;
730 if ($username instanceof Zend_Gdata_Query) {
731 $uri = $username->getQueryUrl();
732 } else {
733 if (count(explode(',', $username)) >
734 self::ACTIVITY_FEED_MAX_USERS) {
735 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
736 throw new Zend_Gdata_App_InvalidArgumentException(
737 'Activity feed can only retrieve for activity for up to ' .
738 self::ACTIVITY_FEED_MAX_USERS . ' users per request');
740 $uri = self::ACTIVITY_FEED_URI . '?author=' . $username;
743 return parent::getFeed($uri, 'Zend_Gdata_YouTube_ActivityFeed');
747 * Retrieve the activity of the currently authenticated users friend.
749 * @throws Zend_Gdata_App_Exception if not logged in.
750 * @return Zend_Gdata_YouTube_ActivityFeed
752 public function getFriendActivityForCurrentUser()
754 if (!$this->isAuthenticated()) {
755 require_once 'Zend/Gdata/App/Exception.php';
756 throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
757 'use the getFriendActivityForCurrentUser function in Zend_' .
758 'Gdata_YouTube.');
760 return parent::getFeed(self::FRIEND_ACTIVITY_FEED_URI,
761 'Zend_Gdata_YouTube_ActivityFeed');
765 * Retrieve a feed of messages in the currently authenticated user's inbox.
767 * @throws Zend_Gdata_App_Exception if not logged in.
768 * @return Zend_Gdata_YouTube_InboxFeed|null
770 public function getInboxFeedForCurrentUser()
772 if (!$this->isAuthenticated()) {
773 require_once 'Zend/Gdata/App/Exception.php';
774 throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
775 'use the getInboxFeedForCurrentUser function in Zend_' .
776 'Gdata_YouTube.');
779 return parent::getFeed(self::INBOX_FEED_URI,
780 'Zend_Gdata_YouTube_InboxFeed');
784 * Send a video message.
786 * Note: Either a Zend_Gdata_YouTube_VideoEntry or a valid video ID must
787 * be provided.
789 * @param string $body The body of the message
790 * @param Zend_Gdata_YouTube_VideoEntry (optional) The video entry to send
791 * @param string $videoId The id of the video to send
792 * @param string $recipientUserName The username of the recipient
793 * @throws Zend_Gdata_App_InvalidArgumentException if no valid
794 * Zend_Gdata_YouTube_VideoEntry or videoId were provided
795 * @return Zend_Gdata_YouTube_InboxEntry|null The
796 * Zend_Gdata_YouTube_Inbox_Entry representing the sent message.
799 public function sendVideoMessage($body, $videoEntry = null,
800 $videoId = null, $recipientUserName)
802 if (!$videoId && !$videoEntry) {
803 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
804 throw new Zend_Gdata_App_InvalidArgumentException(
805 'Expecting either a valid videoID or a videoEntry object in ' .
806 'Zend_Gdata_YouTube->sendVideoMessage().');
809 $messageEntry = new Zend_Gdata_YouTube_InboxEntry();
811 if ($this->getMajorProtocolVersion() == null ||
812 $this->getMajorProtocolVersion() == 1) {
814 if (!$videoId) {
815 $videoId = $videoEntry->getVideoId();
816 } elseif (strlen($videoId) < 12) {
817 //Append the full URI
818 $videoId = self::VIDEO_URI . '/' . $videoId;
821 $messageEntry->setId($this->newId($videoId));
822 // TODO there seems to be a bug where v1 inbox entries dont
823 // retain their description...
824 $messageEntry->setDescription(
825 new Zend_Gdata_YouTube_Extension_Description($body));
827 } else {
828 if (!$videoId) {
829 $videoId = $videoEntry->getVideoId();
830 $videoId = substr($videoId, strrpos($videoId, ':'));
832 $messageEntry->setId($this->newId($videoId));
833 $messageEntry->setSummary($this->newSummary($body));
836 $insertUrl = 'http://gdata.youtube.com/feeds/api/users/' .
837 $recipientUserName . '/inbox';
838 $response = $this->insertEntry($messageEntry, $insertUrl,
839 'Zend_Gdata_YouTube_InboxEntry');
840 return $response;
844 * Post a comment in reply to an existing comment
846 * @param $commentEntry Zend_Gdata_YouTube_CommentEntry The comment entry
847 * to reply to
848 * @param $commentText string The text of the comment to post
849 * @return A Zend_Gdata_YouTube_CommentEntry representing the posted
850 * comment
852 public function replyToCommentEntry($commentEntry, $commentText)
854 $newComment = $this->newCommentEntry();
855 $newComment->content = $this->newContent()->setText($commentText);
856 $commentId = $commentEntry->getId();
857 $commentIdArray = explode(':', $commentId);
859 // create a new link element
860 $inReplyToLinkHref = self::VIDEO_URI . '/' . $commentIdArray[3] .
861 '/comments/' . $commentIdArray[5];
862 $inReplyToLink = $this->newLink($inReplyToLinkHref,
863 self::IN_REPLY_TO_SCHEME, $type="application/atom+xml");
864 $links = $newComment->getLink();
865 $links[] = $inReplyToLink;
866 $newComment->setLink($links);
867 $commentFeedPostUrl = self::VIDEO_URI . '/' . $commentIdArray[3] .
868 '/comments';
869 return $this->insertEntry($newComment,
870 $commentFeedPostUrl, 'Zend_Gdata_YouTube_CommentEntry');