formater.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import time, math
  2. def time_formatter(seconds):
  3. minutes, seconds = divmod(seconds, 60)
  4. hours, minutes = divmod(minutes, 60)
  5. days, hours = divmod(hours, 24)
  6. tmp = (
  7. ((str(days) + " day(s), ") if days else "")
  8. + ((str(hours) + " hour(s), ") if hours else "")
  9. + ((str(minutes) + " minute(s), ") if minutes else "")
  10. + ((str(seconds) + " second(s), ") if seconds else "")
  11. )
  12. return tmp[:-2]
  13. def time_parser(start, end):
  14. time_end = end - start
  15. month = time_end // 2678400
  16. days = time_end // 86400
  17. hours = time_end // 3600 % 24
  18. minutes = time_end // 60 % 60
  19. seconds = time_end % 60
  20. times = ""
  21. if month:
  22. times += "{} month, ".format(month)
  23. if days:
  24. times += "{} days, ".format(days)
  25. if hours:
  26. times += "{} hours, ".format(hours)
  27. if minutes:
  28. times += "{} minutes, ".format(minutes)
  29. if seconds:
  30. times += "{} seconds".format(seconds)
  31. if times == "":
  32. times = "{} miliseconds".format(time_end)
  33. return times
  34. def time_parser_int(time_end):
  35. month = time_end // 2678400
  36. days = time_end // 86400
  37. hours = time_end // 3600 % 24
  38. minutes = time_end // 60 % 60
  39. seconds = time_end % 60
  40. times = ""
  41. if month:
  42. times += "{} month, ".format(month)
  43. if days:
  44. times += "{} days, ".format(days)
  45. if hours:
  46. times += "{} hours, ".format(hours)
  47. if minutes:
  48. times += "{} minutes, ".format(minutes)
  49. if seconds:
  50. times += "{} seconds".format(seconds)
  51. if times == "":
  52. times = "{} miliseconds".format(time_end)
  53. return times[:-2] if times[-2:] == ", " else times
  54. def get_readable_time(seconds: int) -> str:
  55. count = 0
  56. ping_time = ""
  57. time_list = []
  58. time_suffix_list = ["s", "m", "h", "days"]
  59. while count < 4:
  60. count += 1
  61. remainder, result = divmod(seconds, 60) if count < 3 else divmod(seconds, 24)
  62. if seconds == 0 and remainder == 0:
  63. break
  64. time_list.append(int(result))
  65. seconds = int(remainder)
  66. for x in range(len(time_list)):
  67. time_list[x] = str(time_list[x]) + time_suffix_list[x]
  68. if len(time_list) == 4:
  69. ping_time += time_list.pop() + ", "
  70. time_list.reverse()
  71. ping_time += ":".join(time_list)
  72. return ping_time
  73. def convert_size(size_bytes):
  74. if size_bytes == 0:
  75. return "0B"
  76. size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
  77. i = int(math.floor(math.log(size_bytes, 1024)))
  78. p = math.pow(1024, i)
  79. s = round(size_bytes / p, 2)
  80. return "%s %s" % (s, size_name[i])
  81. def extract_time(time_val):
  82. if not any(time_val.endswith(unit) for unit in ("m", "h", "d")):
  83. return False
  84. unit = time_val[-1]
  85. time_num = time_val[:-1] # type: str
  86. if not time_num.isdigit():
  87. return False
  88. if unit == "d":
  89. bantime = int(time.time() + int(time_num) * 24 * 60 * 60)
  90. elif unit == "h":
  91. bantime = int(time.time() + int(time_num) * 60 * 60)
  92. elif unit == "m":
  93. bantime = int(time.time() + int(time_num) * 60)
  94. else:
  95. return False
  96. return bantime
  97. def make_time(time_val):
  98. if int(time_val) == 0:
  99. return "0"
  100. if int(time_val) <= 3600:
  101. bantime = str(int(time_val / 60)) + "m"
  102. elif int(time_val) >= 3600 and time_val <= 86400:
  103. bantime = str(int(time_val / 60 / 60)) + "h"
  104. elif int(time_val) >= 86400:
  105. bantime = str(int(time_val / 24 / 60 / 60)) + "d"
  106. return bantime
  107. def extract_time_set(time_val):
  108. if not any(time_val.endswith(unit) for unit in ("m", "h", "d")):
  109. return False
  110. unit = time_val[-1]
  111. time_num = time_val[:-1] # type: str
  112. if not time_num.isdigit():
  113. return False
  114. if unit == "d":
  115. bantime = int(int(time_num) * 24 * 60 * 60)
  116. elif unit == "h":
  117. bantime = int(int(time_num) * 60 * 60)
  118. elif unit == "m":
  119. bantime = int(int(time_num) * 60)
  120. else:
  121. return False
  122. return bantime
  123. def escape_invalid_curly_brackets(text, valids): # sourcery skip
  124. new_text = ""
  125. idx = 0
  126. while idx < len(text):
  127. if text[idx] == "{":
  128. if idx + 1 < len(text) and text[idx + 1] == "{":
  129. idx += 2
  130. new_text += "{{{{"
  131. continue
  132. else:
  133. success = False
  134. for v in valids:
  135. if text[idx:].startswith("{" + v + "}"):
  136. success = True
  137. break
  138. if success:
  139. new_text += text[idx : idx + len(v) + 2]
  140. idx += len(v) + 2
  141. continue
  142. else:
  143. new_text += "{{"
  144. elif text[idx] == "}":
  145. if idx + 1 < len(text) and text[idx + 1] == "}":
  146. idx += 2
  147. new_text += "}}}}"
  148. continue
  149. else:
  150. new_text += "}}"
  151. else:
  152. new_text += text[idx]
  153. idx += 1
  154. return new_text