diff --git a/general/gmail-api/common.py b/general/gmail-api/common.py index 2e37f7be..3c19987c 100644 --- a/general/gmail-api/common.py +++ b/general/gmail-api/common.py @@ -19,7 +19,7 @@ def gmail_authenticate(): creds = pickle.load(token) # if there are no (valid) credentials availablle, let the user log in. if not creds or not creds.valid: - if creds and creds.expired and creds.refresh_token: + if creds and creds.expired and creds.refresh_token and SCOPES == creds._scopes:: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) @@ -40,4 +40,4 @@ def search_messages(service, query): result = service.users().messages().list(userId='me',q=query, pageToken=page_token).execute() if 'messages' in result: messages.extend(result['messages']) - return messages \ No newline at end of file + return messages diff --git a/general/gmail-api/mark_emails.py b/general/gmail-api/mark_emails.py index e76cef94..37180ec5 100644 --- a/general/gmail-api/mark_emails.py +++ b/general/gmail-api/mark_emails.py @@ -2,6 +2,33 @@ def mark_as_read(service, query): messages_to_mark = search_messages(service, query) + if len(messages_to_mark) == 0: # No emails found + return print("No emails found") + else: + print("="*50) + for message_id in messages_to_mark: + msg = service.users().messages().get(userId='me', id=message_id['id'], format='full').execute() + payload = msg['payload'] + headers = payload.get("headers") + if headers: + # this section prints email basic info & creates a folder for the email + for header in headers: + name = header.get("name") + value = header.get("value") + if name == 'From': + # we print the From address + print("From:", value) + if name == "To": + # we print the To address + print("To:", value) + if name == "Subject": + # we print the Subject + print("Subject:", value) + if name == "Date": + # we print the date when the message was sent + print("Date:", value) + print("="*50) + print("MARKED AS READ") return service.users().messages().batchModify( userId='me', body={ @@ -12,6 +39,33 @@ def mark_as_read(service, query): def mark_as_unread(service, query): messages_to_mark = search_messages(service, query) + if len(messages_to_mark) == 0: # No emails found + return print("No emails found") + else: + print("="*50) + for message_id in messages_to_mark: + msg = service.users().messages().get(userId='me', id=message_id['id'], format='full').execute() + payload = msg['payload'] + headers = payload.get("headers") + if headers: + # this section prints email basic info & creates a folder for the email + for header in headers: + name = header.get("name") + value = header.get("value") + if name == 'From': + # we print the From address + print("From:", value) + if name == "To": + # we print the To address + print("To:", value) + if name == "Subject": + # we print the Subject + print("Subject:", value) + if name == "Date": + # we print the date when the message was sent + print("Date:", value) + print("="*50) + print("MARKED AS UNREAD") return service.users().messages().batchModify( userId='me', body={ @@ -27,9 +81,10 @@ def mark_as_unread(service, query): parser.add_argument("-r", "--read", action="store_true", help='Whether to mark the message as read') parser.add_argument("-u", "--unread", action="store_true", help='Whether to mark the message as unread') - args = parser.parse_args() service = gmail_authenticate() + + args = parser.parse_args() if args.read: - mark_as_read(service, args.query) + mark_as_read(service, '"' + args.query + '" and label:unread' ) elif args.unread: mark_as_unread(service, args.query)