X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fyoutube.rb;h=01f494d1b863e367d3fe99cd381633051086f7a7;hb=052217de30c59206d7025b582d4604557a747470;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..01f494d1 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? ']*>/.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 @@ -58,35 +62,27 @@ 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, " ") 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 - duration = c.elements["@duration"].value 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" @@ -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