aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--project/botmanager.py35
-rw-r--r--project/shoutboxapicommunicator.py8
-rwxr-xr-xradiodiodibot2
3 files changed, 10 insertions, 35 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
diff --git a/radiodiodibot b/radiodiodibot
index 2c1507d..2cfc3dd 100755
--- a/radiodiodibot
+++ b/radiodiodibot
@@ -65,7 +65,7 @@ def main():
telegram_bot_token = config["GENERAL"]["TelegramBotToken"]
shoutbox_api_url = config["GENERAL"]["ShoutboxApiUrl"]
telegram_chat_id = config["GENERAL"]["TelegramChatID"]
- api_call_interval = config["GENERAL"]["ApiCallInterval"]
+ api_call_interval = int(config["GENERAL"]["ApiCallInterval"])
# If the user has specified the parameters as command line
# arguments, use them to override the config values