aboutsummaryrefslogtreecommitdiffstats
path: root/shoutboxapicommunicator.py
blob: 28a5b7497ee15ad747027aab3a35dd5384fb11b4 (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
32
33
import json

import requests
import logging


class Communicator(object):

    @staticmethod
    def fetch(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

    @staticmethod
    def send(url, data):
        try:
            requests.post(url, data)
            user = json.loads(data)["user"]
            logging.info("Sent message from {} to shoutbox.".format(user))
        except:
            logging.warning("Failed to send message to shoutbox API!")