Skip to content

Commit

Permalink
Avoid creating duplicate events by respecting the UID field
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dbarnett committed Aug 25, 2024
1 parent cec6f25 commit b5ebd23
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
52 changes: 28 additions & 24 deletions gcalcli/gcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions gcalcli/ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b5ebd23

Please sign in to comment.