forked from x4nth055/pythoncode-tutorials
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail_api.py
More file actions
Latest commit
45 lines (43 loc) · 2.29 KB
/
Copy pathgmail_api.py
File metadata and controls
45 lines (43 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# for parsing commandline arguments
importargparse
fromcommonimportsearch_messages, gmail_authenticate
fromread_emailsimportread_message
fromsend_emailsimportsend_message
fromdelete_emailsimportdelete_messages
frommark_emailsimportmark_as_read, mark_as_unread
if__name__=='__main__':
parser=argparse.ArgumentParser(description="Send/Search/Delete/Mark messages using gmail's API.")
subparsers=parser.add_subparsers(help='Subcommands')
parser_1=subparsers.add_parser('send', help='Send an email')
parser_1.add_argument('destination', type=str, help='The destination email address')
parser_1.add_argument('subject', type=str, help='The subject of the email')
parser_1.add_argument('body', type=str, help='The body of the email')
parser_1.add_argument('files', type=str, help='email attachments', nargs='+')
parser_1.set_defaults(action='send')
parser_2=subparsers.add_parser('delete', help='Delete a set of emails')
parser_2.add_argument('query', type=str, help='a search query that selects emails to delete')
parser_2.set_defaults(action='delete')
parser_3=subparsers.add_parser('mark', help='Marks a set of emails as read or unread')
parser_3.add_argument('query', type=str, help='a search query that selects emails to mark')
parser_3.add_argument('read_status', type=bool, help='Whether to mark the message as unread, or as read')
parser_3.set_defaults(action='mark')
parser_4=subparsers.add_parser('search', help='Marks a set of emails as read or unread')
parser_4.add_argument('query', type=str, help='a search query, which messages to display')
parser_4.set_defaults(action='search')
args=parser.parse_args()
service=gmail_authenticate()
ifargs.action=='send':
# TODO: add attachements
send_message(service, args.destination, args.subject, args.body, args.files)
elifargs.action=='delete':
delete_messages(service, args.query)
elifargs.action=='mark':
print(args.unread_status)
ifargs.read_status:
mark_as_read(service, args.query)
else:
mark_as_unread(service, args.query)
elifargs.action=='search':
results=search_messages(service, args.query)
formsginresults:
read_message(service, msg)