FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Updates to gmail-api by arne182 · Pull Request #55 · x4nth055/pythoncode-tutorials · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions general/gmail-api/common.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)

Back | FazBrowse Home | New Git URL