]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/youtube.rb
script plugin: store channels as strings
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / youtube.rb
index 8868b4b9f831a2aa21000e9674fa260fdfc87ed0..40922b7d39f5ddb3ec3d83cefd5bbadff6e33cf7 100644 (file)
@@ -20,11 +20,14 @@ class YouTubePlugin < Plugin
   Config.register Config::IntegerValue.new('youtube.descs',
     :default => 3,
     :desc => "When set to n > 0, the bot will return the description of the first n videos found")
+  Config.register Config::BooleanValue.new('youtube.formats',
+    :default => true,
+    :desc => "Should the bot display alternative URLs (swf, rstp) for YouTube videos?")
 
   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? '<div id="watch-vid-title">'
       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
@@ -37,10 +40,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
 
@@ -53,6 +57,7 @@ class YouTubePlugin < Plugin
     #   :title => mg["media:title"].text
     # fails because "media:title" is not an Integer. Bah
     vid = {
+      :formats => [],
       :author => (e.elements["author/name"].text rescue nil),
       :title =>  (e.elements["media:group/media:title"].text rescue nil),
       :desc =>   (e.elements["media:group/media:description"].text rescue nil),
@@ -67,18 +72,42 @@ class YouTubePlugin < Plugin
       vid[:desc].gsub!(/\s+/m, " ")
     end
     if secs = vid[:seconds]
-      mins, secs = secs.divmod 60
-      hours, mins = mins.divmod 60
-      if hours > 0
-        vid[:duration] = "%s:%s:%s" % [hours, mins, secs]
-      elsif mins > 0
-        vid[:duration] = "%s'%s\"" % [mins, secs]
-      else
-        vid[:duration] = "%ss" % [secs]
-      end
+      vid[:duration] = Utils.secs_to_short(secs)
     else
       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)
+              when "1"
+                "h263+amr"
+              when "5"
+                "swf"
+              when "6"
+                "mp4+aac"
+              when nil
+                nil
+              else
+                num_fmt
+              end
+        vid[:formats] << {
+          :url => url, :type => type,
+          :medium => medium, :expression => expression,
+          :seconds => seconds,
+          :numeric_format => num_fmt,
+          :format => fmt
+        }.delete_if { |k, v| v.nil? }
+        if seconds
+          vid[:formats].last[:duration] = Utils.secs_to_short(seconds)
+        else
+          vid[:formats].last[:duration] = _("unknown duration")
+        end
+      end
+    }
     debug vid
     return vid
   end
@@ -151,8 +180,15 @@ class YouTubePlugin < Plugin
 
     vid = @bot.filter(:"youtube.video", :url => movie, :youtube_video_id => id)
     if vid
-      m.reply(_("%{bold}%{title}%{bold} [%{cat}] %{rating} @ %{url} by %{author} (%{duration}). %{views} views, faved %{faves} times. %{desc}") %
-              {:bold => Bold}.merge(vid))
+      str = _("%{bold}%{title}%{bold} [%{cat}] %{rating} @ %{url} by %{author} (%{duration}). %{views} views, faved %{faves} times. %{desc}") %
+        {:bold => Bold}.merge(vid)
+      if @bot.config['youtube.formats'] and not vid[:formats].empty?
+        str << _("\n -- also available at: ")
+        str << vid[:formats].inject([]) { |list, fmt|
+          list << ("%{url} %{type} %{format} (%{duration} %{expression} %{medium})" % fmt)
+        }.join(', ')
+      end
+      m.reply str
     else
       m.reply(_("couldn't retrieve video info") % {:id => id})
     end