summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/rbot/plugins/youtube.rb20
-rw-r--r--lib/rbot/core/utils/utils.rb16
2 files changed, 18 insertions, 18 deletions
diff --git a/data/rbot/plugins/youtube.rb b/data/rbot/plugins/youtube.rb
index 947156bc..5f1aa822 100644
--- a/data/rbot/plugins/youtube.rb
+++ b/data/rbot/plugins/youtube.rb
@@ -71,15 +71,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
@@ -109,15 +101,7 @@ class YouTubePlugin < Plugin
:format => fmt
}.delete_if { |k, v| v.nil? }
if seconds
- mins, secs = seconds.divmod 60
- hours, mins = mins.divmod 60
- if hours > 0
- vid[:formats].last[:duration] = "%s:%s:%s" % [hours, mins, secs]
- elsif mins > 0
- vid[:formats].last[:duration] = "%s'%s\"" % [mins, secs]
- else
- vid[:formats].last[:duration] = "%ss" % [secs]
- end
+ vid[:formats].last[:duration] = Utils.secs_to_short(seconds)
else
vid[:formats].last[:duration] = _("unknown duration")
end
diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb
index 529c37bd..7b1ff7f1 100644
--- a/lib/rbot/core/utils/utils.rb
+++ b/lib/rbot/core/utils/utils.rb
@@ -223,6 +223,22 @@ module ::Irc
end
end
+ # Turn a number of seconds into a hours:minutes:seconds e.g.
+ # 3:18:10 or 5'12" or 7s
+ #
+ def Utils.secs_to_short(seconds)
+ secs = seconds.to_i # make sure it's an integer
+ mins, secs = secs.divmod 60
+ hours, mins = mins.divmod 60
+ if hours > 0
+ return ("%s:%s:%s" % [hours, mins, secs])
+ elsif mins > 0
+ return ("%s'%s\"" % [mins, secs])
+ else
+ return ("%ss" % [secs])
+ end
+ end
+
# Execute an external program, returning a String obtained by redirecting
# the program's standards errors and output