| 1 |
<?php |
<?php |
| 2 |
//$Id: apiclient.inc,v 1.2.4.1 2009/05/27 01:48:28 heshanmw Exp $ |
//$Id: apiclient.inc,v 1.2.4.2 2009/11/07 17:00:19 heshanmw Exp $ |
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* Some functions for using video hosting providers api (Youtube, Google Video, etc..) |
* Some functions for using video hosting providers api (Youtube, Google Video, etc..) |
| 10 |
* @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw at gmail dot com> |
* @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw at gmail dot com> |
| 11 |
* @todo |
* @todo |
| 12 |
*/ |
*/ |
| 13 |
|
|
| 14 |
function _video_apiclient_youtube_video_entry($id, $cacheable = TRUE) { |
// Upload token url. |
| 15 |
|
define('VIDEO_UPLOAD_GDATA_TOKEN_UPLOAD_URL', 'http://gdata.youtube.com/action/GetUploadToken'); |
| 16 |
|
|
| 17 |
|
// Note the different schemes for categories and developer tags. |
| 18 |
|
define('VIDEO_UPLOAD_GDATA_CATEGORIES_SCHEME', 'http://gdata.youtube.com/schemas/2007/categories.cat'); |
| 19 |
|
define('VIDEO_UPLOAD_GDATA_DEVELOPER_TAG_SCHEME', 'http://gdata.youtube.com/schemas/2007/developertags.cat'); |
| 20 |
|
|
| 21 |
|
// Authentication url. |
| 22 |
|
define('VIDEO_UPLOAD_GDATA_AUTHENTICATE_URL', 'https://www.google.com/youtube/accounts/ClientLogin'); |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
/** |
| 26 |
|
* Authenticate YouTube user account. |
| 27 |
|
*/ |
| 28 |
|
function _video_upload_gdata_authenticate_youtube() { |
| 29 |
|
// Username. |
| 30 |
|
// $username = variable_get('video_upload_youtube_username', FALSE); |
| 31 |
|
$username = 'heshanmw'; |
| 32 |
|
if (!$username) { |
| 33 |
|
watchdog('video_upload', 'No YouTube username set', array(), WATCHDOG_ERROR); |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
// Password. |
| 37 |
|
// $password = variable_get('video_upload_youtube_password', FALSE); |
| 38 |
|
$password = 'chicagobulls'; |
| 39 |
|
if (!$password) { |
| 40 |
|
watchdog('video_upload', 'No YouTube password set', array(), WATCHDOG_ERROR); |
| 41 |
|
} |
| 42 |
|
|
| 43 |
|
if (!$username || !$password) { |
| 44 |
|
drupal_set_message(t('YouTube Uploads not currently available'), 'error'); |
| 45 |
|
return FALSE; |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
if (_video_apiclient_youtube_init('authenticate')) { |
| 49 |
|
try { |
| 50 |
|
$http_client = Zend_Gdata_ClientLogin::getHttpClient( |
| 51 |
|
$username, // username |
| 52 |
|
$password, // password |
| 53 |
|
$service = 'youtube', |
| 54 |
|
$client = NULL, |
| 55 |
|
$source = 'drupal', |
| 56 |
|
$loginToken = NULL, |
| 57 |
|
$loginCaptcha = NULL, |
| 58 |
|
VIDEO_UPLOAD_GDATA_AUTHENTICATE_URL |
| 59 |
|
); |
| 60 |
|
} |
| 61 |
|
catch (Zend_Gdata_App_AuthException $authEx) { |
| 62 |
|
drupal_set_message(t('YouTube uploads currently unavailable'), 'error'); |
| 63 |
|
watchdog('video_upload', 'Authentication error for YouTube Account', array(), WATCHDOG_ERROR); |
| 64 |
|
return FALSE; |
| 65 |
|
} |
| 66 |
|
catch (Zend_Gdata_App_HttpException $e) { |
| 67 |
|
drupal_set_message(t('YouTube uploads currently unavailable'), 'error'); |
| 68 |
|
watchdog('video_upload', 'Authentication error for YouTube Account: %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR); |
| 69 |
|
return FALSE; |
| 70 |
|
} |
| 71 |
|
} |
| 72 |
|
else { |
| 73 |
|
return FALSE; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
// Pass developer key. |
| 77 |
|
// $developer_key = variable_get('video_upload_youtube_developer_key', FALSE); |
| 78 |
|
$developer_key = variable_get('video_youtube_api_key', ''); |
| 79 |
|
if (!$developer_key) { |
| 80 |
|
watchdog('video_upload', 'No developer key set', array(), WATCHDOG_ERROR); |
| 81 |
|
drupal_set_message(t('YouTube Uploads not currently available'), 'error'); |
| 82 |
|
return FALSE; |
| 83 |
|
} |
| 84 |
|
$http_client->setHeaders('X-GData-Key', 'key=' . $developer_key); |
| 85 |
|
|
| 86 |
|
return $http_client; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
/* |
| 90 |
|
* Create a Zend YouTube object. |
| 91 |
|
* @param mixed $http_client |
| 92 |
|
* Either a boolean (if FALSE, an un-authenticated connection is |
| 93 |
|
* made), or an authentication object. |
| 94 |
|
* @return object Zend YouTube object |
| 95 |
|
*/ |
| 96 |
|
function _video_apiclient_youtube_gdata($http_client = NULL) { |
| 97 |
_video_apiclient_youtube_init('youtube'); |
_video_apiclient_youtube_init('youtube'); |
| 98 |
|
try { |
| 99 |
|
$yt = new Zend_Gdata_YouTube($http_client); |
| 100 |
|
// Set API version to 2.0. |
| 101 |
|
$yt->setMajorProtocolVersion(2); |
| 102 |
|
} |
| 103 |
|
catch (Zend_Gdata_App_HttpException $e) { |
| 104 |
|
watchdog('youtube_video', 'Authentication error while creating a YouTube connection object: %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR); |
| 105 |
|
} |
| 106 |
|
return $yt; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
function _video_apiclient_youtube_video_entry($id, $cacheable = TRUE) { |
| 110 |
|
//_video_apiclient_youtube_init('youtube'); |
| 111 |
|
// Get new http authentication. |
| 112 |
|
$http_client = _video_upload_gdata_authenticate_youtube(); |
| 113 |
//initialize Gdata lib |
//initialize Gdata lib |
| 114 |
$yt = new Zend_Gdata_YouTube(); |
$yt = _video_apiclient_youtube_gdata($http_client); |
| 115 |
|
//$yt = new Zend_Gdata_YouTube($http_client); |
| 116 |
// set API version |
// set API version |
| 117 |
$yt->setMajorProtocolVersion(2); |
//$yt->setMajorProtocolVersion(2); |
| 118 |
// if it's a cachable request, try to load a cached value |
// if it's a cachable request, try to load a cached value |
| 119 |
if ($cacheable) { |
// if ($cacheable) { |
| 120 |
if ($cache = cache_get($id, 'cache')) { |
// if ($cache = cache_get($id, 'cache')) { |
| 121 |
return unserialize($cache->data); |
// return unserialize($cache->data); |
| 122 |
} |
// } |
| 123 |
|
// } |
| 124 |
|
try{ |
| 125 |
|
//get video entried |
| 126 |
|
$videoEntry = $yt->getVideoEntry($id); |
| 127 |
|
watchdog('youtube_video', $videoEntry->getVideoDuration().$videoEntry->getVideoId().$videoEntry->getVideoTitle()); |
| 128 |
|
// print_r($videoEntry); |
| 129 |
|
// exit; |
| 130 |
} |
} |
| 131 |
//get video entried |
catch (Zend_Gdata_App_HttpException $e){ |
| 132 |
$videoEntry = $yt->getVideoEntry($id); |
watchdog('youtube_video', 'Authentication error while receiving YouTube connection object: %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR); |
|
// save a cacheable result for future use |
|
|
if ($cacheable) { |
|
|
cache_set($id, serialize($videoEntry), 'cache', time() + 3600); |
|
| 133 |
} |
} |
| 134 |
|
// save a cacheable result for future use |
| 135 |
|
// if ($cacheable) { |
| 136 |
|
// cache_set($id, serialize($videoEntry), 'cache', time() + 3600); |
| 137 |
|
// } |
| 138 |
|
return $videoEntry; |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
/** |
/** |
| 143 |
*/ |
*/ |
| 144 |
function _video_apiclient_youtube_init($op) { |
function _video_apiclient_youtube_init($op) { |
| 145 |
// Include Zend loader class. |
// Include Zend loader class. |
| 146 |
|
_video_upload_gdata_set_include_path(); |
| 147 |
//TODO : move Zend to the library folder |
//TODO : move Zend to the library folder |
| 148 |
if (@include_once('youtube/Zend/Loader.php')) { |
if (@include_once('Zend/Loader.php')) { |
| 149 |
switch ($op) { |
switch ($op) { |
| 150 |
case 'authenticate' : |
case 'authenticate' : |
| 151 |
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); |
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); |
| 161 |
} |
} |
| 162 |
} |
} |
| 163 |
|
|
| 164 |
|
/** |
| 165 |
|
* Set include path for Zend GData client libraries, which should be |
| 166 |
|
* installed locally in the module directory. |
| 167 |
|
*/ |
| 168 |
|
function _video_upload_gdata_set_include_path() { |
| 169 |
|
// Include path must be modified. |
| 170 |
|
static $path; |
| 171 |
|
if (!$path) { |
| 172 |
|
$path = get_include_path(); |
| 173 |
|
set_include_path($path . PATH_SEPARATOR . drupal_get_path('module', 'video') . '/includes/youtube'); |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
|
|
| 177 |
/** |
/** |
| 178 |
* When an include file requires to read an xml to receive information, such as for thumbnails, |
* When an include file requires to read an xml to receive information, such as for thumbnails, |