import os.path import json import base64 import re from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from googleapiclient.discovery import build import os import openai from google.oauth2.credentials import Credentials from google.auth.transport.requests import Request #from google.auth.transport.urllib3 import Request from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build #import gspread from datetime import datetime, timedelta import base64 import json from dateutil.parser import parse as parse_date import traceback # If modifying these SCOPES, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] # Get the emails sent by me where the response from the recipient was more than 5 days ago or no response def get_old_emails(gmail_service): #query = f'from:{my_email}' query = f'from:me' # days = 7 # today = datetime.utcnow() # start_date = today - timedelta(days=days+2) # Considering 2 weekend days # query = f'from:me after:{start_date.strftime("%Y/%m/%d")}' result = gmail_service.users().messages().list(userId='me', q=query).execute() messages = result.get('messages', []) old_emails = [] for msg in messages: message = gmail_service.users().messages().get(userId='me', id=msg['id']).execute() headers = message['payload']['headers'] thread_id = message['threadId'] date_str = next(header['value'] for header in headers if header['name'] == 'Date') #date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S %z') try: date = parse_date(date_str) except ValueError: print(f"Error parsing date: {date_str}") continue if (datetime.now(date.tzinfo) - date).days > 5: thread = gmail_service.users().threads().get(userId='me', id=thread_id).execute() messages_in_thread = thread['messages'] if len(messages_in_thread) == 1 or all(msg['labelIds'] == ['SENT'] for msg in messages_in_thread[1:]): old_emails.append(thread_id) return old_emails def authenticate_gmail(): """Shows basic usage of the Gmail API. Lists the user's Gmail labels. """ creds = None # 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) # 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 build('gmail', 'v1', credentials=creds) def get_thread(service, user_id, thread_id): thread = service.users().threads().get(userId=user_id, id=thread_id).execute() messages = thread['messages'] thread_data = [] # Process the last 3 emails in the thread for msg in messages[-3:]: msg_data = { 'id': msg['id'], 'date': '', 'sender': '', 'body': '' } # Get email details headers = msg['payload']['headers'] for header in headers: if header['name'] == 'Date': msg_data['date'] = header['value'] if header['name'] == 'From': msg_data['sender'] = header['value'] # Get email body if 'parts' in msg['payload']: parts = msg['payload']['parts'] for part in parts: if part['mimeType'] == 'text/plain': body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8') msg_data['body'] = body else: body = base64.urlsafe_b64decode(msg['payload']['body']['data']).decode('utf-8') msg_data['body'] = body thread_data.append(msg_data) return thread_data def save_to_json(data, filename='email_thread.json'): with open(filename, 'w') as f: json.dump(data, f, indent=4) def main(): service = authenticate_gmail() user_id = 'me' old_emails = get_old_emails(service) for thread_id in old_emails: #thread_id = 'your-thread-id-here' # Replace with the actual thread ID thread_data = get_thread(service, user_id, thread_id) print(thread_data) main()