aboutsummaryrefslogtreecommitdiffstats
path: root/shoutboxapicommunicator.py
diff options
context:
space:
mode:
authorjantuomi <jans.tuomi@gmail.com>2016-07-18 09:22:59 +0300
committerjantuomi <jans.tuomi@gmail.com>2016-07-18 09:22:59 +0300
commitaa526d6d1b34113c2fdc6a97d6f46bc41b08713b (patch)
treea1fa6113efd419887f22d2bed76c5e526d772651 /shoutboxapicommunicator.py
parent144542ddf58f114f0c8eaef5d2209705f493f04b (diff)
Create shoutbox API communicator class
Diffstat (limited to 'shoutboxapicommunicator.py')
-rw-r--r--shoutboxapicommunicator.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/shoutboxapicommunicator.py b/shoutboxapicommunicator.py
new file mode 100644
index 0000000..57f6bc7
--- /dev/null
+++ b/shoutboxapicommunicator.py
@@ -0,0 +1,31 @@
+import json
+
+import requests
+import logging
+
+
+class Communicator(object):
+
+ def fetch(self, url):
+ try:
+ r = requests.get(url)
+ logging.info("Response from API OK.")
+
+ except:
+ logging.warning("Failed to get response from API!")
+ return
+
+ content = json.loads(r.text)
+ logging.info("Messages:")
+ for msg in content:
+ logging.info("{}: {}".format(msg["user"], msg["text"]))
+
+ return content
+
+ def send(self, url, text, user):
+ data = {"text": text, "user": user}
+ try:
+ requests.post(url, data)
+ logging.info("Sent message from {} to shoutbox.".format(user))
+ except:
+ logging.warning("Failed to send message from {} to shoutbox API!".format(user)) \ No newline at end of file