-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlistmessages.py
181 lines (136 loc) · 4.89 KB
/
listmessages.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
"""Get a list of Messages from the user's mailbox.
"""
from text_speech import *
from apiclient import errors
from authenticate import *
from getmessage import *
import html2text
import pyttsx
def ListMessagesMatchingQuery(service, user_id, query=''):
"""List all Messages of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'from:user@some_domain.com' for Messages from a particular sender.
Returns:
List of Messages that match the criteria of the query. Note that the
returned list contains Message IDs, you must use get with the
appropriate ID to get the details of a Message.
"""
try:
response = service.users().messages().list(userId=user_id,
q=query).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id, q=query,
pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
except errors.HttpError, error:
print 'An error occurred: %s' % error
def ListMessagesWithLabels(service, user_id, label_ids=[]):
"""List all Messages of the user's mailbox with label_ids applied.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
label_ids: Only return Messages with these labelIds applied.
Returns:
List of Messages that have all required Labels applied. Note that the
returned list contains Message IDs, you must use get with the
appropriate id to get the details of a Message.
"""
try:
response = service.users().messages().list(userId=user_id,
labelIds=label_ids).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id,
labelIds=label_ids,
pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
except errors.HttpError, error:
print 'An error occurred: %s' % error
def speaking_message(msg_id):
message=GetMimeMessage(service,"me",msg_id)
a=message.get_payload()
b=a[0].get_payload()
print type(b)
text_to_speech('The subject is : ')
text_to_speech(message['subject'])
print b
text_to_speech('The message is : ')
text_to_speech(b)
def searched_message(query):
number=0
searched_message_list=ListMessagesMatchingQuery(service, 'me', 'from:'+query)
searched_messageIDs=list()
while number < len(searched_message_list) :
searched_messageIDs.append(searched_message_list[number]['id'])
number=number+1
subjects=list()
for current_mailID in searched_messageIDs :
searched_message=GetMimeMessage(service,"me",current_mailID)
subjects.append(searched_message['subject'])
number=0
for subject in subjects:
text_to_speech(str(number)+' '+subject)
number=number+1
if len(subjects) == 0 :
text_to_speech('No E-Mails from this sender!')
return
response=speech_to_text()
#message=GetMimeMessage(service,'me',searched_messageIDs[int(response)])
speaking_message(searched_messageIDs[int(response)])
def get_message_list():
maillist=ListMessagesMatchingQuery(service, 'me')
number=0
messageIDs=list()
while number < 100 :
messageIDs.append(maillist[number]['id'])
number=number + 1
number=0
senders=list()
while number < 100 :
current_message=GetMimeMessage(service,"me",messageIDs[number])
senders.append(current_message['from'])
number=number+1
number=0
while True :
text_to_speech('Say the serial number of E-Mail to read !')
#number=0
#while number < 10 :
while True :
text_to_speech(str(number)+' '+senders[number])
#number=number+1
if number%10 == 9 :
#number=number+1
break
number=number+1
text_to_speech('say next to hear next ten messages')
text_to_speech('say skip to return to the main menu')
response=speech_to_text()
if response.lower() == 'skip' :
return
#response=int(response)
if response.lower() == 'next' :
number=number+1
continue
response=int(response)
speaking_message(messageIDs[response])
text_to_speech('say continue to hear the E-Mails again !')
response=speech_to_text()
if response.lower() == 'continue' :
number=number-10
continue