aboutsummaryrefslogtreecommitdiff
path: root/notify.py
blob: 94d503754f8c90fe744e1bf73a48c768540162d2 (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
#!/usr/bin/env python

import berserk, subprocess, pickle
from datetime import datetime
from os import path,remove

# Change ACCESS TOKEN according to your need
ACCESS_TOKEN=""
NOTIFY_DURATION=15*60 # Notification duration in seconds
CACHE_FILE="/tmp/lichess_notify_cache" # Change this according to your needs
CACHE_EXPIRE=300 # Change cache expiration in minutes

# Notify using notify-send
def notify_send(summary, message):
    subprocess.Popen(['notify-send', '-u', 'critical','-t', str(NOTIFY_DURATION*1000), summary, message])
    return


# Check if notify already done
def notify_done(key):
    if not(path.exists(CACHE_FILE)):
        return(False)
    else:
        data=dict()
        with open(CACHE_FILE, 'rb') as f:
            data=pickle.load(f)
        if datetime.timestamp(datetime.now()) - data[key] >= CACHE_EXPIRE*60:
            remove(CACHE_FILE)
            return(False)
        else:
            return(key in data)

# Save notify key in cache
def add_key(key):
    if not(path.exists(CACHE_FILE)):
        with open(CACHE_FILE, 'wb') as f:
            pickle.dump({key:datetime.timestamp(datetime.now())},f)
    else:
        data=list()
        with open(CACHE_FILE, 'rb') as f:
            data=pickle.load(f)
        if not(key in data["keys"]):
            data[key]=datetime.timestamp(datetime.now())
        with open(CACHE_FILE, 'wb') as f:
            pickle.dump(data,f)

# Fetch data and notify
session = berserk.TokenSession(ACCESS_TOKEN)
client = berserk.Client(session=session)
data=client.games.get_ongoing()
for game in data:
    opponent=game["opponent"]["username"]
    lastMove=game["lastMove"]
    key=opponent+lastMove
    if game["isMyTurn"]:
        if not(notify_done(key)):
            notify_send("Lichess.org ("+opponent+")","It is your turn !\n Your opponent played "+lastMove)
            add_key(key)