-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts_manager.py
101 lines (83 loc) · 3.04 KB
/
contacts_manager.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
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
import schedule
import time
import os
from google.auth.transport.requests import Request
import pickle
# Create a dictionary of contacts {key = number: val = name}
def get_contacts(service):
connections = service.people().connections().list(
resourceName='people/me',
pageSize=2000,
personFields='names,phoneNumbers').execute()
contacts_dict = {}
for person in connections.get('connections', []):
names = person.get('names', [])
phones = person.get('phoneNumbers', [])
if names and phones:
name = names[0].get('displayName')
number = phones[0].get('canonicalForm', '')
if number:
contacts_dict[number] = name
return contacts_dict
# Create a new contact
def create_contact(name, number, service, contact_dict):
person = {
"names": [
{
"givenName": name,
"familyName": "מיידיז"
}
],
"phoneNumbers": [
{
"value": number,
"type": "mobile"
}
]
}
result = service.people().createContact(body=person).execute()
print(f"Contact created:\t{name}\t{number}")
# Update contact_dict
contact_dict[number] = name
def get_credentials():
# API key and credentials
CLIENT_ID = "1035388992614-fcip9rjlurea35k0g7dvbtonrplv5bl0.apps.googleusercontent.com"
CLIENT_SECRET = "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"
SCOPE = "https://www.googleapis.com/auth/contacts"
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
# Create a flow object to obtain user authorization
flow = InstalledAppFlow.from_client_config(
{
"installed": {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"],
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
}
},
scopes=[SCOPE],
)
# Have the user authorize the application
creds = flow.run_local_server()
# Save the credentials to a file
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
def build_people_service(creds):
service = build('people', 'v1', credentials=creds)
return service
# Call the functions
creds = get_credentials()
people_service = build_people_service(creds)