X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fyoutube.rb;h=40922b7d39f5ddb3ec3d83cefd5bbadff6e33cf7;hb=16336b4a240a4265d1f2df1e30d7b68d3a924287;hp=1faa7c35ed01b2e5a71cb13afd8bd4d7d2d693f7;hpb=00e94aa9b086bdbc021095bff2d53ba2617b03e9;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/youtube.rb b/data/rbot/plugins/youtube.rb index 1faa7c35..40922b7d 100644 --- a/data/rbot/plugins/youtube.rb +++ b/data/rbot/plugins/youtube.rb @@ -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? '
' + if s[:text].include? '
' 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 ||= /
]*>/.match(s[:text]).post_match.ircify_html + dm = /]*>/.match(s[:text]) + content ||= dm ? dm.post_match.ircify_html : '(no description found)' return {:title => s[:text].ircify_html_title, :content => content} end @@ -68,15 +72,7 @@ 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 @@ -85,7 +81,7 @@ class YouTubePlugin < Plugin type = c.elements["@type"].value rescue nil medium = c.elements["@medium"].value rescue nil expression = c.elements["@expression"].value rescue nil - duration = c.elements["@duration"].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" @@ -101,10 +97,15 @@ class YouTubePlugin < Plugin vid[:formats] << { :url => url, :type => type, :medium => medium, :expression => expression, - :duration => duration, + :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 @@ -179,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