Sorting of messages
[gc-dialer] / src / backends / gv_backend.py
1 #!/usr/bin/python
2
3 """
4 DialCentral - Front end for Google's GoogleVoice service.
5 Copyright (C) 2008  Eric Warnke ericew AT gmail DOT com
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21 Google Voice backend code
22
23 Resources
24         http://thatsmith.com/2009/03/google-voice-addon-for-firefox/
25         http://posttopic.com/topic/google-voice-add-on-development
26 """
27
28 from __future__ import with_statement
29
30 import itertools
31 import logging
32
33 from gvoice import gvoice
34
35 from util import io as io_utils
36
37
38 _moduleLogger = logging.getLogger(__name__)
39
40
41 class GVDialer(object):
42
43         def __init__(self, cookieFile = None):
44                 self._gvoice = gvoice.GVoiceBackend(cookieFile)
45
46         def is_quick_login_possible(self):
47                 """
48                 @returns True then refresh_account_info might be enough to login, else full login is required
49                 """
50                 return self._gvoice.is_quick_login_possible()
51
52         def refresh_account_info(self):
53                 return self._gvoice.refresh_account_info()
54
55         def login(self, username, password):
56                 """
57                 Attempt to login to GoogleVoice
58                 @returns Whether login was successful or not
59                 """
60                 return self._gvoice.login(username, password)
61
62         def logout(self):
63                 return self._gvoice.logout()
64
65         def persist(self):
66                 return self._gvoice.persist()
67
68         def is_dnd(self):
69                 return self._gvoice.is_dnd()
70
71         def set_dnd(self, doNotDisturb):
72                 return self._gvoice.set_dnd(doNotDisturb)
73
74         def call(self, outgoingNumber):
75                 """
76                 This is the main function responsible for initating the callback
77                 """
78                 return self._gvoice.call(outgoingNumber)
79
80         def cancel(self, outgoingNumber=None):
81                 """
82                 Cancels a call matching outgoing and forwarding numbers (if given). 
83                 Will raise an error if no matching call is being placed
84                 """
85                 return self._gvoice.cancel(outgoingNumber)
86
87         def send_sms(self, phoneNumbers, message):
88                 self._gvoice.send_sms(phoneNumbers, message)
89
90         def search(self, query):
91                 """
92                 Search your Google Voice Account history for calls, voicemails, and sms
93                 Returns ``Folder`` instance containting matching messages
94                 """
95                 return self._gvoice.search(query)
96
97         def get_feed(self, feed):
98                 return self._gvoice.get_feed(feed)
99
100         def download(self, messageId, adir):
101                 """
102                 Download a voicemail or recorded call MP3 matching the given ``msg``
103                 which can either be a ``Message`` instance, or a SHA1 identifier. 
104                 Saves files to ``adir`` (defaults to current directory). 
105                 Message hashes can be found in ``self.voicemail().messages`` for example. 
106                 Returns location of saved file.
107                 """
108                 return self._gvoice.download(messageId, adir)
109
110         def is_valid_syntax(self, number):
111                 """
112                 @returns If This number be called ( syntax validation only )
113                 """
114                 return self._gvoice.is_valid_syntax(number)
115
116         def get_account_number(self):
117                 """
118                 @returns The GoogleVoice phone number
119                 """
120                 return self._gvoice.get_account_number()
121
122         def get_callback_numbers(self):
123                 """
124                 @returns a dictionary mapping call back numbers to descriptions
125                 @note These results are cached for 30 minutes.
126                 """
127                 return self._gvoice.get_callback_numbers()
128
129         def set_callback_number(self, callbacknumber):
130                 """
131                 Set the number that GoogleVoice calls
132                 @param callbacknumber should be a proper 10 digit number
133                 """
134                 return self._gvoice.set_callback_number(callbacknumber)
135
136         def get_callback_number(self):
137                 """
138                 @returns Current callback number or None
139                 """
140                 return self._gvoice.get_callback_number()
141
142         def get_recent(self):
143                 """
144                 @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
145                 """
146                 return list(self._gvoice.get_recent())
147
148         def get_messages(self):
149                 messages = list(self._get_messages())
150                 messages.sort(key=lambda message: message["time"])
151                 return messages
152
153         def _get_messages(self):
154                 voicemails = self._gvoice.get_voicemails()
155                 smss = self._gvoice.get_texts()
156                 conversations = itertools.chain(voicemails, smss)
157                 for conversation in conversations:
158                         messages = conversation.messages
159                         messageParts = [
160                                 (message.whoFrom, self._format_message(message), message.when)
161                                 for message in messages
162                         ]
163
164                         messageDetails = {
165                                 "id": conversation.id,
166                                 "contactId": conversation.contactId,
167                                 "name": conversation.name,
168                                 "time": conversation.time,
169                                 "relTime": conversation.relTime,
170                                 "prettyNumber": conversation.prettyNumber,
171                                 "number": conversation.number,
172                                 "location": conversation.location,
173                                 "messageParts": messageParts,
174                                 "type": conversation.type,
175                                 "isRead": conversation.isRead,
176                                 "isTrash": conversation.isTrash,
177                                 "isSpam": conversation.isSpam,
178                                 "isArchived": conversation.isArchived,
179                         }
180                         yield messageDetails
181
182         def clear_caches(self):
183                 pass
184
185         def get_addressbooks(self):
186                 """
187                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
188                 """
189                 yield self, "", ""
190
191         def open_addressbook(self, bookId):
192                 return self
193
194         @staticmethod
195         def contact_source_short_name(contactId):
196                 return "GV"
197
198         @staticmethod
199         def factory_name():
200                 return "Google Voice"
201
202         def _format_message(self, message):
203                 messagePartFormat = {
204                         "med1": "<i>%s</i>",
205                         "med2": "%s",
206                         "high": "<b>%s</b>",
207                 }
208                 return " ".join(
209                         messagePartFormat[text.accuracy] % io_utils.escape(text.text)
210                         for text in message.body
211                 )
212
213
214 def sort_messages(allMessages):
215         sortableAllMessages = [
216                 (message["time"], message)
217                 for message in allMessages
218         ]
219         sortableAllMessages.sort(reverse=True)
220         return (
221                 message
222                 for (exactTime, message) in sortableAllMessages
223         )
224
225
226 def decorate_recent(recentCallData):
227         """
228         @returns (personsName, phoneNumber, date, action)
229         """
230         contactId = recentCallData["contactId"]
231         if recentCallData["name"]:
232                 header = recentCallData["name"]
233         elif recentCallData["prettyNumber"]:
234                 header = recentCallData["prettyNumber"]
235         elif recentCallData["location"]:
236                 header = recentCallData["location"]
237         else:
238                 header = "Unknown"
239
240         number = recentCallData["number"]
241         relTime = recentCallData["relTime"]
242         action = recentCallData["action"]
243         return contactId, header, number, relTime, action
244
245
246 def decorate_message(messageData):
247         contactId = messageData["contactId"]
248         exactTime = messageData["time"]
249         if messageData["name"]:
250                 header = messageData["name"]
251         elif messageData["prettyNumber"]:
252                 header = messageData["prettyNumber"]
253         else:
254                 header = "Unknown"
255         number = messageData["number"]
256         relativeTime = messageData["relTime"]
257
258         messageParts = list(messageData["messageParts"])
259         if len(messageParts) == 0:
260                 messages = ("No Transcription", )
261         elif len(messageParts) == 1:
262                 messages = (messageParts[0][1], )
263         else:
264                 messages = [
265                         "<b>%s</b>: %s" % (messagePart[0], messagePart[1])
266                         for messagePart in messageParts
267                 ]
268
269         decoratedResults = contactId, header, number, relativeTime, messages
270         return decoratedResults