-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
213 lines (156 loc) · 6.14 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
import os.path
import threading
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/contacts.readonly",
"https://www.googleapis.com/auth/directory.readonly"]
data: list = []
def wrap_error(func):
"""
Used to ensure other function execution doesnt stop due to occurence of an exception
Thanks a lot: https://stackoverflow.com/a/40102885/21615084
"""
def func_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return func_wrapper
def authenticate() -> None:
"""
Authenticate user using Google OAuth
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
print("User not authenticated... Authentication flow Started...")
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=8080)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
if creds.valid:
print("User authenticated")
try:
service = build("people", "v1", credentials=creds)
fetch_contacts(service)
fetch_directory_contacts(service)
except Exception as err:
print("Error: ", err)
@wrap_error
def fetch_contacts(service):
finished: bool = False
next_page_token: str = None
data: set = set()
count: int = 0
print("Fetching Contacts...", end="\r")
while not finished:
results = service.people().connections().list(
resourceName='people/me',
personFields='names,emailAddresses',
pageSize=1000,
).execute()
connections = results.get("connections", [])
count += len(connections) # Increment the count of records fetched
print(f"Fetching Contacts {count}...", end='\r')
for person in connections:
# Store email addresses (if multiple) in a list after fetching from dict
emailAddresses: list = person.get("emailAddresses", [])
# Check if the value exists
if emailAddresses:
for email in emailAddresses: # Iterate over lists
data.add(email['value'].strip())
# Fetch the next page of results (1k)
next_page_token = results.get('nextPageToken')
# If no more results then exit the while loop
finished = not next_page_token
write_contacts_file(data)
@wrap_error
def fetch_directory_contacts(service) -> None:
"""
Fetch all the contacts from Directory
"""
finished: bool = False
next_page_token: str = None
data: set = set()
count: int = 0
print("Fetching Directory Contacts...", end='\r')
while not finished:
# Fetch results of listing directory
results = service.people().listDirectoryPeople(
readMask='names,emailAddresses',
pageSize=1000,
sources="DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE",
pageToken=next_page_token,
).execute()
result = results.get('people', [])
count += len(result) # Increment the count of records fetched
print(f"Fetching Directory Contacts {count}...", end='\r')
for person in result:
# Store email addresses (if multiple) in a list after fetching from dict
emailAddresses: list = person.get("emailAddresses", [])
# Check if the value exists
if emailAddresses:
for email in emailAddresses: # Iterate over lists
data.add(email['value'].strip())
# Fetch the next page of results (1k)
next_page_token = results.get('nextPageToken')
# If no more results then exit the while loop
finished = not next_page_token
write_directory_file(data)
def perform_write(list: list, filename: str, contact_type: str) -> None:
"""
Write the contents of a list to the specified filename
"""
if list:
with open(filename, "w") as file:
for i, data in enumerate(list):
print(f"Progress (Writing to File): {i}/{len(list)}", end='\r')
file.write(data + "\n")
print(f"{filename} Successfully Written!")
else:
print(f"{contact_type} is Empty! No Records found!")
def write_contacts_file(data_set: set) -> None:
"""
"""
data: list = sorted(data_set)
threading.Thread(target=perform_write, args=(
data, "contacts_emails.txt", "Personal Contacts")).start()
def write_directory_file(data_set: set) -> None:
"""
Create 2 threads to write to 2 files.
File 1 (directory_emails_alphabetic.txt) contains emails sorted by their name.
File 2 (directory_emails_domain.txt) contains emails sorted by the domain
"""
if not data_set:
print("No Directory Exists!")
return
data: list = sorted(data_set)
data_sortby_domain: list = sorted(data_set, key=domain)
thread1 = threading.Thread(target=perform_write, args=(
data, "directory_emails_alphabetic.txt", "Directory Contacts"))
thread2 = threading.Thread(target=perform_write, args=(
data_sortby_domain, "directory_emails_domain.txt", "Directory Contacts"))
thread1.start()
thread2.start()
def domain(email) -> str:
"""
Return the domains of the specified email
"""
_, domain = email.split('@')
return domain
if __name__ == "__main__":
authenticate()