diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2020-05-22 20:55:03 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2020-05-22 20:55:03 +0200 |
| commit | 242682843ec0264c76a200b34db18c04e964728e (patch) | |
| tree | 9e8003b5884bf2b07310954cbf584ba16bf18b8f | |
| parent | 81a1645bde6774dd149a360382ef52a1a3fe60ee (diff) | |
Now caching notifications
| -rwxr-xr-x | notify.py | 38 |
1 files changed, 35 insertions, 3 deletions
@@ -1,16 +1,45 @@ #!/usr/bin/env python -import berserk, subprocess +import berserk, subprocess, pickle +from os import path # Change ACCESS TOKEN according to your need ACCESS_TOKEN="" -NOTIFY_DURATION=5*60 # Notification duration in seconds +NOTIFY_DURATION=15*60 # Notification duration in seconds +CACHE_FILE="/tmp/lichess_notify_cache" # Change this according to your needs # 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: + with open(CACHE_FILE, 'rb') as f: + keys=pickle.load(f) + return(key in keys) + +# 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],f) + f.close() + else: + keys=list() + with open(CACHE_FILE, 'rb') as f: + keys=pickle.load(f) + f.close() + if not(key in keys): + keys.append(key) + with open(CACHE_FILE, 'wb') as f: + pickle.dump(keys,f) + f.close() + # Fetch data and notify session = berserk.TokenSession(ACCESS_TOKEN) client = berserk.Client(session=session) @@ -18,6 +47,9 @@ data=client.games.get_ongoing() for game in data: opponent=game["opponent"]["username"] lastMove=game["lastMove"] + key=opponent+lastMove if game["isMyTurn"]: - notify_send("Lichess.org ("+opponent+")","It is your turn !\n Your opponent played "+lastMove) + if not(notify_done(key)): + notify_send("Lichess.org ("+opponent+")","It is your turn !\n Your opponent played "+lastMove) + add_key(key) |
