From c33afeafd13d255a2f7999adb67c4ef4e3b4d1d9 Mon Sep 17 00:00:00 2001 From: Arne Schwarck Date: Wed, 24 Feb 2021 11:30:15 +0100 Subject: [PATCH 1/2] no error when search returns no results --- general/gmail-api/mark_emails.py | 59 ++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) 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) From 0722227364d994549fda3a1e20a7645b3a5bca5a Mon Sep 17 00:00:00 2001 From: Arne Schwarck Date: Wed, 24 Feb 2021 11:31:07 +0100 Subject: [PATCH 2/2] SCOPES must be the same to refresh --- general/gmail-api/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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