Index: youtube_api.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/youtube_api/youtube_api.module,v
retrieving revision 1.3
diff -u -r1.3 youtube_api.module
--- youtube_api.module	20 Jun 2008 16:26:43 -0000	1.3
+++ youtube_api.module	25 Jun 2008 06:04:44 -0000
@@ -506,6 +506,97 @@
 }
 
 /**
+ * Gets a list of video comments from the specified feed and returns them as comment objects.
+ *
+ * @param $feed
+ *   The URL of the comment feed you want.
+ * @param $start_at
+ *   The record to start at.
+ * @param $num_comments
+ *   The number of comments to return.
+ *
+ */
+function youtube_api_get_comments($feed, $start_at = 1, $num_comments = 10) {
+  $feed .= '?start-index='. urlencode($start_at) .'&max-results='. urlencode($num_comments);
+  $result = drupal_http_request($feed, array(), 'GET');
+
+  $comments = array();
+  if ($result->code == 200) {
+    $dom = simplexml_load_string($result->data);
+    foreach ($dom->entry as $entry) {
+        $comments[] = youtube_api_get_comment_object($entry);
+    }
+  }
+
+  return $comments;
+}
+
+/**
+ * Parse the returns of a single comment entry from the XML feed and return it as an object.
+ *
+ * @param $entry
+ *   The simpleXML object for a comments entry record.
+ *
+ * @TODO: Probably needs some cleanup, I could be more proficient in SimpleXML. Would love
+ * someone more proficient to give advice on approach here.
+ */
+function youtube_api_get_comment_object($entry) {
+
+  $properties = array('id', 'published', 'updated', 'title', 'content');
+  $comment = new stdClass();
+  foreach ($properties as $p) {
+    $comment->$p = (string) $entry->$p;
+  }
+
+  $comment->links = array();
+  foreach ($entry->link as $link) {
+    $attrs = $link->attributes();
+    $rel = (string) $attrs['rel'];
+    switch ($rel) {
+      case 'alternate':
+      case 'self':
+      case 'edit':
+        $comment->links[$rel] = (string) $attrs['href'];;
+        break;
+      default:
+        $parts = split("\.", $rel);
+
+        $size = count($parts) - 1;
+        $fragment = $parts[$size];
+        $comment->links[$fragment] = (string) $attrs['href'];
+        break;
+    }
+  }
+  
+  $comment->author = array();
+  $comment->author['name'] = (string) $entry->author->name;
+  $comment->author['uri'] = (string) $entry->author->uri;
+
+  return $comment;
+}
+
+/**
+ * A theme function for outputting a single comment.
+ *
+ * @param $comment
+ *   An object containing the comment you'd like to theme.
+ *
+ */
+function theme_youtube_api_comment($comment) {
+  $comment_date = strtotime($comment->published);
+  $out = "<div class='youtube-comment-wrapper'>".
+            "<div class='youtube-comment-author'>". 
+                t("<span class='youtube-comment-author'>@author</span> <span class='youtube-comment-date'>(@interval ago)</span>", array('@uri' => $comment->author['uri'], '@author' => $comment->author['name'], '@interval' => format_interval(time() - $comment_date))) .
+            "</div>".
+            "<div class='youtube_comment'>".
+                t('@comment', array('@comment' => $comment->content)) .
+             "</div>".
+          "</div>";
+  
+  return $out;
+}
+
+/**
  * A theme function for outputting embedded videos.
  *
  * @param $meta
@@ -516,9 +607,11 @@
  *   The width of the youtube video player.
  * @param $height
  *   The height of the youtube video player.
+ * @param $comments
+ *   Whether to load and display comments along with the video.
  *
  */
-function theme_youtube_api_embed($meta, $video, $width = 350, $height = 250) {
+function theme_youtube_api_embed($meta, $video, $width = 350, $height = 250, $comments = false) {
   $out = "<div class='youtube-container'><h5 class='youtube-title'>". $meta->title ."</h5>";
   $out .= "<p class='youtube-description'>". $meta->content ."</p>";
   $out .= "<object width='". $width ."' height='". $height ."'>
@@ -527,6 +620,15 @@
       type='". $video['type'] ."' width='". $width ."' height='". $height ."'>
     </embed>
   </object>";
+  if ($comments) {
+    $comments = youtube_api_get_comments($meta->comments['href'], 1, 5);
+  
+    foreach ($comments as $comment) {
+      $themed_comments[] = theme('youtube_api_comment', $comment);
+    }
+    $out .= theme('item_list', $themed_comments);
+  }
+
   $out .= "</div>";
   return $out;
 }

