From 6decd65a19b24544939441295e113a5c2019cf0e Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Wed, 11 Dec 2013 17:17:56 +1300 Subject: [PATCH 1/3] split format_time out to a util module! --- plugins/util/timeformat.py | 63 +++++++++++++++++++++++++++++--------- plugins/youtube.py | 54 +++----------------------------- 2 files changed, 53 insertions(+), 64 deletions(-) diff --git a/plugins/util/timeformat.py b/plugins/util/timeformat.py index 185fd36..4661871 100644 --- a/plugins/util/timeformat.py +++ b/plugins/util/timeformat.py @@ -1,14 +1,49 @@ -def timeformat(seconds): - days = seconds / 86400 - seconds -= 86400 * days - hours = seconds / 3600 - seconds -= 3600 * hours - minutes = seconds / 60 - seconds -= 60 * minutes - if days != 0: - return "%sd %sh %sm %ss" % (days, hours, minutes, seconds) - elif hours == 0 and minutes != 0: - return "%sm %ss" % (minutes, seconds) - elif hours == 0 and minutes == 0: - return "%ss" % seconds - return "%sh %sm %ss" % (hours, minutes, seconds) +from util import text + +def format_time(seconds, count=3, accuracy=6, simple=False): + if simple: + periods = [ + ('c', 60 * 60 * 24 * 365 * 100), + ('de', 60 * 60 * 24 * 365 * 10), + ('y', 60 * 60 * 24 * 365), + ('m', 60 * 60 * 24 * 30), + ('d', 60 * 60 * 24), + ('h', 60 * 60), + ('m', 60), + ('s', 1) + ] + else: + periods = [ + (('century', 'centuries'), 60 * 60 * 24 * 365 * 100), + (('decade', 'decades'), 60 * 60 * 24 * 365 * 10), + (('year', 'years'), 60 * 60 * 24 * 365), + (('month', 'months'), 60 * 60 * 24 * 30), + (('day', 'days'), 60 * 60 * 24), + (('hour', 'hours'), 60 * 60), + (('minute', 'minutes'), 60), + (('second', 'seconds'), 1) + ] + + periods = periods[-accuracy:] + + strings = [] + i = 0 + for period_name, period_seconds in periods: + if i < count: + if seconds > period_seconds: + period_value, seconds = divmod(seconds, period_seconds) + i += 1 + if simple: + strings.append("{}{}".format(period_value, period_name)) + else: + if period_value == 1: + strings.append("{} {}".format(period_value, period_name[0])) + else: + strings.append("{} {}".format(period_value, period_name[1])) + else: + break + + if simple: + return " ".join(strings) + else: + return text.get_text_list(strings, "and") \ No newline at end of file diff --git a/plugins/youtube.py b/plugins/youtube.py index 664c3b6..91e0ea7 100644 --- a/plugins/youtube.py +++ b/plugins/youtube.py @@ -1,7 +1,7 @@ import re import time -from util import hook, http, text +from util import hook, http, timeformat youtube_re = (r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)' @@ -17,53 +17,7 @@ def plural(num=0, text=''): return "{:,} {}{}".format(num, text, "s"[num==1:]) -def format_time(seconds, count=3, accuracy=6, simple=False): - if simple: - periods = [ - ('c', 60 * 60 * 24 * 365 * 100), - ('de', 60 * 60 * 24 * 365 * 10), - ('y', 60 * 60 * 24 * 365), - ('m', 60 * 60 * 24 * 30), - ('d', 60 * 60 * 24), - ('h', 60 * 60), - ('m', 60), - ('s', 1) - ] - else: - periods = [ - (('century', 'centuries'), 60 * 60 * 24 * 365 * 100), - (('decade', 'decades'), 60 * 60 * 24 * 365 * 10), - (('year', 'years'), 60 * 60 * 24 * 365), - (('month', 'months'), 60 * 60 * 24 * 30), - (('day', 'days'), 60 * 60 * 24), - (('hour', 'hours'), 60 * 60), - (('minute', 'minutes'), 60), - (('second', 'seconds'), 1) - ] - periods = periods[-accuracy:] - - strings = [] - i = 0 - for period_name, period_seconds in periods: - if i < count: - if seconds > period_seconds: - period_value, seconds = divmod(seconds, period_seconds) - i += 1 - if simple: - strings.append("{}{}".format(period_value, period_name)) - else: - if period_value == 1: - strings.append("{} {}".format(period_value, period_name[0])) - else: - strings.append("{} {}".format(period_value, period_name[1])) - else: - break - - if simple: - return " ".join(strings) - else: - return text.get_text_list(strings, "and") def get_video_description(video_id): @@ -80,7 +34,7 @@ def get_video_description(video_id): return out length = data['duration'] - out += u' - length \x02{}\x02'.format(format_time(length, simple=True)) + out += u' - length \x02{}\x02'.format(timeformat.format_time(length, simple=True)) if 'ratingCount' in data: # format @@ -161,8 +115,8 @@ def youtime(inp): views = data['viewCount'] total = int(length * views) - length_text = format_time(length, simple=True) - total_text = format_time(total, accuracy=8) + length_text = timeformat.format_time(length, simple=True) + total_text = timeformat.format_time(total, accuracy=8) return u'The video \x02{}\x02 has a length of {} and has been viewed {:,} times for ' \ 'a total run time of {}!'.format(data['title'], length_text, views, \ From e94c607b3e5bb23cfa37a34f38a9e920ec6afe92 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Wed, 11 Dec 2013 17:27:42 +1300 Subject: [PATCH 2/3] documentation! --- plugins/util/timeformat.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plugins/util/timeformat.py b/plugins/util/timeformat.py index 4661871..ebfe333 100644 --- a/plugins/util/timeformat.py +++ b/plugins/util/timeformat.py @@ -1,6 +1,23 @@ from util import text def format_time(seconds, count=3, accuracy=6, simple=False): + """ + Takes a length of time in seconds and returns a string describing that length of time. + This function has a number of optional arguments that can be combined: + + SIMPLE: displays the time in a simple format + >>> format_time(SECONDS) + 1 hour, 2 minutes, and 34 seconds + >>> format_time(SECONDS, simple=True) + 1h 2m 34s + + COUNT: how many periods should be shown (default 3) + >>> format_time(SECONDS) + 147 years, 9 months and 8 weeks + >>> format_time(SECONDS, count=6) + 147 years, 9 months, 7 weeks, 18 hours, 12 minutes and 34 seconds + """ + if simple: periods = [ ('c', 60 * 60 * 24 * 365 * 100), From cda99f1852e6fe7d78a9b00299174618fb4c12b7 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Wed, 11 Dec 2013 17:31:57 +1300 Subject: [PATCH 3/3] tweaks --- plugins/{minecraft_bukkit.py => minecraft_bukget.py} | 6 +++--- plugins/newegg.py | 4 ++++ plugins/util/timeformat.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) rename plugins/{minecraft_bukkit.py => minecraft_bukget.py} (97%) diff --git a/plugins/minecraft_bukkit.py b/plugins/minecraft_bukget.py similarity index 97% rename from plugins/minecraft_bukkit.py rename to plugins/minecraft_bukget.py index c72b0e5..c066c74 100644 --- a/plugins/minecraft_bukkit.py +++ b/plugins/minecraft_bukget.py @@ -107,9 +107,9 @@ def format_output(data): ## HOOK FUNCTIONS @hook.command('plugin') -@hook.command -def bukkitplugin(inp, reply=None, message=None): - """plugin - Look up a plugin on dev.bukkit.org""" +@hook.commands +def bukget(inp, reply=None, message=None): + """bukget - Look up a plugin on dev.bukkit.org""" # get the plugin slug using search try: slug = plugin_search(inp) diff --git a/plugins/newegg.py b/plugins/newegg.py index bd07a47..87242dc 100644 --- a/plugins/newegg.py +++ b/plugins/newegg.py @@ -2,6 +2,8 @@ from util import hook, http, text, web import json import re +## CONSTANTS + ITEM_URL = "http://www.newegg.com/Product/Product.aspx?Item={}" API_PRODUCT = "http://www.ows.newegg.com/Products.egg/{}/ProductDetails" @@ -10,6 +12,8 @@ API_SEARCH = "http://www.ows.newegg.com/Search.egg/Advanced" NEWEGG_RE = (r"(?:(?:www.newegg.com|newegg.com)/Product/Product\.aspx\?Item=)([-_a-zA-Z0-9]+)", re.I) +## OTHER FUNCTIONS + def format_item(item, show_url=True): """ takes a newegg API item object and returns a description """ title = text.truncate_str(item["Title"], 50) diff --git a/plugins/util/timeformat.py b/plugins/util/timeformat.py index ebfe333..7ec6abf 100644 --- a/plugins/util/timeformat.py +++ b/plugins/util/timeformat.py @@ -7,7 +7,7 @@ def format_time(seconds, count=3, accuracy=6, simple=False): SIMPLE: displays the time in a simple format >>> format_time(SECONDS) - 1 hour, 2 minutes, and 34 seconds + 1 hour, 2 minutes and 34 seconds >>> format_time(SECONDS, simple=True) 1h 2m 34s