diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-07-20 15:05:09 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-07-20 15:05:09 +0300 |
| commit | 2cb5a755d3d384d47e7c8c9690397f7f886a5915 (patch) | |
| tree | 9061fcc3f9b2b062d53a9536b35ffee4dc596b9b /project | |
| parent | 0659ea69359564ec8d230fe67ffbd61a14358fe4 (diff) | |
Replace last message dict with storing the last message id, fix type conversion problem
Diffstat (limited to 'project')
| -rw-r--r-- | project/botmanager.py | 35 | ||||
| -rw-r--r-- | project/shoutboxapicommunicator.py | 8 |
2 files changed, 9 insertions, 34 deletions
diff --git a/project/botmanager.py b/project/botmanager.py index 7fa15a1..448dbb8 100644 --- a/project/botmanager.py +++ b/project/botmanager.py @@ -6,7 +6,7 @@ import sys import time import traceback import telepot -from dateutil.parser import parse + from project.jsonfactory import JSONFactory from project.shoutboxapicommunicator import ShoutboxCommunicator from project.telegramapicommunicator import TelegramCommunicator @@ -18,7 +18,7 @@ class BotManager(object): # Make a dict of ( message id : timestamp ) pairs to # keep track of sent messages. # Messages older than 2 * update interval will be forgotten. - last_message_timestamps = {} + last_message_id = "" def __init__(self, token): """ @@ -54,42 +54,15 @@ class BotManager(object): # Wait for the update interval time.sleep(ShoutboxCommunicator.interval) - # Remove obsolete messages from the dict to prevent it - # from bloating - self.clean_up_message_dict() - - def clean_up_message_dict(self): - """Purge all obsolete messages from the message dict""" - new_message_dict = {} - for msg_id in self.last_message_timestamps: - - # if the message is older than 2 * call interval, - # pop it from the buffer because there is no way - # it can be a duplicate anymore - str_stamp = self.last_message_timestamps[msg_id] - try: - timestamp = round(float(str_stamp)) - except ValueError: - timestamp_st = parse(timestamp) - timestamp = int(time.mktime(timestamp_st.timetuple())) - - print("Timestamp: {}, now: {}".format(round(time.time()), timestamp)) - - if not abs(round(time.time()) - timestamp) < 2 * ShoutboxCommunicator.interval: - new_message_dict[msg_id] = self.last_message_timestamps[msg_id] - else: - logging.info("Popped message from buffer.") - self.last_message_timestamps = new_message_dict - def forward_to_telegram(self, messages): """Send all messages in the messages list to the Telegram chat""" try: for message in messages: # Do not send messages that have already been sent # This check is in place because of possible artifacts in API calls - if message["id"] not in self.last_message_timestamps: + if message["id"] != self.last_message_id: TelegramCommunicator.send(message) - self.last_message_timestamps[message["id"]] = message["timestamp"] + self.last_message_id = message["id"] except: logging.warning("Failed to send message to Telegram!") diff --git a/project/shoutboxapicommunicator.py b/project/shoutboxapicommunicator.py index 7194c8f..4c7e0c0 100644 --- a/project/shoutboxapicommunicator.py +++ b/project/shoutboxapicommunicator.py @@ -24,9 +24,11 @@ class ShoutboxCommunicator(BaseCommunicator): return content = json.loads(r.text) - logging.info("Messages:") - for msg in content: - logging.info("{}: {}".format(msg["user"], msg["text"])) + + if len(content) > 0: + logging.info("Messages from shoutbox:") + for msg in content: + logging.info("{}: {}".format(msg["user"], msg["text"])) return content |
