Add cache expiration

This commit is contained in:
Loic Guegan 2020-05-22 21:21:14 +02:00
parent 242682843e
commit 404db42aea

View file

@ -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)