aboutsummaryrefslogtreecommitdiffstats
path: root/project
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2017-04-17 14:51:46 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2017-04-17 14:51:46 +0300
commite5c8439a8c0942b227ed3b9449365261786c5e39 (patch)
treecced442b4a153a574e6d193e80197feeee789f4a /project
parent0cb7fe6bee47abf6d6537c1236ab59069142eef9 (diff)
Fix shoutbox API calls
Diffstat (limited to 'project')
-rw-r--r--project/shoutboxapicommunicator.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/project/shoutboxapicommunicator.py b/project/shoutboxapicommunicator.py
index dfabc46..24dfa73 100644
--- a/project/shoutboxapicommunicator.py
+++ b/project/shoutboxapicommunicator.py
@@ -11,12 +11,14 @@ class ShoutboxCommunicator(BaseCommunicator):
"""Class for communication with the shoutbox API"""
url = "http://localhost:8000"
- interval = 10
+ interval = 30
+ message_limit_seconds = 50
+ largest_id = 0
token = ""
@staticmethod
def get_url():
- return "{}/api/last?seconds={}&telegram=false".format(ShoutboxCommunicator.url, ShoutboxCommunicator.interval)
+ return "{}/api/last?&telegram=false&id={}".format(ShoutboxCommunicator.url, ShoutboxCommunicator.message_limit_seconds, ShoutboxCommunicator.largest_id)
@staticmethod
def post_url():
@@ -27,14 +29,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.")
+ #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!")
return
@@ -42,7 +46,11 @@ class ShoutboxCommunicator(BaseCommunicator):
if len(content) > 0:
logging.info("Messages from shoutbox:")
for msg in content:
- logging.info("{}: {}".format(msg["user"], msg["text"]))
+ 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
return content
@@ -51,6 +59,7 @@ class ShoutboxCommunicator(BaseCommunicator):
"""Send a message to the API"""
fields = ("user", "text", "ip", "timestamp", "api_token")
post_params = "?"
+ logging.info("Sending message from shoutbox...")
for field in fields:
if field != fields[-1]:
post_params = "{}{}={}".format(post_params, field, quote(str(data[field]).encode("utf-8")))