split format_time out to a util module!
This commit is contained in:
parent
b65e6e5a75
commit
6decd65a19
|
@ -1,14 +1,49 @@
|
||||||
def timeformat(seconds):
|
from util import text
|
||||||
days = seconds / 86400
|
|
||||||
seconds -= 86400 * days
|
def format_time(seconds, count=3, accuracy=6, simple=False):
|
||||||
hours = seconds / 3600
|
if simple:
|
||||||
seconds -= 3600 * hours
|
periods = [
|
||||||
minutes = seconds / 60
|
('c', 60 * 60 * 24 * 365 * 100),
|
||||||
seconds -= 60 * minutes
|
('de', 60 * 60 * 24 * 365 * 10),
|
||||||
if days != 0:
|
('y', 60 * 60 * 24 * 365),
|
||||||
return "%sd %sh %sm %ss" % (days, hours, minutes, seconds)
|
('m', 60 * 60 * 24 * 30),
|
||||||
elif hours == 0 and minutes != 0:
|
('d', 60 * 60 * 24),
|
||||||
return "%sm %ss" % (minutes, seconds)
|
('h', 60 * 60),
|
||||||
elif hours == 0 and minutes == 0:
|
('m', 60),
|
||||||
return "%ss" % seconds
|
('s', 1)
|
||||||
return "%sh %sm %ss" % (hours, minutes, seconds)
|
]
|
||||||
|
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 re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from util import hook, http, text
|
from util import hook, http, timeformat
|
||||||
|
|
||||||
|
|
||||||
youtube_re = (r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)'
|
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:])
|
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):
|
def get_video_description(video_id):
|
||||||
|
@ -80,7 +34,7 @@ def get_video_description(video_id):
|
||||||
return out
|
return out
|
||||||
|
|
||||||
length = data['duration']
|
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:
|
if 'ratingCount' in data:
|
||||||
# format
|
# format
|
||||||
|
@ -161,8 +115,8 @@ def youtime(inp):
|
||||||
views = data['viewCount']
|
views = data['viewCount']
|
||||||
total = int(length * views)
|
total = int(length * views)
|
||||||
|
|
||||||
length_text = format_time(length, simple=True)
|
length_text = timeformat.format_time(length, simple=True)
|
||||||
total_text = format_time(total, accuracy=8)
|
total_text = timeformat.format_time(total, accuracy=8)
|
||||||
|
|
||||||
return u'The video \x02{}\x02 has a length of {} and has been viewed {:,} times for ' \
|
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, \
|
'a total run time of {}!'.format(data['title'], length_text, views, \
|
||||||
|
|
Reference in a new issue