-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (121 loc) · 4.51 KB
/
main.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
# A Python script to pick up follow-up canvasser emails from your inbox and put them
# into a Google Sheet for logging. Setup: enable Gmail API; authorise via OAuth 2.0 as
# Desktop app
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import base64
import re
from datetime import datetime
GMAIL_QUERY = (
"subject:'Labour Doorstep - Follow-up actions from your canvassing session'"
)
SPREADSHEET_ID = "1MIzNcxvvwLWGuRYtWMmbNyPqmG3oGblUlowfR16m6J8"
# If modifying these scopes, delete the file token.json.
SCOPES = [
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/spreadsheets",
]
def auth(creds):
"""Authorises"""
# 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)
return creds
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
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=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
return creds
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
creds = auth(creds)
# Call the Gmail API
mail_service = build("gmail", "v1", credentials=creds)
# Find all messages with subject "Labour Doorstep - Follow-up actions from your
# canvassing session"
message_response = (
mail_service.users()
.messages()
.list(
userId="me",
q=GMAIL_QUERY,
maxResults=500,
)
.execute()
)
email_messages = message_response.get("messages")
email_messages.reverse()
# Find last action date
sheet_service = build("sheets", "v4", credentials=creds)
# Call the Sheets API
sheet = sheet_service.spreadsheets()
result = (
sheet.values().get(spreadsheetId=SPREADSHEET_ID, range="ACTIONS!A:D").execute()
)
values = result.get("values", [])
last_action_date = datetime.strptime((values[len(values) - 1])[0], "%Y-%m-%d")
# Loop through each email and extract the relevant variables
for email in email_messages:
email = (
mail_service.users()
.messages()
.get(userId="me", id=email["id"], format="full")
).execute()
headers = email["payload"]["headers"]
for header in headers:
if header["name"] == "Date":
date = header["value"][:-15]
date = datetime.strptime(date, "%a, %d %b %Y")
date_format = date.strftime("%Y-%m-%d")
body = email["payload"]["parts"][0]["body"]["data"].strip()
body_decoded = str(base64.urlsafe_b64decode(body))
# Find an action
for action in re.finditer(r"\<li\>(.+?)\ \-\ (.+?)\<\/li\>", body_decoded):
name = action.group(1)
address = action.group(2)
# Check if the action date is later than last_action_date
if date > last_action_date:
# Function to write date, name and address into the Google sheet
write_to_sheet(creds, date, name, address)
else:
print(f"Old email from {date_format} skipped.")
def write_to_sheet(creds, date, name, address):
sheet_service = build("sheets", "v4", credentials=creds)
# Call the Sheets API
sheet = sheet_service.spreadsheets()
# Set date
date = (date).strftime("%Y-%m-%d")
values = [
[date, name, address],
]
body = {"values": values}
result = (
sheet.values()
.append(
spreadsheetId=SPREADSHEET_ID,
range="ACTIONS!A:D",
body=body,
valueInputOption="USER_ENTERED",
)
.execute()
)
print(f"{(result.get('updates').get('updatedCells'))} cells appended.")
return result
if __name__ == "__main__":
main()