/[drupal]/contributions/modules/youtube_api/youtube_api.module
ViewVC logotype

Diff of /contributions/modules/youtube_api/youtube_api.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.8, Tue Jul 1 02:54:35 2008 UTC revision 1.9, Tue Jul 1 04:29:14 2008 UTC
# Line 246  function youtube_api_get_feed($feed, $st Line 246  function youtube_api_get_feed($feed, $st
246    }    }
247    
248    $result = drupal_http_request($feed, $headers, 'GET');    $result = drupal_http_request($feed, $headers, 'GET');
249      print_r($result);
250    if ($result->code == 200) {    if ($result->code == 200) {
251      return simplexml_load_string($result->data);      return simplexml_load_string($result->data);
252    }    }
# Line 253  function youtube_api_get_feed($feed, $st Line 254  function youtube_api_get_feed($feed, $st
254      return $FALSE;      return $FALSE;
255    }    }
256  }  }
257    
258    /**
259     * Retreive a list of favorites for a user.
260     *
261     * @param $user
262     *   The user to get favorites for.
263     * @param $start_at
264     *   The record to start at.
265     * @param $num_items
266     *   The number of items to return.
267     */
268    function youtube_api_get_favorites($user = 'default', $start_at = 1, $num_items = 10) {
269      $feed = t(variable_get('youtube_api_user_favorites', "http://gdata.youtube.com/feeds/api/users/!default/favorites"), array('!default' => $user));
270      $headers = array('Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token']);
271    
272      return youtube_api_process_feed(youtube_api_get_feed($feed, $start_at, $num_items, $headers));
273    }
274    
275    /**
276     * Delete a video from the currently logged in users favorites.
277     *
278     * @param $user
279     *   The video to remove from the feed.
280     */
281    function youtube_api_delete_favorite($video_id) {
282      //Do some basic sanity checking.
283      if (!isset($_SESSION['youtube_api_token'])) {
284        watchdog('youtube_api', 'Could not delete favorite video {$video_id}, no YouTube user is logged in.', WATCHDOG_NOTICE);
285        return FALSE;
286      }
287    
288      $headers = array(
289        'Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
290        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
291        'X-GData-Client' => variable_get('youtube_api_client_id', ''),
292        'Content-Type' => 'application/atom+xml',
293      );
294    
295      $feed = t(variable_get('youtube_api_user_favorites', "http://gdata.youtube.com/feeds/api/users/!default/favorites") .'/!video_id', array('!default' => 'default', '!video_id' => $video_id));
296      $result = drupal_http_request($feed, $headers, 'DELETE');
297      if ($result->code == 200) {
298        return TRUE;
299      }
300      else {
301        watchdog('youtube_api', "Could not delete video from favorites {$video_id}. ". $result->data, WATCHDOG_NOTICE);
302        return FALSE;
303      }
304    }
305    
306    /**
307     * Add a video from the currently logged in users favorites.
308     *
309     * @param $user
310     *   The video to add to the feed.
311     */
312    function youtube_api_add_favorite($video_id) {
313      //Do some basic sanity checking.
314      if (!isset($_SESSION['youtube_api_token'])) {
315        watchdog('youtube_api', 'Could not add favorite video {$video_id}, no YouTube user is logged in.', WATCHDOG_NOTICE);
316        return FALSE;
317      }
318    
319      //Create the XML and headers for the request, as outlined in
320      //http://code.google.com/apis/youtube/developers_guide_protocol.html#Favorite_Videos
321      $xml['entry'] = array(
322        '#name' => 'entry',
323        '#attributes' => array(
324          'xmlns' => youtube_api_get_schema_definition('atom'),
325        ),
326      );
327    
328      $xml['entry']['id'] = array(
329        '#name' => 'id',
330        '#value' => $video_id,
331      );
332    
333      $xml_string = youtube_api_create_xml('youtube_api_favorite', $xml);
334      $headers = array(
335        'Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
336        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
337        'X-GData-Client' => variable_get('youtube_api_client_id', ''),
338        'Content-Type' => 'application/atom+xml',
339      );
340    
341      //Make the request and report the results.
342      $feed = t(variable_get('youtube_api_user_favorites', "http://gdata.youtube.com/feeds/api/users/!default/favorites"), array('!default' => 'default'));
343      $result = drupal_http_request($feed, $headers, 'POST', $xml_string);
344      if ($result->code == 201) {
345        return TRUE;
346      }
347      else {
348        watchdog('youtube_api', "Could not add favorite video {$video_id}. ". $result->data, WATCHDOG_NOTICE);
349        return FALSE;
350      }
351    }
352    
353  /**  /**
354   * Retrieves a list of videos from YouTube's standard feeds   * Retrieves a list of videos from YouTube's standard feeds
355   *   *
# Line 270  function youtube_api_get_feed($feed, $st Line 367  function youtube_api_get_feed($feed, $st
367   *   http://code.google.com/apis/youtube/developers_guide_protocol.html#Searching_for_Videos.   *   http://code.google.com/apis/youtube/developers_guide_protocol.html#Searching_for_Videos.
368   */   */
369  function youtube_api_get_standard_feed($key, $start_at = 1, $num_items = 10, $url_params = array()) {  function youtube_api_get_standard_feed($key, $start_at = 1, $num_items = 10, $url_params = array()) {
370    $standard_feeds = array('top_rated' => 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated',    $standard_feeds = array(
371                            'top_favorites' => 'http://gdata.youtube.com/feeds/api/standardfeeds/top_favorites',      'top_rated' => 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated',
372                            'most_viewed' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed',      'top_favorites' => 'http://gdata.youtube.com/feeds/api/standardfeeds/top_favorites',
373                            'most_popular' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_popular',      'most_viewed' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed',
374                            'most_recent' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_recent',      'most_popular' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_popular',
375                            'most_discussed' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed',      'most_recent' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_recent',
376                            'most_linked' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_linked',      'most_discussed' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed',
377                            'most_responded' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_responded',      'most_linked' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_linked',
378                            'recently_featured' => 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured',      'most_responded' => 'http://gdata.youtube.com/feeds/api/standardfeeds/most_responded',
379                            'watch_on_mobile' => 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile',      'recently_featured' => 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured',
380                            'default' => 'http://gdata.youtube.com/feeds/api/standardfeeds/',      'watch_on_mobile' => 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile',
381                            );      'default' => 'http://gdata.youtube.com/feeds/api/standardfeeds/',
382      );
383    $feed = $standard_feeds[$key] ? $standard_feeds[$key] : $standard_feeds['default'] . $key;    $feed = $standard_feeds[$key] ? $standard_feeds[$key] : $standard_feeds['default'] . $key;
384    
385    return youtube_api_process_feed(youtube_api_get_feed($feed, $start_at, $num_items, $url_params));    return youtube_api_process_feed(youtube_api_get_feed($feed, $start_at, $num_items, $url_params));

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

  ViewVC Help
Powered by ViewVC 1.1.2