comparison multimerge.py @ 110:c6771a596d77

Add gcm_fetch_events() function that properly implements chained event list fetching.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 18 Oct 2016 12:29:13 +0300
parents 8cf20367a372
children 8b773358ad47
comparison
equal deleted inserted replaced
109:8cf20367a372 110:c6771a596d77
156 def gcm_compare_events(ev1, ev2): 156 def gcm_compare_events(ev1, ev2):
157 for field in ev1: 157 for field in ev1:
158 if not field in gcm_no_compare_fields and ev1[field] != ev2[field]: 158 if not field in gcm_no_compare_fields and ev1[field] != ev2[field]:
159 return False 159 return False
160 return True 160 return True
161
162
163 ## Fetch events for given calendar
164 def gcm_fetch_events(calendarId, showDeleted):
165 events = []
166 ev_token = None
167 while True:
168 try:
169 result = service.events().list(
170 calendarId=calendarId,
171 showDeleted=showDeleted,
172 singleEvents=False,
173 pageToken=ev_token,
174 ).execute()
175 except Exception as e:
176 gcm_fatal(u"Failed to fetch calendar events for {0}:\n\nERROR: {1}\n".format(calendarId, str(e)))
177
178 events.extend(result.get("items", []))
179 ev_token = result.get("nextPageToken")
180 if not ev_token:
181 break
182
183 return events
161 184
162 185
163 ### 186 ###
164 ### Class for parsing and manipulating RGB colors 187 ### Class for parsing and manipulating RGB colors
165 ### 188 ###
525 ## Now, we fetch and collect events 548 ## Now, we fetch and collect events
526 gcm_debug(3, u"Fetching calendar events .. ") 549 gcm_debug(3, u"Fetching calendar events .. ")
527 src_events = [] 550 src_events = []
528 for calendar in src_calendars: 551 for calendar in src_calendars:
529 gcm_debug(4, u"- {0} ({1})".format(calendar["id"], calendar["summary"])) 552 gcm_debug(4, u"- {0} ({1})".format(calendar["id"], calendar["summary"]))
530 try:
531 result = service.events().list(
532 timeZone="EEST",
533 calendarId=calendar["id"],
534 singleEvents=False,
535 showDeleted=False,
536 # orderBy="startTime",
537 ).execute()
538 except Exception as e:
539 gcm_fatal(u"Failed to fetch calendar events for {0}:\n\n{1}\n\nERROR: {2}\n".format(calendar["id"], calendar, str(e)))
540
541 c_found = None 553 c_found = None
542 if "colorId" in calendar and calendar["colorId"] in colors["calendar"]: 554 if "colorId" in calendar and calendar["colorId"] in colors["calendar"]:
543 gcm_debug(4, u" Calendar color: {0}".format(colors["calendar"][calendar["colorId"]])) 555 gcm_debug(4, u" Calendar color: {0}".format(colors["calendar"][calendar["colorId"]]))
544 c_found = gcm_find_nearest_color(colors["event"], colors["calendar"][calendar["colorId"]], 100) 556 c_found = gcm_find_nearest_color(colors["event"], colors["calendar"][calendar["colorId"]], 100)
545 if c_found: 557 if c_found:
546 gcm_debug(4, u" Found nearest event color ID: {0}, {1}".format(c_found, colors["event"][c_found])) 558 gcm_debug(4, u" Found nearest event color ID: {0}, {1}".format(c_found, colors["event"][c_found]))
547 else: 559 else:
548 gcm_debug(4, u" No matching event color found!") 560 gcm_debug(4, u" No matching event color found!")
549 561
550 # Add events, if any, to main list 562 # Add events, if any, to main list
551 events = gcm_generate_ids(result.get("items", []), calendar["id"], "___") 563 events = gcm_generate_ids(gcm_fetch_events(calendar["id"], False), calendar["id"], "___")
552 if events: 564 if events:
553 for event in events: 565 for event in events:
554 if event["status"] != u"cancelled": 566 if event["status"] != u"cancelled":
555 if c_found != None: 567 if c_found != None:
556 event["colorId"] = c_found 568 event["colorId"] = c_found
560 gcm_dump_events(events, (lambda ev: ev["status"] != u"cancelled")) 572 gcm_dump_events(events, (lambda ev: ev["status"] != u"cancelled"))
561 573
562 574
563 ## Get current events 575 ## Get current events
564 gcm_debug(3, u"Fetching current target calendar events.") 576 gcm_debug(3, u"Fetching current target calendar events.")
565 result = service.events().list( 577 dst_events = gcm_generate_ids(gcm_fetch_events(cfg.dst_id, True), "", "")
566 calendarId=cfg.dst_id,
567 singleEvents=False,
568 showDeleted=True,
569 ).execute()
570
571 dst_events = gcm_generate_ids(result.get("items", []), "", "")
572 gcm_debug(3, u"Found {0} event(s).".format(len(dst_events))) 578 gcm_debug(3, u"Found {0} event(s).".format(len(dst_events)))
573 579
574 580
575 ## Start merging events .. 581 ## Start merging events ..
576 gcm_debug(3, u"Re-merging events to target calendar ..") 582 gcm_debug(3, u"Re-merging events to target calendar ..")