Rough mockup of subreddit page.

This commit is contained in:
Michael Lazar
2015-01-20 22:53:09 -08:00
parent 06633e27ee
commit 0459785358
3 changed files with 138 additions and 15 deletions

View File

@@ -52,28 +52,28 @@ def curses_session():
curses.nocbreak()
curses.endwin()
def humanize_timestamp(utc_timestamp):
def humanize_timestamp(utc_timestamp, long=True):
"""
Convert a utc timestamp into a human readable time relative to now.
"""
timedelta = datetime.utcnow() - datetime.utcfromtimestamp(utc_timestamp)
seconds = int(timedelta.total_seconds())
if seconds < 60:
return 'moments ago'
return 'moments ago' if long else '0min'
minutes = seconds / 60
if minutes < 60:
return '{} minutes ago'.format(minutes)
return '%d' % minutes + (' minutes ago' if long else 'min')
hours = minutes / 60
if hours < 24:
return '{} hours ago'.format(hours)
return '%d' % hours + (' hours ago' if long else 'hr')
days = hours / 24
if days < 30:
return '{} days ago'.format(days)
return '%d' % days + (' days ago' if long else 'day')
months = days / 30.4
if months < 12:
return '{} months ago'.format(months)
return '%d' % months + (' months ago' if long else 'month')
years = months / 12
return '{} years ago'.format(years)
return '%d' % years + (' years ago' if long else 'yr')
def flatten_tree(tree):
@@ -95,4 +95,9 @@ def flatten_tree(tree):
n.nested_level = item.nested_level + 1
stack[0:0] = nested
retval.append(item)
return retval
return retval
def clean(unicode_string):
"Convert unicode string into ascii-safe characters."
return unicode_string.encode('ascii', 'replace').replace('\\', '')