From 404db42aeaf30947a2da76d22963a2df7beb24dc Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Fri, 22 May 2020 21:21:14 +0200 Subject: [PATCH] Add cache expiration --- notify.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/notify.py b/notify.py index 46041b7..a97656b 100755 --- a/notify.py +++ b/notify.py @@ -1,12 +1,14 @@ #!/usr/bin/env python import berserk, subprocess, pickle -from os import path +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): @@ -19,26 +21,28 @@ def notify_done(key): if not(path.exists(CACHE_FILE)): return(False) else: + data=dict() with open(CACHE_FILE, 'rb') as f: - keys=pickle.load(f) - return(key in keys) + data=pickle.load(f) + if datetime.timestamp(datetime.now()) - data["ts"] >= CACHE_EXPIRE*60: + remove(CACHE_FILE) + return(False) + else: + return(key in data["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() + pickle.dump({"ts":datetime.timestamp(datetime.now()),"keys":key},f) else: - keys=list() + data=list() with open(CACHE_FILE, 'rb') as f: - keys=pickle.load(f) - f.close() - if not(key in keys): - keys.append(key) + data=pickle.load(f) + if not(key in data["keys"]): + data["keys"].append(key) with open(CACHE_FILE, 'wb') as f: - pickle.dump(keys,f) - f.close() + pickle.dump(data,f) # Fetch data and notify session = berserk.TokenSession(ACCESS_TOKEN)