From b5ebd23d2205cbe74b7cfec2e06cd55940639fda Mon Sep 17 00:00:00 2001 From: David Barnett Date: Sun, 25 Aug 2024 17:04:29 -0600 Subject: [PATCH] Avoid creating duplicate events by respecting the UID field For events that contain a UID field, use Google Calendar's event "import" feature instead of "insert" to avoid creating duplicates. Note that events with UID populated may still get processed as new/updated events if the SEQUENCE number is also incremented (https://www.kanzaki.com/docs/ical/sequence.html). --- gcalcli/gcal.py | 52 ++++++++++++++++++++++++++----------------------- gcalcli/ics.py | 6 ++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/gcalcli/gcal.py b/gcalcli/gcal.py index 09a3c42..043a8e8 100644 --- a/gcalcli/gcal.py +++ b/gcalcli/gcal.py @@ -1464,32 +1464,36 @@ def ImportICS(self, verbose=False, dump=False, reminders=None, continue self._add_reminders(event, reminders) + if not verbose: - new_event = self._retry_with_backoff( - self.get_events().insert( - calendarId=self.cals[0]['id'], body=event - ) - ) - hlink = new_event.get('htmlLink') - self.printer.msg('New event added: %s\n' % hlink, 'green') - continue + # Don't prompt, just assume user wants to import. + pass + else: + # Prompt for whether to import. + self.printer.msg('\n[S]kip [i]mport [q]uit: ', 'magenta') + val = input() + if not val or val.lower() == 's': + continue + elif val.lower() == 'q': + sys.exit(0) + elif val.lower() == 'i': + # Import requested, continue + pass + else: + self.printer.err_msg('Error: invalid input\n') + sys.exit(1) - self.printer.msg('\n[S]kip [i]mport [q]uit: ', 'magenta') - val = input() - if not val or val.lower() == 's': - continue - if val.lower() == 'i': - new_event = self._retry_with_backoff( - self.get_events().insert( - calendarId=self.cals[0]['id'], body=event - ) + # Import event + import_method = ( + self.get_events().import_ if 'iCalUID' in event + else self.get_events().insert) + new_event = self._retry_with_backoff( + import_method( + calendarId=self.cals[0]['id'], body=event ) - hlink = new_event.get('htmlLink') - self.printer.msg('New event added: %s\n' % hlink, 'green') - elif val.lower() == 'q': - sys.exit(0) - else: - self.printer.err_msg('Error: invalid input\n') - sys.exit(1) + ) + hlink = new_event.get('htmlLink') + self.printer.msg(f'New event added: {hlink}\n', 'green') + # TODO: return the number of events added return True diff --git a/gcalcli/ics.py b/gcalcli/ics.py index 413f0ad..755e4ef 100644 --- a/gcalcli/ics.py +++ b/gcalcli/ics.py @@ -134,4 +134,10 @@ def CreateEventFromVOBJ( {'displayName': attendee.name, 'email': email} ) + if hasattr(ve, 'uid'): + uid = ve.uid.value.strip() + if verbose: + print(f'UID..........{uid}') + event['iCalUID'] = uid + return event