From aa526d6d1b34113c2fdc6a97d6f46bc41b08713b Mon Sep 17 00:00:00 2001 From: jantuomi Date: Mon, 18 Jul 2016 09:22:59 +0300 Subject: Create shoutbox API communicator class --- shoutboxapicommunicator.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 shoutboxapicommunicator.py (limited to 'shoutboxapicommunicator.py') 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 -- cgit v1.3