Skip to content

Commit

Permalink
Make sensible data copy configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
huanlui committed Jun 22, 2021
1 parent 632c244 commit fa6cba8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
17 changes: 12 additions & 5 deletions gcalsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
import sys


def main(sourceAccountName, sourceCalendarId, targetAccountName, targetCalendarId, datePeriod):
def main(sourceAccountName, sourceCalendarId, targetAccountName, targetCalendarId, datePeriod, copySensibleData):
print(f'Copying from {sourceAccountName}:{sourceCalendarId} to {targetAccountName}:{targetCalendarId}'
f' from {datePeriod.start} to {datePeriod.end}')
if copySensibleData:
print('WARNING: copying full inforation from the event. Make sure sensible data is not copied to your new calendar!!')

sourceCalendar = Calendar(sourceAccountName, sourceCalendarId)
targetCalendar = Calendar(targetAccountName, targetCalendarId)

targetCalendar.copyAllEventsFrom(sourceCalendar, DatePeriod.weeks(1))
targetCalendar.copyAllEventsFrom(sourceCalendar, DatePeriod.weeks(1), copySensibleData)

print('Events copied successfully')


if __name__ == '__main__':
if len(sys.argv) < 5:
print(f"Usage python3 gcalsync sourceAccountName sourceCalendarId targetAccountName targetCalendarId")
if len(sys.argv) < 6:
print(
f"Usage python3 gcalsync sourceAccountName sourceCalendarId targetAccountName targetCalendarId [copySensibleData]")

copySensibleData = len(sys.argv) > 6 and sys.argv[6] == 'copySensibleData'

main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],DatePeriod.parse(sys.argv[5]))
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], DatePeriod.parse(sys.argv[5]), copySensibleData)
## Pass as first an single argument the ID of your client calendar visible from your TW account
## use target.printCalendars() to see all your calendars and find out this id.
35 changes: 18 additions & 17 deletions src/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'https://www.googleapis.com/auth/calendar.events'
]

COPIED_EVENT_LEADING_TEXT = '[CLIENT MEETING]'

class Calendar:
def __init__(self, name, calendarId):
self.name = name
Expand All @@ -30,31 +32,32 @@ def printCalendars(self):
if not page_token:
break

def copyAllEventsFrom(self, otherCalendar, period: DatePeriod):
def copyAllEventsFrom(self, otherCalendar, period: DatePeriod, copySensibleData):
self.removeCopiedEvents(period)
for event in otherCalendar.getEvents(period):
self.createEventFrom(event)
self.createEventFrom(event, copySensibleData)

def getEvents(self, period: DatePeriod):
events_result = self.service.events().list(calendarId=self.calendarId, timeMin=period.start,
timeMax=period.end, singleEvents=True,
orderBy='startTime').execute()
timeMax=period.end, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
return events

def removeCopiedEvents(self, period: DatePeriod):
eventsToRemove = list(filter(lambda event: '[CLIENT MEETING]' in event.get('summary','') , self.getEvents(period)))
eventsToRemove = list(
filter(lambda event: COPIED_EVENT_LEADING_TEXT in event.get('summary', ''), self.getEvents(period)))
for event in eventsToRemove:
self.deleteEvent(event.get("id"))

def deleteEvent(self, eventId):
self.service.events().delete(calendarId=self.calendarId , eventId=eventId).execute()
self.service.events().delete(calendarId=self.calendarId, eventId=eventId).execute()

def createEventFrom(self,sourceEvent):
def createEventFrom(self, sourceEvent, copySensibleData):
eventBody = {
'summary': '[CLIENT MEETING] ' + sourceEvent.get('summary',''),
'location': sourceEvent.get('location', ''),
'description': sourceEvent.get('description', ''),
'summary': (COPIED_EVENT_LEADING_TEXT + ' ' + sourceEvent.get('summary', '')) if copySensibleData else COPIED_EVENT_LEADING_TEXT,
'location': sourceEvent.get('location', '') if copySensibleData else '',
'description': sourceEvent.get('description', '') if copySensibleData else '',
'start': sourceEvent.get('start'),
'end': sourceEvent.get('end'),
'recurrence': sourceEvent.get('recurrence'),
Expand All @@ -63,25 +66,24 @@ def createEventFrom(self,sourceEvent):
'reminders': sourceEvent.get('reminders'),
}

createdEvent = self.service.events().insert(calendarId=self.calendarId , body=eventBody).execute()
createdEvent = self.service.events().insert(calendarId=self.calendarId, body=eventBody).execute()

return createdEvent

def printEventsForPeriod(self, datePeriod: DatePeriod):
def printEventsForPeriod(self, datePeriod: DatePeriod):
print('\n++++++++++++', self.name, 'calendar', '++++++++++++')
self.printEvents(self.getEvents(datePeriod))
print('++++++++++++++++++++++++++++++++++++++++++')


def printEvents(self,events):

def printEvents(self, events):
if not events:
print('No upcoming events found.')
for event in events:
# start = event['start'].get('dateTime', event['start'].get('date'))
# print(start, event['summary'])
print(event)

def __buildService(self,source):
def __buildService(self, source):
credentials = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
Expand All @@ -105,4 +107,3 @@ def __buildService(self,source):
pickle.dump(credentials, token)

return build('calendar', 'v3', credentials=credentials)

0 comments on commit fa6cba8

Please sign in to comment.