From 7d840c555b66ef989628b5a23bec51a62fd60133 Mon Sep 17 00:00:00 2001 From: Matthew Vernon Date: Mon, 12 Nov 2018 16:52:06 +0000 Subject: [PATCH] Add looping support If you specify -l now, run through the main loop every 10 minutes. --- gooswapper.py | 56 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/gooswapper.py b/gooswapper.py index 23ac350..1742b55 100644 --- a/gooswapper.py +++ b/gooswapper.py @@ -6,6 +6,7 @@ import os import pickle import collections import argparse +import time import logging logger = logging.getLogger('gooswapper') logger.setLevel(logging.INFO) @@ -425,30 +426,39 @@ def main(): gcal_account = gcal_login(args) gcal_tz = get_gcal_timezone(gcal_account,gcal_id) - - try: - with open(cachepath,"rb") as f: - cache = pickle.load(f) - except FileNotFoundError: - cache = None - - current = get_ex_events(ex_account.calendar) - - if cache is not None: - added,deleted,changed = ex_event_changes(cache,current) - add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,added,gcal_id) - #delete op needs the "cache" set, as that has the link ids in - #for events that are now deleted - del_ex_to_gcal(ex_account,gcal_account,cache,deleted,gcal_id) - update_ex_to_gcal(ex_account,gcal_account,gcal_tz,current, - changed,gcal_id) - else: - toadd = match_ex_to_gcal(ex_account,gcal_account,gcal_tz,current, - gcal_id) - add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,toadd,gcal_id) + #Main loop (broken at the end if login is false) + while True: + try: + with open(cachepath,"rb") as f: + cache = pickle.load(f) + except FileNotFoundError: + cache = None + + current = get_ex_events(ex_account.calendar) + + if cache is not None: + added,deleted,changed = ex_event_changes(cache,current) + add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current, + added,gcal_id) + #delete op needs the "cache" set, as that has the link ids in + #for events that are now deleted + del_ex_to_gcal(ex_account,gcal_account,cache,deleted,gcal_id) + update_ex_to_gcal(ex_account,gcal_account,gcal_tz,current, + changed,gcal_id) + else: + toadd = match_ex_to_gcal(ex_account,gcal_account,gcal_tz,current, + gcal_id) + add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current, + toadd,gcal_id) - with open(cachepath,"wb") as f: - pickle.dump(current,f) + with open(cachepath,"wb") as f: + pickle.dump(current,f) + + #If not looping, break here (after 1 run) + if args.loop==False: + break + #otherwise, wait 10 minutes, then go round again + time.sleep(600) if __name__ == "__main__": main() -- 2.30.2