Advertisement
WupEly

Untitled

Feb 10th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import json
  2. import datetime
  3.  
  4.  
  5. class LastMessage:
  6. def __init__(self, json_filename: str):
  7. self.filename = json_filename
  8.  
  9. def load_from_json(self):
  10. with open(self.filename, "r") as json_file:
  11. last_messages_dict = json.loads(json_file.read())
  12. if not last_messages_dict:
  13. self.last_messages_dict = {}
  14. else:
  15. self.last_messages_dict = last_messages_dict
  16.  
  17. def write_to_json(self) -> None:
  18. with open(self.filename, "w") as json_file:
  19. json_file.write(json.dumps(self.last_messages_dict))
  20.  
  21. def read_last_message(self, chat_id: int) -> int:
  22. return self.last_messages_dict[chat_id]
  23.  
  24. def write_last_message(self, chat_id: int, timestamp: int) -> None:
  25. self.load_from_json()
  26. self.last_messages_dict[chat_id] = timestamp
  27. self.write_to_json()
  28.  
  29. def calculate_time(self, timestamp: int) -> datetime.datetime:
  30. return datetime.datetime.fromtimestamp((datetime.datetime.now().timestamp() - timestamp))
  31.  
  32. def get_estimated_time(self, chat_id: int):
  33. ts = self.read_last_message(chat_id)
  34. estimated = 5 - self.calculate_time(ts).minute
  35.  
  36. return estimated
  37.  
  38.  
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement