Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -260,7 +260,96 @@ async def stop_processing():
|
|
260 |
status = "Stopped"
|
261 |
return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
|
262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
264 |
print('starting')
|
265 |
logging.info('starting project!...')
|
266 |
# running = True
|
|
|
260 |
status = "Stopped"
|
261 |
return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
|
262 |
|
263 |
+
|
264 |
+
import os.path
|
265 |
+
import base64
|
266 |
+
import json
|
267 |
+
import pickle
|
268 |
+
from google.auth.transport.requests import Request
|
269 |
+
from google.oauth2.credentials import Credentials
|
270 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
271 |
+
from googleapiclient.discovery import build
|
272 |
+
from googleapiclient.errors import HttpError
|
273 |
+
|
274 |
+
# If modifying these SCOPES, delete the file token.json.
|
275 |
+
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
|
276 |
+
|
277 |
+
def authenticate_gmail():
|
278 |
+
"""Shows basic usage of the Gmail API.
|
279 |
+
Lists the user's Gmail labels.
|
280 |
+
"""
|
281 |
+
creds = None
|
282 |
+
# The file token.json stores the user's access and refresh tokens, and is
|
283 |
+
# created automatically when the authorization flow completes for the first time.
|
284 |
+
if os.path.exists('token.json'):
|
285 |
+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
286 |
+
# If there are no (valid) credentials available, let the user log in.
|
287 |
+
if not creds or not creds.valid:
|
288 |
+
if creds and creds.expired and creds.refresh_token:
|
289 |
+
creds.refresh(Request())
|
290 |
+
else:
|
291 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
292 |
+
'Credentials.json', SCOPES)
|
293 |
+
creds = flow.run_local_server(port=0)
|
294 |
+
# Save the credentials for the next run
|
295 |
+
with open('token.json', 'w') as token:
|
296 |
+
token.write(creds.to_json())
|
297 |
+
|
298 |
+
try:
|
299 |
+
# Call the Gmail API
|
300 |
+
service = build('gmail', 'v1', credentials=creds)
|
301 |
+
return service
|
302 |
+
except HttpError as error:
|
303 |
+
print(f'An error occurred: {error}')
|
304 |
+
return None
|
305 |
+
|
306 |
+
def get_latest_email(service):
|
307 |
+
"""Get the latest email from the inbox"""
|
308 |
+
try:
|
309 |
+
# Retrieve a list of messages
|
310 |
+
results = service.users().messages().list(userId='me', maxResults=1).execute()
|
311 |
+
messages = results.get('messages', [])
|
312 |
+
|
313 |
+
if not messages:
|
314 |
+
print('No messages found.')
|
315 |
+
return
|
316 |
+
|
317 |
+
# Get the message ID
|
318 |
+
message_id = messages[0]['id']
|
319 |
+
|
320 |
+
# Fetch the email content by message ID
|
321 |
+
message = service.users().messages().get(userId='me', id=message_id, format='full').execute()
|
322 |
+
|
323 |
+
# Get the email payload
|
324 |
+
payload = message['payload']
|
325 |
+
headers = payload.get('headers', [])
|
326 |
+
|
327 |
+
# Print the sender, subject, and body of the email
|
328 |
+
for header in headers:
|
329 |
+
if header['name'] == 'From':
|
330 |
+
print(f"From: {header['value']}")
|
331 |
+
if header['name'] == 'Subject':
|
332 |
+
print(f"Subject: {header['value']}")
|
333 |
+
|
334 |
+
# Extract the body
|
335 |
+
parts = payload.get('parts', [])
|
336 |
+
for part in parts:
|
337 |
+
if part.get('mimeType') == 'text/plain':
|
338 |
+
body = part.get('body', {}).get('data', '')
|
339 |
+
body_decoded = base64.urlsafe_b64decode(body).decode('utf-8')
|
340 |
+
print(f"Body: {body_decoded}")
|
341 |
+
|
342 |
+
except HttpError as error:
|
343 |
+
print(f'An error occurred: {error}')
|
344 |
+
|
345 |
+
|
346 |
+
|
347 |
+
|
348 |
if __name__ == "__main__":
|
349 |
+
service = authenticate_gmail()
|
350 |
+
print(service)
|
351 |
+
if service:
|
352 |
+
get_latest_email(service)
|
353 |
print('starting')
|
354 |
logging.info('starting project!...')
|
355 |
# running = True
|