Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to gmail-api #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions general/gmail-api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
return messages
59 changes: 57 additions & 2 deletions general/gmail-api/mark_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -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={
Expand All @@ -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)