split format_time out to a util module!
This commit is contained in:
parent
b65e6e5a75
commit
6decd65a19
|
@ -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")
|
|
@ -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, \
|
||||
|
|
Reference in New Issue