diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2015-11-08 10:46:58 +0100 |
|---|---|---|
| committer | schneefux <schneefux-github@gmx-topmail.de> | 2015-11-08 10:46:58 +0100 |
| commit | 67b656eae75be20bf036558db83c34ac10957f5f (patch) | |
| tree | d2b30bcd21dcc7277f82e290797dd927c18995ca /client.py | |
| download | internet-is-magic-67b656eae75be20bf036558db83c34ac10957f5f.tar.gz internet-is-magic-67b656eae75be20bf036558db83c34ac10957f5f.zip | |
created
Diffstat (limited to 'client.py')
| -rwxr-xr-x | client.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/client.py b/client.py new file mode 100755 index 0000000..34fb053 --- /dev/null +++ b/client.py @@ -0,0 +1,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) |
