forked from taers232c/GAM-Scripts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCommonEmails.py
executable file
·43 lines (38 loc) · 1.78 KB
/
FindCommonEmails.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
#!/usr/bin/env python3
"""
# Purpose: Create a CSV file showing email addresses that appear in all CSV files generated by separate gam report commands
# 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: Issue various report commands:
# $ gam report users filter "gmail:num_emails_sent<1" parameters gmail:num_emails_sent > num_emails_sent.csv
# $ gam report users filter "accounts:creation_time<2017-09-01T00:00:00.000Z" parameters accounts:creation_time > creation_time.csv
# $ gam report users filter "gmail:last_interaction_time<2016-01-01T00:00:00.000Z" parameters gmail:last_interaction_time > last_interaction_time.csv
# 2: From that list of files, output a CSV file with the header email that shows the email addresses that appear in all files
# $ python3 FindCommonEmails.py ./CommonEmails.csv ./num_emails_sent.csv ./creation_time.csv ./last_interaction_time.csv
"""
import csv
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
if sys.argv[1] != '-':
outputFile = open(sys.argv[1], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['email',], lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
users = {}
allFilesCount = len(sys.argv)-2
for i in range(2, len(sys.argv)):
inputFile = open(sys.argv[i], 'r', encoding='utf-8')
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
email = row['email']
users.setdefault(email, 0)
users[email] += 1
inputFile.close()
for user, count in sorted(iter(users.items())):
if count == allFilesCount:
outputCSV.writerow({'email': user})
if outputFile != sys.stdout:
outputFile.close()