/
gmail-trip-tracker.py
102 lines (92 loc) · 3.97 KB
/
gmail-trip-tracker.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
from __future__ import print_function
from flask import Flask
from flask import Markup
from flask import Flask
from flask import render_template
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from bs4 import BeautifulSoup
import base64
import re
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
"""
Searches GMAIL for location services emails and pulls the cities traveled count out then plots the data by Month.
Thanks to: https://github.com/abhishekchhibber/Gmail-Api-through-Python
"""
creds = None
# The file token.pickle 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.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
# This will open a browser to store the token locally.
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()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
user_id = 'me'
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API / query based on sender address
query = 'noreply-maps-timeline@google.com'
results = service.users().messages().list(userId=user_id,q=query).execute()
messages = results['messages']
# Ugly but works perfectly for these emails.
regMonth = r'(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)'
regYear = r'(\d{4})'
# Initialize data that will be fed to our line chart.
city_data = []
month_data = []
for msg in messages:
msg_id = msg['id']
message = service.users().messages().get(userId=user_id, id=msg_id).execute() # fetch the message using API
payld = message['payload'] # get payload of the message base64 encoded
headr = payld['headers'] # get header of the payload
for one in headr: # getting the Subject
if one['name'] == 'Subject':
msg_subject = one['value']
line_reg = re.findall(regMonth, msg_subject)
for match in line_reg:
if match is None:
continue
else:
for two in headr: # getting the date
if two['name'] == 'Date':
msg_date = two['value']
year_reg = re.findall(regYear, msg_date)
# Add month + year / will appear as January 2019, etc
month_data.append(match[0] + ' ' + year_reg[0])
# Fetching message body
body_data = payld['parts'][0]['body']['data'] # fetching first element of the part
clean_one = body_data.replace("-", "+").replace('_', '/') # decoding from Base64 to UTF-8
clean_two = base64.b64decode(bytes(clean_one, 'UTF-8')) # decoding from Base64 to UTF-8
soup = BeautifulSoup(clean_two, "lxml")
mssg_body = soup.prettify().splitlines()
# mssg_body is a readable form of message body
# Read msg to find the line we want.
for lines in mssg_body:
if 'cities visited this month' in lines:
cities_visited = lines.strip().split(' ')[0]
city_data.append(int(cities_visited))
# Set a relative max.
max = max(city_data) + 5
# Build Flask App to serve the data nicely.
app = Flask(__name__)
@app.route("/")
def chart():
labels = month_data
values = city_data
return render_template('chart.html', title='Cities Traveled', max=max, values=values, labels=labels)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5004, debug=True)