-
Notifications
You must be signed in to change notification settings - Fork 52
/
DeleteOldContacts.py
executable file
·64 lines (55 loc) · 2.7 KB
/
DeleteOldContacts.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
#!/usr/bin/env python3
"""
# Purpose: Delete old contacts from user's personal contacts
# Note: This script can use GAM7 or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# 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: Generate OldContacts.csv, header row primaryEmail, one email address per row
# You can generate this file by hand, or, for example, if you have an OU of users to remove from all other user's contact lists,
# $ gam redirect csv ./OldContacts.csv ou "/Path/to/OU" print users primaryemail
# 2: Get current contacts for all users, if you don't want all users, replace all users with your user selection in the command below
# $ gam redirect csv ./CurrentContacts.csv all users print contacts fields email,name
# 3: From that list of user's contacts, output a CSV file with headers: User,ContactID,Name,Email
# that shows user's contacts with an email address from OldContacts.csv
# $ python3 DeleteOldContacts.py ./OldContacts.csv ./CurrentContacts.csv ./DeleteContacts.csv
# 4: Inspect DeleteContacts.csv, verify that it makes sense and then proceed
# 5: If desired, delete the contacts
# $ gam csv DeleteContacts.csv gam user "~User" delete contact "~ContactID"
"""
import csv
import re
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
EMAILS_N_ADDRESS = re.compile(r"Emails.(\d+).address")
OldContacts = set()
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
OldContacts.add(row['primaryEmail'].lower())
inputFile.close()
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) > 3) and (sys.argv[3] != '-'):
outputFile = open(sys.argv[3], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['User','ContactID','Name','Email'], lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
for k, v in iter(row.items()):
mg = EMAILS_N_ADDRESS.match(k)
if mg and v.lower() in OldContacts:
outputCSV.writerow({'User': row['User'],
'ContactID': row['ContactID'],
'Name': row['Name'],
'Email': v})
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()