-
Notifications
You must be signed in to change notification settings - Fork 52
/
DeleteFutureEvents.py
executable file
·80 lines (71 loc) · 3.29 KB
/
DeleteFutureEvents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""
# Purpose: For a Google Drive User(s), output a CSV that shows all user organized events with a start date >= a specified date; they can then be deleted.
# Note: This script can use GAM7 or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Customize: Set DELETE_EVENTS_WITH_ATTENDEES = True or False to determine whether events with attendees will be deleted.
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Get calendar events for a user
# $ Example, user's primary calendar: gam redirect csv ./UserEvents.csv user [email protected] print events primary singleevents orderby starttime maxattendees 1
# $ Example, all calendars a user owns: gam redirect csv ./UserEvents.csv user [email protected] print events minaccessrole owner singleevents orderby starttime maxattendees 1
# 2: From that list of Events, output a CSV file with only the rows with an event start date >= a specified date
# $ python3 DeleteFutureEvents.py yyyy-mm-dd UserEvents.csv UserFutureEvents.csv
# 3: Delete the events
# Parallel, faster:
# $ gam csv UserFutureEvents.csv gam user "~primaryEmail" delete event calendars "~calendarId" events "~id" doit
# Serial, cleaner output:
# $ gam csvkmd users UserFutureEvents.csv keyfield primaryEmail subkeyfield calendarId datafield id delete event calendars csvsubkey calendarId events csvdata id doit
# 4: Empty the calendars trash
# $ gam csvkmd users UserFutureEvents.csv keyfield primaryEmail datafield calendarId empty calendartrash calendars csvdata calendarId
"""
import csv
import datetime
import sys
DELETE_EVENTS_WITH_ATTENDEES = False
YYYYMMDD_FORMAT = '%Y-%m-%d'
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
if (len(sys.argv) > 3) and (sys.argv[3] != '-'):
outputFile = open(sys.argv[3], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
inputFile = open(sys.argv[2], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
if len(sys.argv) > 1:
startDate = sys.argv[1]
try:
datetime.datetime.strptime(startDate, YYYYMMDD_FORMAT)
except ValueError:
sys.stderr.write(f'ERROR: date ({startDate}) is not valid, it must be (yyyy-mm-dd)\n')
sys.exit(1)
else:
startDate = datetime.datetime.now().strftime(YYYYMMDD_FORMAT)
inputCSV = csv.DictReader(inputFile, quotechar=QUOTE_CHAR)
outputCSV = csv.DictWriter(outputFile, inputCSV.fieldnames, lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for row in inputCSV:
if row['primaryEmail'] != row.get('creator.email'):
continue
if row.get('start.date'):
if row['start.date'] < startDate:
continue
elif row.get('start.dateTime'):
if row['start.dateTime'][:10] < startDate:
continue
else:
continue
if not DELETE_EVENTS_WITH_ATTENDEES:
numAttendees = row.get('attendees', '')
if numAttendees and int(numAttendees) > 0:
continue
outputCSV.writerow(row)
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()