/[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.7, Sat Jun 28 23:16:25 2008 UTC revision 1.8, Tue Jul 1 02:54:35 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: youtube_api.module,v 1.6 2008/06/26 04:46:43 beeradb Exp $  // $Id: youtube_api.module,v 1.7 2008/06/28 23:16:25 beeradb Exp $
3    
4    /**
5     * @file
6     * Provides integration to the YouTube Public API.
7     */
8    
9  /**  /**
10   *  Implement hook_menu.   *  Implement hook_menu.
# Line 57  function youtube_api_admin_form() { Line 62  function youtube_api_admin_form() {
62        '#title' => t('URL for API Upload Calls'),        '#title' => t('URL for API Upload Calls'),
63        '#default_value' => variable_get('youtube_api_upload_location', 'http://uploads.gdata.youtube.com/feeds/api/users/!user/uploads'),        '#default_value' => variable_get('youtube_api_upload_location', 'http://uploads.gdata.youtube.com/feeds/api/users/!user/uploads'),
64        '#description' => t('This should only be changed if the YouTube API specifications change. !user will be replaced by the currently logged in username'),        '#description' => t('This should only be changed if the YouTube API specifications change. !user will be replaced by the currently logged in username'),
65      );
66    
67      $form['youtube_api_video_rating_location'] = array(
68          '#type' => 'textfield',
69          '#title' => t('URL for Video Ratings'),
70          '#default_value' => variable_get('youtube_api_video_comments_location', 'http://gdata.youtube.com/feeds/api/videos/!video_id/ratings'),
71          '#description' => t('This should only be changed if the YouTube API specifications change. !video_id will be automatically replaced by a video_id'),
72      );
73    
74      $form['youtube_api_video_rating_location'] = array(
75          '#type' => 'textfield',
76          '#title' => t('URL for Video Ratings'),
77          '#default_value' => variable_get('youtube_api_video_ratings_location', 'http://gdata.youtube.com/feeds/api/videos/!video_id/ratings'),
78          '#description' => t('This should only be changed if the YouTube API specifications change. !video_id will be automatically replaced by a video_id'),
79      );
80    
81      $form['youtube_api_video_response_location'] = array(
82          '#type' => 'textfield',
83          '#title' => t('URL for Video Responses'),
84          '#default_value' => variable_get('youtube_api_video_response_location', 'http://gdata.youtube.com/feeds/api/videos/!video_id/responses'),
85          '#description' => t('This should only be changed if the YouTube API specifications change. !video_id will be automatically replaced by a video_id'),
86    );    );
87    
88    $form['youtube_api_user_upload_feed'] = array(    $form['youtube_api_user_upload_feed'] = array(
# Line 150  function youtube_api_video_upload($xml, Line 175  function youtube_api_video_upload($xml,
175    $data .= "\r\n--". $boundary ."--";    $data .= "\r\n--". $boundary ."--";
176    
177    $headers = array('Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],    $headers = array('Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
178                        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),                     'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
179                        'X-GData-Client' => variable_get('youtube_api_client_id', ''),                     'X-GData-Client' => variable_get('youtube_api_client_id', ''),
180                        'Slug' => $filename,                     'Slug' => $filename,
181                        'Content-Type' => 'multipart/related; boundary="'. $boundary .'"',                     'Content-Type' => 'multipart/related; boundary="'. $boundary .'"',
182                        'Connection' => 'close',                     'Connection' => 'close',
183                  );                     );
184    $location = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';    $location = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
185    
186    watchdog('video_upload', t("Sending payload to youtube..."), WATCHDOG_NOTICE);    watchdog('video_upload', t("Sending payload to youtube..."), WATCHDOG_NOTICE);
# Line 225  function youtube_api_get_feed($feed, $st Line 250  function youtube_api_get_feed($feed, $st
250      return simplexml_load_string($result->data);      return simplexml_load_string($result->data);
251    }    }
252    else {    else {
253      return $false;      return $FALSE;
254    }    }
255  }  }
256  /**  /**
# Line 235  function youtube_api_get_feed($feed, $st Line 260  function youtube_api_get_feed($feed, $st
260   *  The key of the feed to get, as outlined here:   *  The key of the feed to get, as outlined here:
261   *  http://code.google.com/apis/youtube/developers_guide_protocol.html#Standard_feeds.   *  http://code.google.com/apis/youtube/developers_guide_protocol.html#Standard_feeds.
262   * @param $comments   * @param $comments
263   *  True if parsing a comment feed, false otherwise.   *  TRUE if parsing a comment feed, FALSE otherwise.
264   * @param $start_at   * @param $start_at
265   *   The record to start at.   *   The record to start at.
266   * @param $num_items   * @param $num_items
# Line 268  function youtube_api_get_standard_feed($ Line 293  function youtube_api_get_standard_feed($
293   * @param $feed   * @param $feed
294   *   A simplexml object feed data.   *   A simplexml object feed data.
295   * @param $comments   * @param $comments
296   *  True if parsing a comment feed, false otherwise.   *  TRUE if parsing a comment feed, FALSE otherwise.
297   */   */
298  function youtube_api_process_feed($feed, $comments = false) {  function youtube_api_process_feed($feed, $comments = FALSE) {
299    if ($feed) {    if ($feed) {
300      $objects = array();      $objects = array();
301      if (count($feed->entry)) {      if (count($feed->entry)) {
# Line 494  function youtube_api_get_schema_definiti Line 519  function youtube_api_get_schema_definiti
519  function youtube_api_update_video($xml, $video_id) {  function youtube_api_update_video($xml, $video_id) {
520    $feed = t(variable_get('youtube_api_upload_location', 'http://uploads.gdata.youtube.com/feeds/api/users/!user/uploads') ."/". $video_id, array('!user' => $_SESSION['youtube_api_user']));    $feed = t(variable_get('youtube_api_upload_location', 'http://uploads.gdata.youtube.com/feeds/api/users/!user/uploads') ."/". $video_id, array('!user' => $_SESSION['youtube_api_user']));
521    $headers = array('Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],    $headers = array('Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
522                        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),                     'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
523                        'X-GData-Client' => variable_get('youtube_api_client_id', ''),                     'X-GData-Client' => variable_get('youtube_api_client_id', ''),
524                        'Content-Type' => 'application/atom+xml',                     'Content-Type' => 'application/atom+xml',
525    );    );
526    
527    $result = drupal_http_request($feed, $headers, 'PUT', $xml);    $result = drupal_http_request($feed, $headers, 'PUT', $xml);
528    if ($result->code ==200) {    if ($result->code ==200) {
529      watchdog('youtube_api', t("Video %video_id updated successfully.", array('%video_id' => $email)), WATCHDOG_NOTICE);      watchdog('youtube_api', t("Video %video_id updated successfully.", array('%video_id' => $video_id)), WATCHDOG_NOTICE);
530      return true;      return TRUE;
531    }    }
532    else {    else {
533      watchdog('youtube_api', t("Video %video_id could not be updated. ". $result->data, array('%video_id' => $email)), WATCHDOG_NOTICE);      watchdog('youtube_api', t("Video %video_id could not be updated. ". $result->data, array('%video_id' => $video_id)), WATCHDOG_NOTICE);
534      return false;      return FALSE;
535    }    }
536  }  }
537    
# Line 518  function youtube_api_update_video($xml, Line 543  function youtube_api_update_video($xml,
543   */   */
544  function youtube_api_delete_video($video_id) {  function youtube_api_delete_video($video_id) {
545    $feed = t(variable_get('youtube_api_upload_location', 'http://uploads.gdata.youtube.com/feeds/api/users/!user/uploads') ."/". $video_id, array('!user' => $_SESSION['youtube_api_user']));    $feed = t(variable_get('youtube_api_upload_location', 'http://uploads.gdata.youtube.com/feeds/api/users/!user/uploads') ."/". $video_id, array('!user' => $_SESSION['youtube_api_user']));
546    $headers = array('Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],    $headers = array(
547                        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),      'Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
548                        'X-GData-Client' => variable_get('youtube_api_client_id', ''),      'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
549                        'Content-Type' => 'application/atom+xml',      'X-GData-Client' => variable_get('youtube_api_client_id', ''),
550        'Content-Type' => 'application/atom+xml',
551    );    );
552    $result = drupal_http_request($feed, $headers, 'DELETE');    $result = drupal_http_request($feed, $headers, 'DELETE');
553  }  }
# Line 540  function youtube_api_delete_video($video Line 566  function youtube_api_delete_video($video
566   *   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.
567   *   *
568   */   */
569  function youtube_api_search($term, $start_at = 1, $num_items = 10, $url_params = array()) {  function youtube_api_video_search($term, $start_at = 1, $num_items = 10, $url_params = array()) {
570    $feed = variable_get('youtube_api_category_feed', 'http://gdata.youtube.com/feeds/api/videos');    $feed = variable_get('youtube_api_category_feed', 'http://gdata.youtube.com/feeds/api/videos');
571    $url_params += array('vq' => $term);    $url_params += array('vq' => $term);
572    
# Line 578  function youtube_api_get_video($vid) { Line 604  function youtube_api_get_video($vid) {
604   *   *
605   */   */
606  function youtube_api_get_comments($feed, $start_at = 1, $num_comments = 10) {  function youtube_api_get_comments($feed, $start_at = 1, $num_comments = 10) {
607    return youtube_api_process_feed(youtube_api_get_feed($feed, $start_at, $num_comments), true);    return youtube_api_process_feed(youtube_api_get_feed($feed, $start_at, $num_comments), TRUE);
608  }  }
609    
610  /**  /**
# Line 625  function youtube_api_get_comment_object( Line 651  function youtube_api_get_comment_object(
651    return $comment;    return $comment;
652  }  }
653    
654    
655    /**
656     * Rates a specified video.
657     *
658     * @param $video_id
659     *   The id of the video to rate.
660     * @param $rating
661     *   The rating to assign.
662     * @param $min
663     *   The minimum allowed rating.
664     * @param $max
665     *   The maximum allowed rating.
666     *
667     */
668    function youtube_api_rate_video($video_id, $rating, $min = 1, $max = 5) {
669      //Do some basic sanity checking.
670      if (!($min <= $rating && $rating <= $max))
671      {
672        watchdog('youtube_api', "Rating of video {$video_id} failed checked of {$min} <= {$rating} <= {$max}", WATCHDOG_NOTICE);
673        return FALSE;
674      }
675      if (!isset($_SESSION['youtube_api_token'])) {
676        watchdog('youtube_api', "Rating of video {$video_id} failed because no user is logged in", WATCHDOG_NOTICE);
677        return FALSE;
678      }
679    
680      //Create the XML and headers for the request, as outlined in
681      //http://code.google.com/apis/youtube/developers_guide_protocol.html#Ratings
682      $xml['entry'] = array(
683        '#name' => 'entry',
684        '#attributes' => array(
685          'xmlns' => youtube_api_get_schema_definition('atom'),
686          'xmlns:gd' => youtube_api_get_schema_definition('gd'), ),
687      );
688    
689      $xml['entry']['gd:rating'] = array(
690        '#name' => 'gd:rating',
691        '#attributes' => array(
692          'min' => $min,
693          'max' => $max,
694          'value' => $rating,
695        ),
696      );
697    
698      $xml_string = youtube_api_create_xml('youtube_api_rating', $xml);
699      $headers = array(
700        'Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
701        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
702        'X-GData-Client' => variable_get('youtube_api_client_id', ''),
703        'Content-Type' => 'application/atom+xml',
704      );
705    
706      //Make the request and report the result.
707      $feed = t(variable_get('youtube_api_video_rating_location', 'http://gdata.youtube.com/feeds/api/videos/!video_id/ratings'), array('!video_id' => $video_id));
708      $result = drupal_http_request($feed, $headers, 'POST', $xml_string);
709      if ($result->code == 201) {
710        return TRUE;
711      }
712      else {
713        watchdog('youtube_api', "Could not rate video {$video_id}. ". $result->data, WATCHDOG_NOTICE);
714        return FALSE;
715      }
716    }
717    
718    
719    /**
720     * Replies to a video or a comment.
721     *
722     * @param $target
723     *   The target item to respond to, as specified by a video or comment object.
724     * @param $comment
725     *   The comment.
726     *
727     */
728    function youtube_api_add_comment($target, $comment) {
729      //Do some basic sanity checking.
730      if (!isset($_SESSION['youtube_api_token'])) {
731        watchdog('youtube_api', 'Could not add comment to video {$video_id}, no YouTube user is logged in.', WATCHDOG_NOTICE);
732        return FALSE;
733      }
734    
735      //Create the XML and headers for the request, as outlined in
736      //http://code.google.com/apis/youtube/developers_guide_protocol.html#Comments
737      $xml['entry'] = array(
738        '#name' => 'entry',
739        '#attributes' => array(
740          'xmlns' => youtube_api_get_schema_definition('atom'),
741          'xmlns:yt' => youtube_api_get_schema_definition('yt'),
742        ),
743      );
744    
745      $xml['entry']['content'] = array(
746        '#name' => 'content',
747        '#value' => $comment,
748      );
749    
750      $xml_string = youtube_api_create_xml('youtube_api_comment', $xml);
751      $headers = array(
752        'Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
753        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
754        'X-GData-Client' => variable_get('youtube_api_client_id', ''),
755        'Content-Type' => 'application/atom+xml',
756      );
757    
758      //Make the request and report the results.
759      $result = drupal_http_request($target, $headers, 'POST', $xml_string);
760      if ($result->code == 201) {
761        return TRUE;
762      }
763      else {
764        watchdog('youtube_api', "Could not comment on video {$video_id}. ". $result->data, WATCHDOG_NOTICE);
765        return FALSE;
766      }
767    }
768    
769    /**
770     * Create a video response request.
771     *
772     * @param $target_video
773     *   The video_id of the response target.
774     * @param $response_video
775     *   The video_id of response.
776     *
777     */
778    function youtube_api_add_response($target_video, $response_video) {
779      //Do some basic sanity checking.
780      if (!isset($_SESSION['youtube_api_token'])) {
781        watchdog('youtube_api', 'Could not add response to video {$video_id}, no YouTube user is logged in.', WATCHDOG_NOTICE);
782        return FALSE;
783      }
784    
785      //Create the XML and headers for the request, as outlined in
786      //http://code.google.com/apis/youtube/developers_guide_protocol.html#Responses
787      $xml['entry'] = array(
788        '#name' => 'entry',
789        '#attributes' => array(
790          'xmlns' => youtube_api_get_schema_definition('atom'),
791        ),
792      );
793    
794      $xml['entry']['id'] = array(
795        '#name' => 'id',
796        '#value' => $response_video,
797      );
798    
799      $xml_string = youtube_api_create_xml('youtube_api_response', $xml);
800      $headers = array(
801        'Authorization' => "GoogleLogin auth=". $_SESSION['youtube_api_token'],
802        'X-GData-Key' => "key=". variable_get('youtube_api_key', ''),
803        'X-GData-Client' => variable_get('youtube_api_client_id', ''),
804        'Content-Type' => 'application/atom+xml',
805      );
806    
807    
808      //Make the request and report the results.
809      $feed = t(variable_get('youtube_api_video_response_location', 'http://gdata.youtube.com/feeds/api/videos/!video_id/responses'), array('!video_id' => $target_video));
810      $result = drupal_http_request($feed, $headers, 'POST', $xml_string);
811    
812      if ($result->code == 201) {
813        return TRUE;
814      }
815      else {
816        watchdog('youtube_api', "Could not respond to  video {$video_id}. ". $result->data, WATCHDOG_NOTICE);
817        return FALSE;
818      }
819    }
820    
821  /**  /**
822   * A theme function for outputting a single comment.   * A theme function for outputting a single comment.
823   *   *
# Line 661  function theme_youtube_api_comment($comm Line 854  function theme_youtube_api_comment($comm
854   *   Whether to load and display comments along with the video.   *   Whether to load and display comments along with the video.
855   *   *
856   */   */
857  function theme_youtube_api_embed($meta, $video, $width = 350, $height = 250, $print_comments = false) {  function theme_youtube_api_embed($meta, $video, $width = 350, $height = 250, $print_comments = FALSE) {
858    $out = "<div class='youtube-container'><h5 class='youtube-title'>". $meta->title ."</h5>";    $out = "<div class='youtube-container'><h5 class='youtube-title'>". $meta->title ."</h5>";
859    $out .= "<p class='youtube-description'>". $meta->content ."</p>";    $out .= "<p class='youtube-description'>". $meta->content ."</p>";
860    $out .= "<object width='". $width ."' height='". $height ."'>    $out .= "<object width='". $width ."' height='". $height ."'>

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

  ViewVC Help
Powered by ViewVC 1.1.2