]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/youtube.rb
chucknorris: read gzip stream before passing it to YAML.load
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / youtube.rb
index 5f1aa8223ba28af3d88d6efce1d6aa01ae8035fe..abda9ca311fa88e5037f220c680a1583cd22a4fb 100644 (file)
@@ -12,7 +12,7 @@ class YouTubePlugin < Plugin
   YOUTUBE_SEARCH = "http://gdata.youtube.com/feeds/api/videos?vq=%{words}&orderby=relevance"
   YOUTUBE_VIDEO = "http://gdata.youtube.com/feeds/api/videos/%{id}"
 
-  YOUTUBE_VIDEO_URLS = %r{youtube.com/(?:watch\?v=|v/)(.*?)(&.*)?$}
+  YOUTUBE_VIDEO_URLS = %r{youtube.com/(?:watch\?(?:.*&)?v=|v/)(.*?)(&.*)?$}
 
   Config.register Config::IntegerValue.new('youtube.hits',
     :default => 3,
@@ -24,10 +24,14 @@ class YouTubePlugin < Plugin
     :default => true,
     :desc => "Should the bot display alternative URLs (swf, rstp) for YouTube videos?")
 
+  def help(plugin, topic="")
+    'youtube [search] <query> : searches youtube videos | youtube info <id> : returns description and video links'
+  end
+
   def youtube_filter(s)
     loc = Utils.check_location(s, /youtube\.com/)
     return nil unless loc
-    if s[:text].include? '<div id="vidTitle">'
+    if s[:text].include? '<link rel="alternate" type="text/xml+oembed"'
       vid = @bot.filter(:"youtube.video", s)
       return nil unless vid
       content = _("Category: %{cat}. Rating: %{rating}. Author: %{author}. Duration: %{duration}. %{views} views, faved %{faves} times. %{desc}") % vid
@@ -40,10 +44,11 @@ class YouTubePlugin < Plugin
     end
     # otherwise, just grab the proper div
     if defined? Hpricot
-      content = (Hpricot(s[:text])/"#mainContent").to_html.ircify_html
+      content = (Hpricot(s[:text])/".watch-video-desc").to_html.ircify_html
     end
     # suboptimal, but still better than the default HTML info extractor
-    content ||= /<div id="mainContent"[^>]*>/.match(s[:text]).post_match.ircify_html
+    dm = /<div\s+class="watch-video-desc"[^>]*>/.match(s[:text])
+    content ||= dm ? dm.post_match.ircify_html : '(no description found)'
     return {:title => s[:text].ircify_html_title, :content => content}
   end
 
@@ -61,11 +66,11 @@ class YouTubePlugin < Plugin
       :title =>  (e.elements["media:group/media:title"].text rescue nil),
       :desc =>   (e.elements["media:group/media:description"].text rescue nil),
       :cat => (e.elements["media:group/media:category"].text rescue nil),
-      :seconds => (e.elements["media:group/yt:duration/@seconds"].value.to_i rescue nil),
-      :url => (e.elements["media:group/media:player/@url"].value rescue nil),
-      :rating => (("%s/%s" % [e.elements["gd:rating/@average"].value, e.elements["gd:rating/@max"].value]) rescue nil),
-      :views => (e.elements["yt:statistics/@viewCount"].value rescue nil),
-      :faves => (e.elements["yt:statistics/@favoriteCount"].value rescue nil)
+      :seconds => (e.elements["media:group/yt:duration/"].attributes["seconds"].to_i rescue nil),
+      :url => (e.elements["media:group/media:player/"].attributes["url"] rescue nil),
+      :rating => (("%s/%s" % [e.elements["gd:rating"].attributes["average"], e.elements["gd:rating/@max"].value]) rescue nil),
+      :views => (e.elements["yt:statistics"].attributes["viewCount"] rescue nil),
+      :faves => (e.elements["yt:statistics"].attributes["favoriteCount"] rescue nil)
     }
     if vid[:desc]
       vid[:desc].gsub!(/\s+/m, " ")
@@ -76,12 +81,12 @@ class YouTubePlugin < Plugin
       vid[:duration] = _("unknown duration")
     end
     e.elements.each("media:group/media:content") { |c|
-      if url = (c.elements["@url"].value rescue nil)
-        type = c.elements["@type"].value rescue nil
-        medium = c.elements["@medium"].value rescue nil
-        expression = c.elements["@expression"].value rescue nil
-        seconds = c.elements["@duration"].value.to_i rescue nil
-        fmt = case num_fmt = (c.elements["@yt:format"].value rescue nil)
+      if url = (c.attributes["url"] rescue nil)
+        type = c.attributes["type"] rescue nil
+        medium = c.attributes["medium"] rescue nil
+        expression = c.attributes["expression"] rescue nil
+        seconds = c.attributes["duration"].to_i rescue nil
+        fmt = case num_fmt = (c.attributes["yt:format"] rescue nil)
               when "1"
                 "h263+amr"
               when "5"
@@ -115,6 +120,7 @@ class YouTubePlugin < Plugin
     vids = []
     title = nil
     begin
+debug s.inspect
       doc = REXML::Document.new(s[:text])
       title = doc.elements["feed/title"].text
       doc.elements.each("*/entry") { |e|
@@ -147,8 +153,9 @@ class YouTubePlugin < Plugin
     debug id
 
     url = YOUTUBE_VIDEO % {:id => id}
-    resp, xml = @bot.httputil.get_response(url)
-    unless Net::HTTPSuccess === resp
+    resp = @bot.httputil.get_response(url)
+    xml = resp.body
+    unless resp.kind_of? Net::HTTPSuccess
       debug("error looking for movie %{id} on youtube: %{e}" % {:id => id, :e => xml})
       return nil
     end
@@ -197,8 +204,9 @@ class YouTubePlugin < Plugin
     what = params[:words].to_s
     searchfor = CGI.escape what
     url = YOUTUBE_SEARCH % {:words => searchfor}
-    resp, xml = @bot.httputil.get_response(url)
-    unless Net::HTTPSuccess === resp
+    resp = @bot.httputil.get_response(url)
+    xml = resp.body
+    unless resp.kind_of? Net::HTTPSuccess
       m.reply(_("error looking for %{what} on youtube: %{e}") % {:what => what, :e => xml})
       return
     end
@@ -234,3 +242,4 @@ plugin = YouTubePlugin.new
 
 plugin.map "youtube info :movie", :action => 'info', :threaded => true
 plugin.map "youtube [search] *words", :action => 'search', :threaded => true
+