| 1 |
<?php |
<?php |
| 2 |
//$Id$ |
//$Id: apiclient.inc,v 1.2.4.1 2009/05/27 01:48:28 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..) |
| 7 |
* for the video module by jyamada1 |
* for the video module by jyamada1 |
| 8 |
* |
* |
| 9 |
* @author Fabio Varesano <fvaresano at yahoo dot it> |
* @author Fabio Varesano <fvaresano at yahoo dot it> |
| 10 |
* porting to Drupal 6 |
* @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw at gmail dot com> |
|
* @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw@gmail.com> |
|
| 11 |
* @todo |
* @todo |
| 12 |
*/ |
*/ |
| 13 |
|
|
| 14 |
|
function _video_apiclient_youtube_video_entry($id, $cacheable = TRUE) { |
| 15 |
|
_video_apiclient_youtube_init('youtube'); |
| 16 |
|
//initialize Gdata lib |
| 17 |
|
$yt = new Zend_Gdata_YouTube(); |
| 18 |
|
// set API version |
| 19 |
|
$yt->setMajorProtocolVersion(2); |
| 20 |
|
// if it's a cachable request, try to load a cached value |
| 21 |
|
if ($cacheable) { |
| 22 |
|
if ($cache = cache_get($id, 'cache')) { |
| 23 |
|
return unserialize($cache->data); |
| 24 |
|
} |
| 25 |
|
} |
| 26 |
|
//get video entried |
| 27 |
|
$videoEntry = $yt->getVideoEntry($id); |
| 28 |
|
// save a cacheable result for future use |
| 29 |
|
if ($cacheable) { |
| 30 |
|
cache_set($id, serialize($videoEntry), 'cache', time() + 3600); |
| 31 |
|
} |
| 32 |
|
} |
| 33 |
|
|
| 34 |
|
/** |
| 35 |
|
* function to include necessary Zend Gdata libraries. |
| 36 |
|
*/ |
| 37 |
|
function _video_apiclient_youtube_init($op) { |
| 38 |
|
// Include Zend loader class. |
| 39 |
|
//TODO : move Zend to the library folder |
| 40 |
|
if (@include_once('youtube/Zend/Loader.php')) { |
| 41 |
|
switch ($op) { |
| 42 |
|
case 'authenticate' : |
| 43 |
|
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); |
| 44 |
|
break; |
| 45 |
|
case 'youtube' : |
| 46 |
|
Zend_Loader::loadClass('Zend_Gdata_YouTube'); |
| 47 |
|
break; |
| 48 |
|
} |
| 49 |
|
return TRUE; |
| 50 |
|
} |
| 51 |
|
else { |
| 52 |
|
watchdog('youtbe_video', "Couldn't find the Zend client libraries.", array(), WATCHDOG_ERROR); |
| 53 |
|
} |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
|
| 57 |
/** |
/** |
| 58 |
* 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, |
| 59 |
* this script can be used to request the xml and return it as an array. |
* this script can be used to request the xml and return it as an array. |
| 60 |
* Note that this is a modified function from the flickr.module, made to handle this type of |
* Note that this is a modified function from the flickr.module, made to handle this type of |
| 61 |
* call more generically. also, i suspect it could be done easier (and more quickly) in php 5. |
* call more generically. also, i suspect it could be done easier (and more quickly) in php 5. |
| 62 |
* @param $provider |
* @param $provider |
| 63 |
* the string of the third party provider, such as 'youtube' or 'google' |
* the string of the third party provider, such as 'youtube' or 'google' |
| 64 |
* @param $url |
* @param $url |
| 65 |
* the url for the xml request |
* the url for the xml request |
| 66 |
* @param $args |
* @param $args |
| 67 |
* an array of args to pass to the xml url |
* an array of args to pass to the xml url |
| 68 |
* @param $cacheable |
* @param $cacheable |
| 69 |
* optional; if true, the result of this xml request will be cached. good to play nice w/ |
* optional; if true, the result of this xml request will be cached. good to play nice w/ |
| 70 |
* the third party folks so they don't stop providing service to your site... |
* the third party folks so they don't stop providing service to your site... |
| 71 |
* @return |
* @return |
| 72 |
* the xml results returned as an array |
* the xml results returned as an array |
| 73 |
*/ |
*/ |
| 74 |
function _video_apiclient_request_xml($provider, $url, $args = array(), $cacheable = true) { |
function _video_apiclient_request_xml($provider, $url, $args = array(), $cacheable = true) { |
| 75 |
ksort($args); |
ksort($args); |
| 76 |
|
|
| 77 |
// build an argument hash that we'll use for the cache id and api signing |
// build an argument hash that we'll use for the cache id and api signing |
| 78 |
$arghash = $provider . ':'; |
$arghash = $provider . ':'; |
| 79 |
foreach($args as $k => $v){ |
foreach($args as $k => $v) { |
| 80 |
$arghash .= $k . $v; |
$arghash .= $k . $v; |
| 81 |
} |
} |
| 82 |
|
|
| 83 |
// build the url |
// build the url |
| 84 |
foreach ($args as $k => $v){ |
foreach ($args as $k => $v) { |
| 85 |
$encoded_params[] = urlencode($k).'='.urlencode($v); |
$encoded_params[] = urlencode($k).'='.urlencode($v); |
| 86 |
} |
} |
| 87 |
$url .= '?'. implode('&', $encoded_params); |
$url .= '?'. implode('&', $encoded_params); |
| 88 |
|
|
| 89 |
// if it's a cachable request, try to load a cached value |
// if it's a cachable request, try to load a cached value |
| 90 |
if ($cacheable) { |
if ($cacheable) { |
| 91 |
if ($cache = cache_get($arghash, 'cache')) { |
if ($cache = cache_get($arghash, 'cache')) { |
| 92 |
return unserialize($cache->data); |
return unserialize($cache->data); |
| 93 |
} |
} |
| 94 |
} |
} |
| 95 |
|
|
| 96 |
// connect and fetch a value |
// connect and fetch a value |
| 97 |
$result = drupal_http_request($url); |
$result = drupal_http_request($url); |
| 98 |
|
|
| 99 |
if ($result->code == 200) { |
if ($result->code == 200) { |
| 100 |
$parser = drupal_xml_parser_create($result->data); |
$parser = drupal_xml_parser_create($result->data); |
| 101 |
$vals = array(); |
$vals = array(); |
| 102 |
$index = array(); |
$index = array(); |
| 103 |
xml_parse_into_struct($parser, $result->data, $vals, $index); |
xml_parse_into_struct($parser, $result->data, $vals, $index); |
| 104 |
xml_parser_free($parser); |
xml_parser_free($parser); |
| 105 |
|
|
| 106 |
$params = array(); |
$params = array(); |
| 107 |
$level = array(); |
$level = array(); |
| 108 |
$start_level = 1; |
$start_level = 1; |
| 109 |
foreach ($vals as $xml_elem) { |
foreach ($vals as $xml_elem) { |
| 110 |
if ($xml_elem['type'] == 'open') { |
if ($xml_elem['type'] == 'open') { |
| 111 |
if (array_key_exists('attributes',$xml_elem)) { |
if (array_key_exists('attributes',$xml_elem)) { |
| 112 |
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']); |
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']); |
| 113 |
} else { |
} else { |
| 114 |
$level[$xml_elem['level']] = $xml_elem['tag']; |
$level[$xml_elem['level']] = $xml_elem['tag']; |
| 115 |
} |
} |
| 116 |
} |
} |
| 117 |
if ($xml_elem['type'] == 'complete') { |
if ($xml_elem['type'] == 'complete') { |
| 118 |
$php_stmt = '$params'; |
$php_stmt = '$params'; |
| 119 |
while($start_level < $xml_elem['level']) { |
while($start_level < $xml_elem['level']) { |
| 120 |
$php_stmt .= '[$level['.$start_level.']]'; |
$php_stmt .= '[$level['.$start_level.']]'; |
| 121 |
$start_level ++; |
$start_level ++; |
| 122 |
|
} |
| 123 |
|
$php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];'; |
| 124 |
|
eval($php_stmt); |
| 125 |
|
$start_level--; |
| 126 |
} |
} |
|
$php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];'; |
|
|
eval($php_stmt); |
|
|
$start_level--; |
|
| 127 |
} |
} |
|
} |
|
| 128 |
|
|
| 129 |
// save a cacheable result for future use |
// save a cacheable result for future use |
| 130 |
if ($cacheable) { |
if ($cacheable) { |
| 131 |
cache_set($arghash, 'cache', time() + 3600, serialize($params)); |
cache_set($arghash, serialize($params), 'cache', time() + 3600); |
| 132 |
} |
} |
| 133 |
return $params; |
return $params; |
| 134 |
} |
} |
| 135 |
return array(); |
return array(); |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
/** |
/** |
| 140 |
* Create a file object from thumbnail images from providers |
* Create a file object from thumbnail images from providers |
| 141 |
* to allow for automatic thumbnailing of videos from providers |
* to allow for automatic thumbnailing of videos from providers |
| 142 |
* @param $node |
* @param $node |
| 143 |
* the video node being called |
* the video node being called |
| 144 |
* @return |
* @return |
| 145 |
* a file object containing the thumbnail file |
* a file object containing the thumbnail file |
| 146 |
*/ |
*/ |
| 147 |
/* |
/* |
| 148 |
function _video_apiclient_provider_auto_thumbnail($node) { |
function _video_apiclient_provider_auto_thumbnail($node) { |
| 149 |
// get thumbnail url |
// get thumbnail url |