PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/youtube.php

https://github.com/jacknl/t2
PHP | 309 lines | 175 code | 40 blank | 94 comment | 33 complexity | 0dafdea92199f85baefa1018089ff634 MD5 | raw file
  1. <?php
  2. /**
  3. * Gaurav Mishra | http://phpcollection.com/
  4. * MSN: gauravtechie@hotmail.com
  5. * Email: gaurav@hostcurry.com
  6. * Started: 02/03/2011 19:44 (IST)
  7. * Tested: Yes
  8. * PHP 4/5: 5
  9. * No warranty is given to code used
  10. */
  11. /**
  12. * Youtube API
  13. *
  14. * @package Youtube API
  15. * @url http://phpcollecttion.com/classes/youtube
  16. * @desc Package which allows easy interaction with the Youtube v2 Data API
  17. * @author Gaurav Mishra
  18. * @copyright 2011
  19. * @version 0.0.1
  20. * @access public
  21. * @license GPL
  22. */
  23. /**
  24. * Todo:-
  25. */
  26. class youtube
  27. {
  28. /** Your API KEY */
  29. public $key;
  30. /** URI to the API , used for making REST call. */
  31. var $feed = "http://gdata.youtube.com/feeds/api/";
  32. /**
  33. * Youtube::__construct()
  34. *
  35. * @param mixed $apikey Your developer API Key
  36. * @param bool $check Should we run checks to make sure all the extensions are loaded?
  37. * @return void
  38. */
  39. public function __construct($apikey = null, $partner_id = null, $check = true)
  40. {
  41. /** Construct class, runs on initialization **/
  42. if ($check) {
  43. /** If the check parametre is set **/
  44. if (!extension_loaded('curl') /** Is the cURL extension loaded? **/ ) {
  45. throw new Exception('You don\'t have cURL loaded.');
  46. /** If not, throw an exception **/
  47. }
  48. if (!extension_loaded('SimpleXML') /** Is the SimpleXML extension loaded? **/ ) {
  49. throw new Exception('You don\'t have the SimpleXML lib loaded.');
  50. /** If not, throw an exception **/
  51. }
  52. }
  53. }
  54. function check_youtube_db($profile_id, $watchUrl){
  55. global $db;
  56. $q = "SELECT * FROM `youtube_data` WHERE `profile_ID` = '$profile_id' AND `watchUrl` = '$watchUrl'";
  57. if($db->get_row($q) == null){
  58. return false;
  59. }else{
  60. return true;
  61. }
  62. }
  63. function top_videos($profile_id) {
  64. global $db;
  65. $q = "SELECT * from `youtube_data` WHERE `profile_ID` = '$profile_id' ORDER by `viewCount` DESC LIMIT 0, 7";
  66. //echo $q;
  67. $data = $db->get_results($q, ARRAY_A);
  68. return $data;
  69. }
  70. function get_youtube_data($profile_id, $to , $from) {
  71. global $db;
  72. $query = "SELECT * from `youtube_data` WHERE `profile_id` = '$profile_id' AND `created` BETWEEN '$from' AND '$to'";
  73. $data = $db->get_results($query, ARRAY_A);
  74. return $data;
  75. }
  76. /**
  77. * Youtube::search_videos()
  78. *
  79. * @param mixed $
  80. * @return array $response Array of data of results
  81. */
  82. function search_videos($query, $orderby = null, $time = null, $start = 0, $max = 0)
  83. {
  84. $url = "";
  85. if ($start != null) {
  86. $url .= "&start-index=$start";
  87. }
  88. if ($max != null) {
  89. $url .= "&max-results=$max";
  90. }
  91. if ($orderby != null) {
  92. $url .= "&orderby=$orderby";
  93. }
  94. if ($time != null) {
  95. $url .= "&time=$time";
  96. }
  97. $query = urlencode($query);
  98. /** URI used for making REST call. Each Web Service uses a unique URL.*/
  99. $request = $this->feed . "videos?q=$query" . $url;
  100. echo "Link is being prepared for the keyword ".$query."\n";
  101. /** Get the response via CURL */
  102. $response = $this->get($request);
  103. //print_r($response);
  104. /** search data from the xml result*/
  105. $video_arrray = $this->get_search_data($response);
  106. // print_r($video_array);
  107. // exit();
  108. /** Arrayed data of videos*/
  109. return $video_arrray;
  110. }
  111. function search_channel($query, $orderby = null, $time = null, $start = 0, $max = 0)
  112. {
  113. $url = "";
  114. if ($start != null) {
  115. $url .= "&start-index=$start";
  116. }
  117. if ($max != null) {
  118. $url .= "&max-results=$max";
  119. }
  120. if ($orderby != null) {
  121. $url .= "&orderby=$orderby";
  122. }
  123. if ($time != null) {
  124. $url .= "&time=$time";
  125. }
  126. $query = urlencode($query);
  127. /** URI used for making REST call. Each Web Service uses a unique URL.*/
  128. $request = $this->feed . "channels?q=$query" . $url;
  129. /** Get the response via CURL */
  130. $response = $this->get($request);
  131. /** search data from the xml result*/
  132. $video_arrray = $this->get_search_data($response);
  133. /** Arrayed data of videos*/
  134. return $video_arrray;
  135. }
  136. /**
  137. * Youtube::video_data()
  138. *
  139. * @param void
  140. * @return array $data Array of data of results
  141. */
  142. function get_video_insight_by_id($video_id)
  143. {
  144. /** URI used for making REST call. Each Web Service uses a unique URL.*/
  145. echo "video insight for $video_id is being called.\n";
  146. $request = $this->feed . "videos/$video_id?v=2";
  147. //print_r($request);
  148. /** Get the response via CURL */
  149. $response = $this->get($request);
  150. /** Arrayed data of video*/
  151. return $response;
  152. }
  153. function get_video_insight_by_url($request)
  154. {
  155. //echo $request;
  156. /** Get the response via CURL */
  157. $response = $this->get($request);
  158. //print_r($response);
  159. $response_data = $this->parseVideoEntry($response);
  160. /** Arrayed data of video*/
  161. return $response_data;
  162. }
  163. /**
  164. * Youtube::get()
  165. *
  166. * @param mixed $request request data from the youtube gdata api !
  167. * @return array $data arrayed data from the request
  168. */
  169. function get($url, $username = '', $password = '')
  170. {
  171. $ch = curl_init();
  172. curl_setopt($ch, CURLOPT_URL, $url);
  173. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  174. $xml = curl_exec($ch);
  175. $Headers = curl_getinfo($ch);
  176. curl_close($ch);
  177. //print_r($Headers);
  178. if ($Headers['http_code'] == 200) {
  179. // $xml = file_get_contents($url);
  180. $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  181. //print_r($data);
  182. return $data;
  183. } else {
  184. return null;
  185. }
  186. }
  187. function get_search_data($xml)
  188. {
  189. $i = 0;
  190. /** Traverse XML tree and save desired values from child nodes */
  191. //print_r($xml->entry);
  192. if($xml->entry != ''){
  193. foreach ($xml->entry as $result) {
  194. //print_r($result);
  195. $data[$i]['id'] = (string )$result->id;
  196. $data[$i]['published'] = (string )$result->published;
  197. $data[$i]['updated'] = (string )$result->updated;
  198. $data[$i]['title'] = (string )$result->title;
  199. $data[$i]['content'] = (string )$result->content;
  200. $data[$i]['author'] = (string )$result->author->name;
  201. $data[$i]['author_uri'] = (string )$result->author->uri;
  202. $data[$i]['video_link'] = (string )$result->link[0]->attributes()->href[0];
  203. echo "A video with id=".$result->id." is being inserted in the Array\n";
  204. $i++;
  205. }
  206. }else{
  207. $data = array();
  208. }
  209. //print_r($data);
  210. return $data;
  211. }
  212. function parseVideoEntry($entry)
  213. {
  214. $obj = new stdClass;
  215. /** get nodes in media: namespace for media information */
  216. $media = $entry->children('http://search.yahoo.com/mrss/');
  217. //print_r($entry->published);
  218. //print_r($media->group->published);
  219. $obj->title = $media->group->title;
  220. $obj->description = $media->group->description;
  221. $obj->published = strtotime($entry->published);
  222. /** get video player URL */
  223. $attrs = $media->group->player->attributes();
  224. $obj->watchURL = $attrs['url'];
  225. // get video thumbnail
  226. $attrs = $media->group->thumbnail[0]->attributes();
  227. $obj->thumbnailURL = $attrs['url'];
  228. // get <yt:duration> node for video length
  229. $yt = $media->children('http://gdata.youtube.com/schemas/2007');
  230. $attrs = $yt->duration->attributes();
  231. $obj->length = $attrs['seconds'];
  232. // get <yt:stats> node for viewer statistics
  233. $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
  234. $attrs = $yt->statistics->attributes();
  235. $obj->viewCount = $attrs['viewCount'];
  236. // get <gd:rating> node for video ratings
  237. $gd = $entry->children('http://schemas.google.com/g/2005');
  238. if ($gd->rating) {
  239. $attrs = $gd->rating->attributes();
  240. $obj->rating = $attrs['average'];
  241. } else {
  242. $obj->rating = 0;
  243. }
  244. // get <gd:comments> node for video comments
  245. $gd = $entry->children('http://schemas.google.com/g/2005');
  246. if ($gd->comments->feedLink) {
  247. $attrs = $gd->comments->feedLink->attributes();
  248. $obj->commentsURL = $attrs['href'];
  249. $obj->commentsCount = $attrs['countHint'];
  250. }
  251. // get feed URL for video responses
  252. $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
  253. $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
  254. 2007#video.responses']");
  255. if (count($nodeset) > 0) {
  256. $obj->responsesURL = $nodeset[0]['href'];
  257. }
  258. // get feed URL for related videos
  259. $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
  260. $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
  261. 2007#video.related']");
  262. if (count($nodeset) > 0) {
  263. $obj->relatedURL = $nodeset[0]['href'];
  264. }
  265. // return object to caller
  266. return $obj;
  267. }
  268. }
  269. ?>