summaryrefslogtreecommitdiff
path: root/client.py
blob: 34fb0530a6c9b683e4ab3304fb4d415f7b6ddb02 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python2

import tgl
import socket
import datetime

IP_ADDR = "0.0.0.0"
FRIEND_1_ID = 00000000000
FRIEND_2_ID = 00000000000


def update_esp(what):
    print("sending " + str(what))
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((IP_ADDR, 23))
    sock.send(what)
    sock.close()


# only send changes, prevents unnecessary wakeups
last_1 = b""
last_2 = b""


def on_news(peer):
    then = peer.user_status["when"]
    now = datetime.datetime.now()

    if (now - then).seconds < 1 * 60 or \
            then > now:
        # `when` in the future means online right now
        if peer.id == FRIEND_1_ID and last_i != b"B":
            update_esp(b"B")
        else:
            if peer.id == FRIEND_2_ID and last_t != b"A":
                update_esp(b"A")
    else:
        if peer.id == FRIEND_1_ID and last_i != b"b":
            update_esp(b"b")
        else:
            if peer.id == FRIEND_2_ID and last_t != b"a":
                update_esp(b"a")


def contact_list_cb(success, peer_list):
    if success:
        for peer in peer_list:
            on_news(peer)


last_run = datetime.datetime(1970, 1, 1)


def on_loop():
    global last_run
    # update every two minutes
    if (datetime.datetime.now() - last_run).seconds > 1 * 60:
        last_run = datetime.datetime.now()
        tgl.get_contact_list(contact_list_cb)


tgl.set_on_loop(on_loop)