Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import datetime
- class LastMessage:
- def __init__(self, json_filename: str):
- self.filename = json_filename
- def load_from_json(self):
- with open(self.filename, "r") as json_file:
- last_messages_dict = json.loads(json_file.read())
- if not last_messages_dict:
- self.last_messages_dict = {}
- else:
- self.last_messages_dict = last_messages_dict
- def write_to_json(self) -> None:
- with open(self.filename, "w") as json_file:
- json_file.write(json.dumps(self.last_messages_dict))
- def read_last_message(self, chat_id: int) -> int:
- return self.last_messages_dict[chat_id]
- def write_last_message(self, chat_id: int, timestamp: int) -> None:
- self.load_from_json()
- self.last_messages_dict[chat_id] = timestamp
- self.write_to_json()
- def calculate_time(self, timestamp: int) -> datetime.datetime:
- return datetime.datetime.fromtimestamp((datetime.datetime.now().timestamp() - timestamp))
- def get_estimated_time(self, chat_id: int):
- ts = self.read_last_message(chat_id)
- estimated = 5 - self.calculate_time(ts).minute
- return estimated
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement