aboutsummaryrefslogtreecommitdiffstats
path: root/shoutboxapicommunicator.py
blob: 57f6bc74c1028aa306551854c01423137bb29989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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))