From fdff9ef67d470ead152917f17ae2d9987fd76f3d Mon Sep 17 00:00:00 2001 From: Yessiest Date: Fri, 4 Feb 2022 00:43:40 +0000 Subject: [PATCH] implemented offline caching --- decal | 56 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/decal b/decal index 6b9195d..00406a4 100755 --- a/decal +++ b/decal @@ -62,6 +62,9 @@ parser.add_argument("-n", action="store", type=int, help="show n months") +parser.add_argument("-s","--sync", + action="store_true", + help="sync the calendar cache") args = vars(parser.parse_args()) @@ -74,7 +77,10 @@ if args["create"]: if not os.path.exists(configpath): config['DEFAULT'] = {'uri': 'your caldav server here', 'user': 'your username here', - 'password': 'your pass here'} + 'password': 'your pass here', + 'cache':os.env['HOME']+'/.cache/decal.json', + 'cacheEnabled':1, + 'syncAfter':1} print("Creating an empty config in ~/.config/decal.conf") with open(configpath,'w') as configfile: config.write(configfile) @@ -189,13 +195,6 @@ else: start,end = getbounds(args["year"],args["month"],1) offset = 1 -# connect to the DAV and receive calendars -client = caldav.DAVClient(url = config['DEFAULT']['uri'], - username = config['DEFAULT']['user'], - password = config['DEFAULT']['password']) -principal = client.principal() -calendars = principal.calendars() - # aggregate selected calendars def aggregateCalendars(calendars): calendars2 = [] @@ -205,10 +204,6 @@ def aggregateCalendars(calendars): calendars2.append(cal) return calendars2 -if "calendars" in config['DEFAULT']: - calendars = aggregateCalendars(calendars) - - def daysOfEvent(event): event = event.vobject_instance.vevent.contents curdate = event["dtstart"][0].value @@ -234,7 +229,6 @@ def jsonifyEvent(event): evdata[key] = event[key][0].value return evdata - def generateDateTree(calendars): events = {} for cal in calendars: @@ -246,10 +240,42 @@ def generateDateTree(calendars): events[date].append(jsonifyEvent(event)) return events -events = generateDateTree(calendars) +cache = None +if "cache" in config['DEFAULT']: + if os.path.exists(config['DEFAULT']['cache']): + with open(config['DEFAULT']['cache'],"r") as file: + cache = json.loads(file.read()) + +def updateCriteria(): + if not ("cacheEnabled" in config["DEFAULT"]): + return False + if (not (config['DEFAULT']["cacheEnabled"] == "1")): + return False + if not cache: + return True + date = datetime.datetime.strptime(cache["lastsync"],"%Y-%m-%d") + if args["sync"]: + return True + return (date.date() <= today - timedelta(days=int(config['DEFAULT']["syncAfter"]))) + +#Update cache if update criteria are met. +if updateCriteria(): + # connect to the DAV and receive calendars + client = caldav.DAVClient(url = config['DEFAULT']['uri'], + username = config['DEFAULT']['user'], + password = config['DEFAULT']['password']) + principal = client.principal() + calendars = principal.calendars() + if "calendars" in config['DEFAULT']: + calendars = aggregateCalendars(calendars) + cache = generateDateTree(calendars) + cache["lastsync"] = str(today) + with open(config['DEFAULT']['cache'],"w") as file: + file.write(json.dumps(cache)) +events = cache if args["json"]: - print(json.JSONEncoder().encode(events)) + print(json.dumps(events)) exit(0) # and now we're just generating calendar lines