aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2017-04-17 16:19:06 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2017-04-17 16:19:06 +0300
commitf536354dad36b61992feef9f8de565510ee4e5a3 (patch)
tree2223cd904a0f88a5fde4c3fc161da120b99d539a
parent6c609d9178a678e2b7c6fb6a17539fc8fe6500a0 (diff)
Forward only new messagesmaster
-rw-r--r--.gitignore2
-rw-r--r--project/botmanager.py2
-rw-r--r--project/shoutboxapicommunicator.py30
-rwxr-xr-xradiodiodibot2
4 files changed, 30 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index 542476a..e0e209f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -93,3 +93,5 @@ ENV/
.gitignore
\.idea/
+
+bot\.largest_id
diff --git a/project/botmanager.py b/project/botmanager.py
index 4984a97..4f815d5 100644
--- a/project/botmanager.py
+++ b/project/botmanager.py
@@ -45,6 +45,8 @@ class BotManager(object):
def start(self):
"""Try fetching bot information from Telegram to check connection"""
+ ShoutboxCommunicator.load_largest_id()
+
TelegramCommunicator.start_listening(self.handle)
while True:
diff --git a/project/shoutboxapicommunicator.py b/project/shoutboxapicommunicator.py
index 6655fd3..218ed1c 100644
--- a/project/shoutboxapicommunicator.py
+++ b/project/shoutboxapicommunicator.py
@@ -6,6 +6,7 @@ import requests
from project.basecommunicator import BaseCommunicator
+logging.getLogger("requests").setLevel(logging.WARNING)
class ShoutboxCommunicator(BaseCommunicator):
"""Class for communication with the shoutbox API"""
@@ -14,12 +15,13 @@ class ShoutboxCommunicator(BaseCommunicator):
interval = 30
message_limit_seconds = 50
largest_id = 0
+ largest_id_file = "bot.largest_id"
token = ""
@staticmethod
def get_url():
id_hex = "{0:0{1}x}".format(ShoutboxCommunicator.largest_id, 24)
- return "{}/api/last?&telegram=false&id={}".format(ShoutboxCommunicator.url, ShoutboxCommunicator.message_limit_seconds, id_hex)
+ return "{}/api/last?&telegram=false&id={}".format(ShoutboxCommunicator.url, id_hex)
@staticmethod
def post_url():
@@ -30,18 +32,16 @@ class ShoutboxCommunicator(BaseCommunicator):
"""Get and return a list of new messages from the API"""
try:
r = requests.get(ShoutboxCommunicator.get_url())
- #logging.info("Response from API OK.")
except:
logging.warning("Failed to get response from API! ({})".format(ShoutboxCommunicator.get_url()))
return
try:
- logging.info("url: {}".format(ShoutboxCommunicator.get_url()))
content = json.loads(r.text)
- logging.info(content)
except:
logging.warning("Could not parse JSON from request!")
+ logging.warning("Request data:\n{}".format(r))
return
if len(content) > 0:
@@ -51,8 +51,8 @@ class ShoutboxCommunicator(BaseCommunicator):
logging.info("{}: {}, id: {}".format(msg["user"], msg["text"], msg["id"]))
id_dec = int(msg["id"], 16)
- if id_dec > largest_id:
- largest_id = id_dec
+ if id_dec > ShoutboxCommunicator.largest_id:
+ ShoutboxCommunicator.largest_id = id_dec
return content
@@ -76,3 +76,21 @@ class ShoutboxCommunicator(BaseCommunicator):
logging.info("Sent message from {} to shoutbox, with url:\n{}".format(user, url))
except:
logging.warning("Failed to send message to shoutbox API!")
+
+ @staticmethod
+ def load_largest_id():
+ try:
+ with open(ShoutboxCommunicator.largest_id_file, 'r') as id_file:
+ ShoutboxCommunicator.largest_id = int(id_file.read())
+ logging.info("Loaded minimum shoutbox message id: {}".format(ShoutboxCommunicator.largest_id))
+ except:
+ logging.warning("Failed to load minimum shoutbox message id! Defaulting to 0.")
+
+ @staticmethod
+ def save_largest_id():
+ try:
+ with open(ShoutboxCommunicator.largest_id_file, 'w') as id_file:
+ id_file.write("{}".format(ShoutboxCommunicator.largest_id))
+ logging.info("Wrote minimum shoutbox id '{}' to file.".format(ShoutboxCommunicator.largest_id))
+ except:
+ logging.warning("Failed to write minimum shoutbox id to file!")
diff --git a/radiodiodibot b/radiodiodibot
index c52d777..564ac3e 100755
--- a/radiodiodibot
+++ b/radiodiodibot
@@ -9,6 +9,7 @@ import os
from project import botmanager
from project import uptimeservice
+from project import shoutboxapicommunicator
class BotLauncher(object):
CONFIG_FILE = "bot.config"
@@ -18,6 +19,7 @@ class BotLauncher(object):
"""Exit gracefully when receiving an interrupt signal"""
logging.info("Stopping uptime services (if any)...")
uptimeservice.UptimeService.stop_services()
+ shoutboxapicommunicator.ShoutboxCommunicator.save_largest_id()
print("Exiting radiodiodibot...")
sys.exit(0)