/models/datasources/vimeo_source.php

https://github.com/gmimano/newd · PHP · 217 lines · 119 code · 14 blank · 84 comment · 20 complexity · afa840fc2886ace56e41bbdcc388ebdc MD5 · raw file

  1. <?php
  2. /**
  3. * Vimeo Datasource 0.1
  4. *
  5. * Vimeo datasource to communicate with the Vimeo Simple API (Advanced on the way...)
  6. * Also utilizes the Vimeo oEmbed API for generating embed code.
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. *
  12. * @author Jon (pointlessjon) Adams <jon@anti-gen.com>
  13. * @copyright (c) n/a
  14. * @link http://github.com/pointlessjon/CakePHP-Vimeo-Datasource/tree/master
  15. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  16. * @created May 7, 2009
  17. * @version 0.1 *
  18. */
  19. App::import('Core', array('HttpSocket'));
  20. class VimeoSource extends DataFeed {
  21. var $description = 'Vimeo Simple API';
  22. var $Http = null;
  23. var $allowedRequests = array(
  24. 'user' => array(
  25. 'info',
  26. 'clips',
  27. 'likes',
  28. 'appears_in',
  29. 'all_clips',
  30. 'subscriptions',
  31. 'albums',
  32. 'channels',
  33. 'groups',
  34. 'contacts_clips',
  35. 'contacts_like'
  36. ),
  37. 'activity' => array(
  38. 'user_did',
  39. 'happened_to_user',
  40. 'contacts_did',
  41. 'happened_to_contacts',
  42. 'everyone_did'
  43. ),
  44. 'group' => array(
  45. 'clips',
  46. 'users',
  47. 'info'
  48. ),
  49. 'channel' => array(
  50. 'clips',
  51. 'info'
  52. ),
  53. 'album' => array(
  54. 'clips',
  55. 'info'
  56. )
  57. );
  58. /**
  59. * Constructor sets configuration and instantiates HttpSocket
  60. *
  61. * @param array config Optional.
  62. * @see http://www.vimeo.com/api/docs/simple-api
  63. */
  64. function __construct($config = null) {
  65. parent::__construct($config);
  66. $this->Http =& new HttpSocket();
  67. }
  68. /**
  69. * Shortcut to retrieve only the embed code of the oembed object for a specific video.
  70. *
  71. * @param string videoId Required.
  72. * @param array options Optional.
  73. * @see http://www.vimeo.com/api/docs/oembed
  74. */
  75. function embed($videoId = null, $options = null) {
  76. if (!empty($videoId)) {
  77. $_oembed = $this->oembed($videoId, $options);
  78. return $_oembed->html;
  79. }
  80. return false;
  81. }
  82. /**
  83. * Retrieve oembed object for a specific video
  84. *
  85. * @param string videoId Required.
  86. * @param array options Optional.
  87. * @see http://www.vimeo.com/api/docs/oembed
  88. */
  89. function oembed($videoId = null, $options = null) {
  90. if (!empty($videoId)) {
  91. $url = "http://vimeo.com/api/oembed.json?url=http://vimeo.com/{$videoId}";
  92. foreach ($options as $key => $value) {
  93. $url .= "&{$key}={$value}";
  94. }
  95. $response = $this->Http->get($url);
  96. if ($response === FALSE) {
  97. return false;
  98. }
  99. return json_decode($response);
  100. }
  101. return false;
  102. }
  103. /**
  104. * Retrieve data about a specific video
  105. *
  106. * @param string videoId Required.
  107. * @see http://www.vimeo.com/api/docs/simple-api
  108. */
  109. function video($videoId = null) {
  110. if (!empty($videoId)) {
  111. return $this->__vimeoApiRequest("clip/{$videoId}");
  112. }
  113. return false;
  114. }
  115. /**
  116. * Retrieve data for a specific user
  117. *
  118. * @param string username Required.
  119. * @param string request Required. See allowed requests in api documentation
  120. * @see http://www.vimeo.com/api/docs/simple-api
  121. */
  122. function userRequest($username = null, $request = null) {
  123. if (!empty($username) && !empty($request)) {
  124. if (in_array($request, $this->allowedRequests['user'])) {
  125. return $this->__vimeoApiRequest("{$username}/{$request}");
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * Retrieve activity data for a specific user
  132. *
  133. * @param string username Required.
  134. * @param string request Required. See allowed requests in api documentation
  135. * @see http://www.vimeo.com/api/docs/simple-api
  136. */
  137. function activityRequest($username = null, $request = null) {
  138. if (!empty($username) && !empty($request)) {
  139. if (in_array($request, $this->allowedRequests['activity'])) {
  140. return $this->__vimeoApiRequest("activity/{$username}/{$request}");
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * Retrieve data for a specific group
  147. *
  148. * @param string groupname Required.
  149. * @param string request Required. See allowed requests in api documentation
  150. * @see http://www.vimeo.com/api/docs/simple-api
  151. */
  152. function groupRequest($groupname = null, $request = null) {
  153. if (!empty($groupname) && !empty($request)) {
  154. if (in_array($request, $this->allowedRequests['group'])) {
  155. return $this->__vimeoApiRequest("group/{$groupname}/{$request}");
  156. }
  157. }
  158. return false;
  159. }
  160. /**
  161. * Retrieve data for a specific channel
  162. *
  163. * @param string channelname Required.
  164. * @param string request Required. See allowed requests in api documentation
  165. * @see http://www.vimeo.com/api/docs/simple-api
  166. */
  167. function channelRequest($channelname = null, $request = null) {
  168. if (!empty($channelname) && !empty($request)) {
  169. if (in_array($request, $this->allowedRequests['channel'])) {
  170. return $this->__vimeoApiRequest("channel/{$channelname}/{$request}");
  171. }
  172. }
  173. return false;
  174. }
  175. /**
  176. * Retrieve data for a specific album
  177. *
  178. * @param string albumname Required.
  179. * @param string request Required. See allowed requests in api documentation
  180. * @see http://www.vimeo.com/api/docs/simple-api
  181. */
  182. function albumRequest($albumname = null, $request = null) {
  183. if (!empty($albumname) && !empty($request)) {
  184. if (in_array($request, $this->allowedRequests['album'])) {
  185. return $this->__vimeoApiRequest("album/{$albumname}/{$request}");
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * Internal function to make the requests to the Vimeo Simple API
  192. *
  193. * @param string data Required.
  194. * @see http://www.vimeo.com/api/docs/simple-api
  195. */
  196. function __vimeoApiRequest($data = null) {
  197. if (!empty($data)) {
  198. return unserialize($this->Http->get("http://vimeo.com/api/{$data}.php", null));
  199. }
  200. return false;
  201. }
  202. }
  203. ?>