from __future__ import with_statement
+import itertools
import logging
-import gvoice
+from gvoice import gvoice
+from util import io as io_utils
-_moduleLogger = logging.getLogger("gv_backend")
+
+_moduleLogger = logging.getLogger(__name__)
class GVDialer(object):
+ MESSAGE_TEXTS = "Text"
+ MESSAGE_VOICEMAILS = "Voicemail"
+ MESSAGE_ALL = "All"
+
def __init__(self, cookieFile = None):
self._gvoice = gvoice.GVoiceBackend(cookieFile)
-
- self._contacts = None
+ self._texts = []
+ self._voicemails = []
def is_quick_login_possible(self):
"""
- @returns True then is_authed might be enough to login, else full login is required
+ @returns True then refresh_account_info might be enough to login, else full login is required
"""
return self._gvoice.is_quick_login_possible()
- def is_authed(self, force = False):
- """
- Attempts to detect a current session
- @note Once logged in try not to reauth more than once a minute.
- @returns If authenticated
- """
- return self._gvoice.is_authed(force)
+ def refresh_account_info(self):
+ return self._gvoice.refresh_account_info()
def login(self, username, password):
"""
def logout(self):
return self._gvoice.logout()
+ def persist(self):
+ return self._gvoice.persist()
+
def is_dnd(self):
return self._gvoice.is_dnd()
"""
return self._gvoice.cancel(outgoingNumber)
- def send_sms(self, phoneNumber, message):
- self._gvoice.send_sms(phoneNumber, message)
+ def send_sms(self, phoneNumbers, message):
+ self._gvoice.send_sms(phoneNumbers, message)
def search(self, query):
"""
"""
@returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
"""
- return self._gvoice.get_recent()
+ return list(self._gvoice.get_recent())
- def get_contacts(self):
- """
- @returns Iterable of (contact id, contact name)
- """
- self._update_contacts_cache()
- contactsToSort = [
- (contactDetails["name"], contactId)
- for contactId, contactDetails in self._contacts.iteritems()
- ]
- contactsToSort.sort()
- return (
- (contactId, contactName)
- for (contactName, contactId) in contactsToSort
- )
+ def get_messages(self, messageType):
+ messages = list(self._get_messages(messageType))
+ messages.sort(key=lambda message: message["time"])
+ return messages
- def get_contact_details(self, contactId):
- """
- @returns Iterable of (Phone Type, Phone Number)
- """
- if self._contacts is None:
- self._update_contacts_cache()
- contactDetails = self._contacts[contactId]
- return (
- (number.get("phoneType", ""), number["phoneNumber"])
- for number in contactDetails["numbers"]
- )
+ def _get_messages(self, messageType):
+ if messageType in [self.MESSAGE_VOICEMAILS, self.MESSAGE_ALL] or not self._voicemails:
+ self._voicemails = list(self._gvoice.get_voicemails())
+ voicemails = self._voicemails
+ if messageType in [self.MESSAGE_TEXTS, self.MESSAGE_ALL] or not self._texts:
+ self._texts = list(self._gvoice.get_texts())
+ smss = self._texts
- def get_messages(self):
- conversations = self._gvoice.get_conversations()
+ conversations = itertools.chain(voicemails, smss)
for conversation in conversations:
messages = conversation.messages
- messageParts = (
+ messageParts = [
(message.whoFrom, self._format_message(message), message.when)
for message in messages
- )
+ ]
messageDetails = {
"id": conversation.id,
def factory_name():
return "Google Voice"
- def _update_contacts_cache(self):
- self._contacts = dict(self._gvoice.get_contacts())
-
def _format_message(self, message):
messagePartFormat = {
"med1": "<i>%s</i>",
"high": "<b>%s</b>",
}
return " ".join(
- messagePartFormat[text.accuracy] % text.text
+ messagePartFormat[text.accuracy] % io_utils.escape(text.text)
for text in message.body
)