util.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import re
  2. class bcolors:
  3. """
  4. Allows for prettyprinting to the console for debugging.
  5. """
  6. HEADER = '\033[95m'
  7. OKBLUE = '\033[94m'
  8. OKGREEN = '\033[92m'
  9. WARNING = '\033[93m'
  10. CYAN = '\033[36m'
  11. GREEN = '\033[32m'
  12. YELLOW = '\033[33m'
  13. FAIL = '\033[91m'
  14. ENDC = '\033[0m'
  15. def strip_nick(nick):
  16. """
  17. Clean up nicks of their op levels (&Schooly_D, ~BoneKin, etc)
  18. """
  19. nick = re.sub('[@~+]', '', nick)
  20. return nick
  21. def __prettyDate(time):
  22. """
  23. Similar to Rails's nice time since thing.
  24. """
  25. now = datetime.now()
  26. if type(time) is int:
  27. diff = now - datetime.fromtimestamp(time)
  28. elif isinstance(time,datetime):
  29. diff = now - time
  30. elif not time:
  31. diff = now - now
  32. second_diff = diff.seconds
  33. day_diff = diff.days
  34. if day_diff < 0:
  35. return ''
  36. if day_diff == 0:
  37. if second_diff < 10:
  38. return "just now"
  39. if second_diff < 60:
  40. return str(second_diff) + " seconds ago"
  41. if second_diff < 120:
  42. return "a minute ago"
  43. if second_diff < 3600:
  44. return str( second_diff / 60 ) + " minutes ago"
  45. if second_diff < 7200:
  46. return "an hour ago"
  47. if second_diff < 86400:
  48. return str( second_diff / 3600 ) + " hours ago"
  49. if day_diff == 1:
  50. return "Yesterday"
  51. if day_diff < 7:
  52. return str(day_diff) + " days ago"
  53. if day_diff < 31:
  54. return str(day_diff/7) + " weeks ago"
  55. if day_diff < 365:
  56. return str(day_diff/30) + " months ago"
  57. return str(day_diff/365) + " years ago"