-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
209 lines (155 loc) · 6.84 KB
/
test.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
"""
Project time management script
Author: Marco Gutierrez
MIT License
Usage:
- Asks user for project information and keywords
- Returns hours spent in a month on the project in
excel spreadsheet
"""
from __future__ import print_function
from datetime import datetime, timedelta
import pandas as pd
import os.path
from dateutil.parser import isoparse
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import base64
import email
# if modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly', 'https://mail.google.com/']
class GoogleInfoGatherer:
"""
Gathers information from chosen Google service. Requires credentials from
respective user and API
"""
def __init__(self, credentials, token):
self.credentials = credentials # name of json file with credentials
self.token = token # name of json file with token
def credential_reader(self):
"""
Reads credentials for google api and returns them in required
format
Input: None
Output: credentials in google readable format
"""
# inputting credentials
creds = None
# checking if credentials previously read
if os.path.exists(self.token):
creds = Credentials.from_authorized_user_file(self.token, SCOPES)
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(
self.credentials, SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(self.token, 'w') as token:
token.write(creds.to_json())
return creds
def initializing_service(self, service, version):
"""
Initializes Google service and returns it ready for usage
Input: service name (str), version (str)
Output: Service build
"""
creds = self.credential_reader()
# initializing the calendar service
return build(service, version, credentials=creds)
def initial_final_dates(self, service_name):
"""
Asks user to input his preferred initial and final dates
Input: Initial and final search dates
Output: Formatted initial and final search dates for Google search
"""
# inputting initial date
while True:
min_time = input("Por favor, ingrese una fecha inicial con el siguiente formato (dd/mm/aaaa): ")
try:
min_time_formatted = datetime.strptime(min_time, '%d/%m/%Y').isoformat() + 'Z'
if service_name=="gmail":
min_time_formatted = datetime.strptime(min_time, '%d/%m/%Y').strftime('%Y/%m/%d')
min_time_type = type(min_time_formatted)
except ValueError:
print("No ha ingresado una fecha en el formato correcto. \nPor favor, vuelva a intentarlo")
else:
break
# inputting final date
while True:
max_time = input("\nPor favor, ingrese una fecha final con el siguiente formato (dd/mm/aaaa)\n"+\
"o escriba 'ahora' para usar la fecha actual: ")
try:
if max_time == "ahora":
max_time_formatted = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
if service_name=="gmail":
max_time_formatted = datetime.utcnow().strftime('%Y/%m/%d')
else:
max_time_formatted = datetime.strptime(max_time, '%d/%m/%Y').isoformat() + 'Z'
if service_name=="gmail":
max_time_formatted = datetime.strptime(max_time, '%d/%m/%Y').strftime('%Y/%m/%d')
max_time_type = type(max_time_formatted)
except ValueError:
print("No ha ingresado una fecha en el formato correcto. \nPor favor, vuelva a intentarlo")
else:
break
print(min_time_formatted, max_time_formatted)
return min_time_formatted, max_time_formatted
def get_projects_data(self):
"""
"""
class GmailGatherer(GoogleInfoGatherer):
def __init__(self, credentials, token):
super().__init__(credentials, token)
self.last_searched_results = None # list that stores latest searched items
def time_query(self, min_time=None, max_time=None):
"""
Prepares a time based query for the mail search
Input: initial and final times
Output: query in Google format for Gmail API
"""
# if min/max time not inputted, ask user
if min_time == None or max_time==None:
min_time, max_time = self.initial_final_dates("gmail")
return f"before: {max_time} after: {min_time}"
def search_emails(self, query):
"""
Searches for all emails following a given query
Input: query in Google format for Gmail API
Output: search results
"""
service = self.initializing_service('gmail', 'v1')
mail_id_result = service.users().messages().list(userId="me", q=query).execute()
number_results = mail_id_result["resultSizeEstimate"]
mail_results = []
if number_results > 0:
message_ids = mail_id_result["messages"]
for id in message_ids:
message = service.users().messages().get(userId="me", id=id["id"], format="raw").execute()
message_raw = base64.urlsafe_b64decode(message["raw"].encode("ASCII"))
message_str = email.message_from_bytes(message_raw)
content_types = message_str.get_content_maintype()
print("content_types =", content_types)
if content_types == "multipart":
parsed_message = message_str.get_payload()[0]
mail_result.append(parsed_message.get_payload())
else:
mail_result.append(message_str.get_payload())
else:
print("no results")
self.last_searched_results = mail_results
return mail_results
def search_emails_keywords(self):
"""
Searches for emails with an specific keyword
Input: User project name, user keywords per project
Output: List with user events
"""
email_dict = {}
self.get_events() # searching all events
gaaa = GmailGatherer("credentials.json", "token.json")
query = gaaa.time_query()
print(gaaa.search_emails(query))