Ejemplo n.º 1
0
def create_stream(stream_id=None):
    with db.connect() as c:
        if stream_id is None:
            return c.execute(
                """
                INSERT INTO stream (created, bots, keep_alive)
                VALUES (%s, %s, %s)
            """, unix_timestamp(), json.dumps([]), unix_timestamp())
        else:
            return c.execute(
                """
                INSERT IGNORE INTO stream (stream_id, created, bots, keep_alive)
                VALUES (%s, %s, %s, %s)
            """, stream_id, unix_timestamp(), json.dumps([]), unix_timestamp())
Ejemplo n.º 2
0
def update_message(message_id, text, metadata):
    with db.connect() as c:
        return c.execute(
            """
            UPDATE message
            SET text=%s, metadata=%s, updated=%s, pending=False
            WHERE message_id=%s
        """, text, json.dumps(metadata), unix_timestamp(), message_id)
Ejemplo n.º 3
0
def remove_bot(stream_id, bot_id):
    with db.connect() as c:
        row = c.get("SELECT bots FROM stream WHERE stream_id=%s", stream_id)
        if row:
            bots = json.loads(row.bots)
            if bot_id in bots:
                bots.remove(bot_id)
                kill_pending_messages(stream_id, bot_id)
                return c.query(
                    """
                    UPDATE stream SET bots=%s WHERE stream_id=%s
                """, json.dumps(bots), stream_id)
Ejemplo n.º 4
0
def add_bot(stream_id, bot_id):
    out = 0
    with db.connect() as c:
        row = c.get("SELECT bots FROM stream WHERE stream_id=%s", stream_id)
        if row:
            bots = set(json.loads(row.bots))
            bots.add(bot_id)
            out = c.query(
                """
                UPDATE stream SET bots=%s WHERE stream_id=%s
            """, json.dumps(list(bots)), stream_id)

    add_pending_message(stream_id, bot_id, soon=True)
    return out
Ejemplo n.º 5
0
def create_message(stream_id,
                   text,
                   bot_id=None,
                   pending=False,
                   pending_time=None,
                   metadata=None):
    now = unix_timestamp()

    with db.connect() as c:
        return c.execute(
            """
            INSERT INTO message (stream_id, bot_id, created, updated, pending, pending_time, text, metadata)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
        """, stream_id, bot_id, now, now, pending, pending_time, text,
            json.dumps(metadata))
Ejemplo n.º 6
0
def call(name, args):
    out = requests.post("http://localhost:5000/%s" % name, json.dumps(args))
    return out.json()
Ejemplo n.º 7
0
 def _write_json(self, data):
     data = json.dumps(data)
     self.write(data + "\n")