Merge branch 'develop' into refresh

Conflicts:
	plugins/util/timeformat.py
This commit is contained in:
Luke Rogers 2013-12-12 12:41:02 +13:00
commit df894932d2
4 changed files with 77 additions and 53 deletions

View file

@ -107,9 +107,9 @@ def format_output(data):
## HOOK FUNCTIONS
@hook.command('plugin')
@hook.command
def bukkitplugin(inp, reply=None, message=None):
"""plugin <slug/name> - Look up a plugin on dev.bukkit.org"""
@hook.commands
def bukget(inp, reply=None, message=None):
"""bukget <slug/name> - Look up a plugin on dev.bukkit.org"""
# get the plugin slug using search
try:
slug = plugin_search(inp)

View file

@ -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)

View file

@ -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, \

66
util/timeformat.py Normal file
View file

@ -0,0 +1,66 @@
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),
('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")