Spaces:
Running
Running
Update google_function.py
Browse files- google_function.py +87 -9
google_function.py
CHANGED
|
@@ -21,7 +21,8 @@ SCOPES = ["https://www.googleapis.com/auth/gmail.compose",
|
|
| 21 |
"https://www.googleapis.com/auth/calendar.readonly",
|
| 22 |
"https://www.googleapis.com/auth/documents.readonly",
|
| 23 |
"https://www.googleapis.com/auth/forms.body",
|
| 24 |
-
"https://www.googleapis.com/auth/spreadsheets.readonly"
|
|
|
|
| 25 |
|
| 26 |
#---------------------------------------------------------- LETTURA EMAIL ---------------------------------------------------------
|
| 27 |
def converti_email_txt(body):
|
|
@@ -33,10 +34,13 @@ def converti_email_txt(body):
|
|
| 33 |
body = body
|
| 34 |
return body
|
| 35 |
|
| 36 |
-
def leggi_gmail(max_results=10):
|
| 37 |
links = []
|
| 38 |
service = build("gmail", "v1", credentials=st.session_state.creds)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
messages = results.get("messages", [])
|
| 41 |
testo_email = ''
|
| 42 |
if not messages:
|
|
@@ -68,15 +72,18 @@ def leggi_gmail(max_results=10):
|
|
| 68 |
body = converti_email_txt(body)
|
| 69 |
break
|
| 70 |
else:
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
testo_email += 'Data Ricezione: ' + received_date[5:] + '\nMittente: ' + from_name + '\nOggetto: ' + ', '.join(subject) + '\nTesto Email: ' + body + '\n---------------------------------------------------\n'
|
| 74 |
links.append(('Mittente: ' + from_name, 'Data: ' + received_date[5:] + '\n\nOggetto: ' + ', '.join(subject)))
|
|
|
|
|
|
|
| 75 |
return testo_email, links
|
| 76 |
|
| 77 |
#---------------------------------------------------------- SCRITTURA BOZZA EMAIL ---------------------------------------------------------
|
| 78 |
def scrivi_bozza_gmail(testo):
|
| 79 |
-
draft_url = ''
|
| 80 |
try:
|
| 81 |
service = build("gmail", "v1", credentials=st.session_state.creds)
|
| 82 |
message = EmailMessage()
|
|
@@ -93,18 +100,87 @@ def scrivi_bozza_gmail(testo):
|
|
| 93 |
print(draft_details)
|
| 94 |
except HttpError as error:
|
| 95 |
print(f"An error occurred: {error}")
|
| 96 |
-
return draft_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
#---------------------------------------------------------- LEGGI GOOGLE CALENDAR ---------------------------------------------------------
|
| 99 |
-
def leggi_calendario_google(max_results=10):
|
| 100 |
try:
|
| 101 |
service = build("calendar", "v3", credentials=st.session_state.creds)
|
| 102 |
calendar_list_result = service.calendarList().list().execute()
|
| 103 |
calendars = calendar_list_result.get('items', [])
|
| 104 |
descrizione_eventi = ''
|
| 105 |
links = []
|
|
|
|
|
|
|
| 106 |
for calendar in calendars:
|
| 107 |
-
events_result = (service.events().list(calendarId=calendar['id'], timeMin=
|
| 108 |
events = events_result.get("items", [])
|
| 109 |
for event in events:
|
| 110 |
start = event["start"].get("dateTime", event["start"].get("date"))
|
|
@@ -129,6 +205,8 @@ def leggi_calendario_google(max_results=10):
|
|
| 129 |
links.append((f'Calendario: {calendario}', descrizione_link))
|
| 130 |
except HttpError as error:
|
| 131 |
print(f"An error occurred: {error}")
|
|
|
|
|
|
|
| 132 |
return descrizione_eventi, links
|
| 133 |
|
| 134 |
#---------------------------------------------------------- CONNESSIONE ACCOUNT GOOGLE ---------------------------------------------------------
|
|
|
|
| 21 |
"https://www.googleapis.com/auth/calendar.readonly",
|
| 22 |
"https://www.googleapis.com/auth/documents.readonly",
|
| 23 |
"https://www.googleapis.com/auth/forms.body",
|
| 24 |
+
"https://www.googleapis.com/auth/spreadsheets.readonly",
|
| 25 |
+
"https://www.googleapis.com/auth/drive"]
|
| 26 |
|
| 27 |
#---------------------------------------------------------- LETTURA EMAIL ---------------------------------------------------------
|
| 28 |
def converti_email_txt(body):
|
|
|
|
| 34 |
body = body
|
| 35 |
return body
|
| 36 |
|
| 37 |
+
def leggi_gmail(max_results=10, data_inizio = None, data_fine = None):
|
| 38 |
links = []
|
| 39 |
service = build("gmail", "v1", credentials=st.session_state.creds)
|
| 40 |
+
start_date_str = data_inizio.strftime("%Y/%m/%d")
|
| 41 |
+
end_date_str = data_fine.strftime("%Y/%m/%d")
|
| 42 |
+
query = f"is:unread after:{start_date_str} before:{end_date_str}"
|
| 43 |
+
results = service.users().messages().list(userId="me", labelIds=["INBOX"], q=query, maxResults=max_results).execute()
|
| 44 |
messages = results.get("messages", [])
|
| 45 |
testo_email = ''
|
| 46 |
if not messages:
|
|
|
|
| 72 |
body = converti_email_txt(body)
|
| 73 |
break
|
| 74 |
else:
|
| 75 |
+
if "data" in msg["payload"]["body"]:
|
| 76 |
+
body = base64.urlsafe_b64decode(msg["payload"]["body"]["data"]).decode("utf-8")
|
| 77 |
+
body = converti_email_txt(body)
|
| 78 |
testo_email += 'Data Ricezione: ' + received_date[5:] + '\nMittente: ' + from_name + '\nOggetto: ' + ', '.join(subject) + '\nTesto Email: ' + body + '\n---------------------------------------------------\n'
|
| 79 |
links.append(('Mittente: ' + from_name, 'Data: ' + received_date[5:] + '\n\nOggetto: ' + ', '.join(subject)))
|
| 80 |
+
if testo_email == '':
|
| 81 |
+
testo_email = 'Non sono presenti email nell intervallo di date specificate! Riprova con altre date'
|
| 82 |
return testo_email, links
|
| 83 |
|
| 84 |
#---------------------------------------------------------- SCRITTURA BOZZA EMAIL ---------------------------------------------------------
|
| 85 |
def scrivi_bozza_gmail(testo):
|
| 86 |
+
draft_url = 'https://mail.google.com/mail/u/0/#drafts'
|
| 87 |
try:
|
| 88 |
service = build("gmail", "v1", credentials=st.session_state.creds)
|
| 89 |
message = EmailMessage()
|
|
|
|
| 100 |
print(draft_details)
|
| 101 |
except HttpError as error:
|
| 102 |
print(f"An error occurred: {error}")
|
| 103 |
+
return draft_url
|
| 104 |
+
|
| 105 |
+
#---------------------------------------------------------- CREA DOCUMENTO GOOGLE ---------------------------------------------------------
|
| 106 |
+
def crea_documento_google(testo):
|
| 107 |
+
drive_service = build('drive', 'v3', credentials=st.session_state.creds)
|
| 108 |
+
docs_service = build('docs', 'v1', credentials=st.session_state.creds)
|
| 109 |
+
file_metadata = {'name': 'Documento di Bonsi AI', 'mimeType': 'application/vnd.google-apps.document'}
|
| 110 |
+
new_document = drive_service.files().create(body=file_metadata).execute()
|
| 111 |
+
document_id = new_document['id']
|
| 112 |
+
|
| 113 |
+
title_text = "Documento di Bonsi AI"
|
| 114 |
+
title_index = 1
|
| 115 |
+
bold_title_request = {
|
| 116 |
+
"insertText": {
|
| 117 |
+
"location": {
|
| 118 |
+
"index": title_index,
|
| 119 |
+
},
|
| 120 |
+
"text": title_text
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
bold_request = {
|
| 124 |
+
"updateTextStyle": {
|
| 125 |
+
"range": {
|
| 126 |
+
"startIndex": title_index,
|
| 127 |
+
"endIndex": title_index + len(title_text)
|
| 128 |
+
},
|
| 129 |
+
"textStyle": {
|
| 130 |
+
"bold": True,
|
| 131 |
+
"fontSize": {
|
| 132 |
+
"magnitude": 20,
|
| 133 |
+
"unit": "PT"
|
| 134 |
+
}
|
| 135 |
+
},
|
| 136 |
+
"fields": "bold,fontSize"
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
normal_text = "\n\n" + testo
|
| 141 |
+
normal_text_index = title_index + len(title_text)
|
| 142 |
+
normal_text_request = {
|
| 143 |
+
"insertText": {
|
| 144 |
+
"location": {
|
| 145 |
+
"index": normal_text_index,
|
| 146 |
+
},
|
| 147 |
+
"text": normal_text
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
normal_request = {
|
| 151 |
+
"updateTextStyle": {
|
| 152 |
+
"range": {
|
| 153 |
+
"startIndex": normal_text_index,
|
| 154 |
+
"endIndex": normal_text_index + len(normal_text)
|
| 155 |
+
},
|
| 156 |
+
"textStyle": {
|
| 157 |
+
"bold": False,
|
| 158 |
+
"fontSize": {
|
| 159 |
+
"magnitude": 12,
|
| 160 |
+
"unit": "PT"
|
| 161 |
+
}
|
| 162 |
+
},
|
| 163 |
+
"fields": "bold,fontSize"
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
docs_service.documents().batchUpdate(documentId=document_id, body={
|
| 168 |
+
'requests': [bold_title_request, bold_request, normal_text_request, normal_request]
|
| 169 |
+
}).execute()
|
| 170 |
+
return 'https://docs.google.com/document/d/' + document_id
|
| 171 |
|
| 172 |
#---------------------------------------------------------- LEGGI GOOGLE CALENDAR ---------------------------------------------------------
|
| 173 |
+
def leggi_calendario_google(max_results=10, data_inizio = None, data_fine = None):
|
| 174 |
try:
|
| 175 |
service = build("calendar", "v3", credentials=st.session_state.creds)
|
| 176 |
calendar_list_result = service.calendarList().list().execute()
|
| 177 |
calendars = calendar_list_result.get('items', [])
|
| 178 |
descrizione_eventi = ''
|
| 179 |
links = []
|
| 180 |
+
timeMin = datetime.datetime.combine(data_inizio, datetime.time()).isoformat() + 'Z'
|
| 181 |
+
timeMax = datetime.datetime.combine(data_fine, datetime.time(23, 59, 59)).isoformat() + 'Z'
|
| 182 |
for calendar in calendars:
|
| 183 |
+
events_result = (service.events().list(calendarId=calendar['id'], timeMin=timeMin, timeMax = timeMax, maxResults=max_results, singleEvents=True, orderBy="startTime", ).execute())
|
| 184 |
events = events_result.get("items", [])
|
| 185 |
for event in events:
|
| 186 |
start = event["start"].get("dateTime", event["start"].get("date"))
|
|
|
|
| 205 |
links.append((f'Calendario: {calendario}', descrizione_link))
|
| 206 |
except HttpError as error:
|
| 207 |
print(f"An error occurred: {error}")
|
| 208 |
+
if descrizione_eventi == '':
|
| 209 |
+
descrizione_eventi = 'Non sono presenti eventi nel calendario per le date specificate. Riprova con altre date!'
|
| 210 |
return descrizione_eventi, links
|
| 211 |
|
| 212 |
#---------------------------------------------------------- CONNESSIONE ACCOUNT GOOGLE ---------------------------------------------------------
|