3 # DialCentral - Front end for Google's Grand Central service.
4 # Copyright (C) 2008 Eric Warnke ericew AT gmail DOT com
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 Grandcentral backend code
32 from browser_emu import MozillaEmulator
37 socket.setdefaulttimeout(5)
40 class GCDialer(object):
42 This class encapsulates all of the knowledge necessary to interace with the grandcentral servers
43 the functions include login, setting up a callback number, and initalting a callback
46 _gcDialingStrRe = re.compile("This may take a few seconds", re.M)
47 _accessTokenRe = re.compile(r"""<input type="hidden" name="a_t" [^>]*value="(.*)"/>""")
48 _isLoginPageRe = re.compile(r"""<form method="post" action="https://www.grandcentral.com/mobile/account/login">""")
49 _callbackRe = re.compile(r"""name="default_number" value="(\d+)" />\s+(.*)\s$""", re.M)
50 _accountNumRe = re.compile(r"""<img src="/images/mobile/inbox_logo.gif" alt="GrandCentral" />\s*(.{14})\s* """, re.M)
51 _inboxRe = re.compile(r"""<td>.*?(voicemail|received|missed|call return).*?</td>\s+<td>\s+<font size="2">\s+(.*?)\s+ \| \s+<a href="/mobile/contacts/.*?">(.*?)\s?</a>\s+<br/>\s+(.*?)\s?<a href=""", re.S)
52 _contactsRe = re.compile(r"""<a href="/mobile/contacts/detail/(\d+)">(.*?)</a>""", re.S)
53 _contactsNextRe = re.compile(r""".*<a href="/mobile/contacts(\?page=\d+)">Next</a>""", re.S)
54 _contactDetailGroupRe = re.compile(r"""Group:\s*(\w*)""", re.S)
55 _contactDetailPhoneRe = re.compile(r"""(\w+):[0-9\-\(\) \t]*?<a href="/mobile/calls/click_to_call\?destno=(\d+).*?">call</a>""", re.S)
57 _validateRe = re.compile("^[0-9]{10,}$")
59 _forwardselectURL = "http://www.grandcentral.com/mobile/settings/forwarding_select"
60 _loginURL = "https://www.grandcentral.com/mobile/account/login"
61 _setforwardURL = "http://www.grandcentral.com/mobile/settings/set_forwarding?from=settings"
62 _clicktocallURL = "http://www.grandcentral.com/mobile/calls/click_to_call?a_t=%s&destno=%s"
63 _inboxallURL = "http://www.grandcentral.com/mobile/messages/inbox?types=all"
64 _contactsURL = "http://www.grandcentral.com/mobile/contacts"
65 _contactDetailURL = "http://www.grandcentral.com/mobile/contacts/detail"
67 def __init__(self, cookieFile = None):
68 # Important items in this function are the setup of the browser emulation and cookie file
69 self._browser = MozillaEmulator(None, 0)
70 if cookieFile is None:
71 cookieFile = os.path.join(os.path.expanduser("~"), ".gc_cookies.txt")
72 self._browser.cookies.filename = cookieFile
73 if os.path.isfile(cookieFile):
74 self._browser.cookies.load()
76 self._accessToken = None
77 self._accountNum = None
78 self._callbackNumbers = {}
79 self._lastAuthed = 0.0
81 self.__contacts = None
83 def is_authed(self, force = False):
85 Attempts to detect a current session and pull the auth token ( a_t ) from the page.
86 @note Once logged in try not to reauth more than once a minute.
87 @returns If authenticated
90 if (time.time() - self._lastAuthed) < 60 and not force:
94 forwardSelectionPage = self._browser.download(GCDialer._forwardselectURL)
95 except urllib2.URLError, e:
96 warnings.warn("%s is not accesible" % GCDialer._forwardselectURL, UserWarning, 2)
99 self._browser.cookies.save()
100 if GCDialer._isLoginPageRe.search(forwardSelectionPage) is None:
101 self._grab_token(forwardSelectionPage)
102 self._lastAuthed = time.time()
107 def login(self, username, password):
109 Attempt to login to grandcentral
110 @returns Whether login was successful or not
115 loginPostData = urllib.urlencode( {'username' : username , 'password' : password } )
118 loginSuccessOrFailurePage = self._browser.download(GCDialer._loginURL, loginPostData)
119 except urllib2.URLError, e:
120 warnings.warn("%s is not accesible" % GCDialer._loginURL, UserWarning, 2)
123 return self.is_authed()
126 self._lastAuthed = 0.0
127 self._browser.cookies.clear()
128 self._browser.cookies.save()
132 def dial(self, number):
134 This is the main function responsible for initating the callback
136 # If the number is not valid throw exception
137 if not self.is_valid_syntax(number):
138 raise ValueError('number is not valid')
140 # No point if we don't have the magic cookie
141 if not self.is_authed():
142 raise RunetimeError("Not Authenticated")
144 # Strip leading 1 from 11 digit dialing
145 if len(number) == 11 and number[0] == 1:
149 callSuccessPage = self._browser.download(
150 GCDialer._clicktocallURL % (self._accessToken, number),
152 {'Referer' : 'http://www.grandcentral.com/mobile/messages'}
154 except urllib2.URLError, e:
155 warnings.warn("%s is not accesible" % GCDialer._clicktocallURL, UserWarning, 2)
156 raise RunetimeError("%s is not accesible" % GCDialer._clicktocallURL)
158 if GCDialer._gcDialingStrRe.search(callSuccessPage) is None:
159 raise RuntimeError("Grand Central returned an error")
163 def clear_caches(self):
164 self.__contacts = None
166 def is_valid_syntax(self, number):
168 @returns If This number be called ( syntax validation only )
170 return self._validateRe.match(number) is not None
172 def get_account_number(self):
174 @returns The grand central phone number
176 return self._accountNum
178 def set_sane_callback(self):
180 Try to set a sane default callback number on these preferences
181 1) 1747 numbers ( Gizmo )
182 2) anything with gizmo in the name
183 3) anything with computer in the name
186 numbers = self.get_callback_numbers()
188 for number, description in numbers.iteritems():
189 if not re.compile(r"""1747""").match(number) is None:
190 self.set_callback_number(number)
193 for number, description in numbers.iteritems():
194 if not re.compile(r"""gizmo""", re.I).search(description) is None:
195 self.set_callback_number(number)
198 for number, description in numbers.iteritems():
199 if not re.compile(r"""computer""", re.I).search(description) is None:
200 self.set_callback_number(number)
203 for number, description in numbers.iteritems():
204 self.set_callback_number(number)
207 def get_callback_numbers(self):
209 @returns a dictionary mapping call back numbers to descriptions
210 @note These results are cached for 30 minutes.
212 if time.time() - self._lastAuthed < 1800 or self.is_authed():
213 return self._callbackNumbers
217 def set_callback_number(self, callbacknumber):
219 Set the number that grandcental calls
220 @param callbacknumber should be a proper 10 digit number
222 callbackPostData = urllib.urlencode({
223 'a_t': self._accessToken,
224 'default_number': callbacknumber
227 callbackSetPage = self._browser.download(GCDialer._setforwardURL, callbackPostData)
228 except urllib2.URLError, e:
229 warnings.warn("%s is not accesible" % GCDialer._setforwardURL, UserWarning, 2)
232 self._browser.cookies.save()
235 def get_callback_number(self):
237 @returns Current callback number or None
239 for c in self._browser.cookies:
240 if c.name == "pda_forwarding_number":
244 def get_recent(self):
246 @returns Iterable of (personsName, phoneNumber, date, action)
249 recentCallsPage = self._browser.download(GCDialer._inboxallURL)
250 except urllib2.URLError, e:
251 warnings.warn("%s is not accesible" % GCDialer._inboxallURL, UserWarning, 2)
254 for match in self._inboxRe.finditer(recentCallsPage):
255 phoneNumber = match.group(4)
256 action = match.group(1)
257 date = match.group(2)
258 personsName = match.group(3)
259 yield personsName, phoneNumber, date, action
261 def get_addressbooks(self):
263 @returns Iterable of (Address Book Factory, Book Id, Book Name)
267 def open_addressbook(self, bookId):
271 def contact_source_short_name(contactId):
276 return "Grand Central"
278 def get_contacts(self):
280 @returns Iterable of (contact id, contact name)
282 if self.__contacts is None:
285 contactsPagesUrls = [GCDialer._contactsURL]
286 for contactsPageUrl in contactsPagesUrls:
287 contactsPage = self._browser.download(contactsPageUrl)
288 for contact_match in self._contactsRe.finditer(contactsPage):
289 contactId = contact_match.group(1)
290 contactName = contact_match.group(2)
291 contact = contactId, contactName
292 self.__contacts.append(contact)
295 next_match = self._contactsNextRe.match(contactsPage)
296 if next_match is not None:
297 newContactsPageUrl = self._contactsURL + next_match.group(1)
298 contactsPagesUrls.append(newContactsPageUrl)
300 for contact in self.__contacts:
303 def get_contact_details(self, contactId):
305 @returns Iterable of (Phone Type, Phone Number)
307 detailPage = self._browser.download(GCDialer._contactDetailURL + '/' + contactId)
308 for detail_match in self._contactDetailPhoneRe.finditer(detailPage):
309 phoneType = detail_match.group(1)
310 phoneNumber = detail_match.group(2)
311 yield (phoneType, phoneNumber)
313 def _grab_token(self, data):
314 "Pull the magic cookie from the datastream"
315 atGroup = GCDialer._accessTokenRe.search(data)
316 self._accessToken = atGroup.group(1)
318 anGroup = GCDialer._accountNumRe.search(data)
319 self._accountNum = anGroup.group(1)
321 self._callbackNumbers = {}
322 for match in GCDialer._callbackRe.finditer(data):
323 self._callbackNumbers[match.group(1)] = match.group(2)