import os from datetime import datetime, date, timedelta import logging from splinter import Browser class Messagenet(object): BASE = 'https://www.messagenet.com' def __init__(self, browser_driver='chrome'): self.browser_driver = browser_driver self._browser = None # roba pitonesca {{{ def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): if self._browser is not None: if os.getenv('MN_DEBUG', '0') == '1': raw_input('Premi un tasto per uscire') self._browser.quit() self._browser = None # }}} @property def browser(self): if self._browser is None: self._browser = Browser(self.browser_driver) return self._browser def login(self, user, password): self.browser.visit(self.BASE) self.browser.fill('userid', user) self.browser.fill('password', password) self.browser.find_by_css('#login button').click() def get_inbound_calls(self): '''you cannot call this unless you have already logged in''' b = self.browser b.visit(self.BASE + '/voip/log/?chiamate=ricevute') # estendo la ricerca {{{ longest = b.find_by_css('select[name="data"] option:last-child') longest.click() b.find_by_css('button.search_button').click() # }}} rows = b.find_by_css('.sepLog') for r in rows: ok = r.has_class('statusOK') cells = r.find_by_tag('td') call_type_desc = cells[0].value date = cells[1].value # avanzato: prendi la data {{{2 # }}} call_from = cells[2].value call_to = cells[3].value duration = cells[4].value yield {'status': (ok, call_type_desc), 'date': date, 'from': call_from, 'to': call_to, 'duration': duration} def get_outbound_calls(self): '''you cannot call this unless you have already logged in''' b = self.browser b.visit(self.BASE + '/voip/log/?chiamate=effettuate') # estendo la ricerca {{{ longest = b.find_by_css('select[name="data"] option:last-child') longest.click() b.find_by_css('button.search_button').click() # }}} rows = b.find_by_css('.sepLog') for r in rows: ok = r.has_class('statusOK') cells = r.find_by_tag('td') call_type_desc = cells[0].value human_date = cells[1].value # avanzato: parsa la data {{{2 try: parsed_date = datetime.strptime(human_date, '%Y-%m-%d, %H.%M.%S') except ValueError as exc: print 'parsing date', exc if human_date.startswith('Oggi, '): d = date.today() t = datetime.strptime(human_date.split(',', 1)[1].strip(), '%H.%M.%S').time() parsed_date = datetime.combine(d, t) elif human_date.startswith('Ieri, '): d = date.today() - timedelta(days=1) t = datetime.strptime(human_date.split(',', 1)[1].strip(), '%H.%M.%S').time() parsed_date = datetime.combine(d, t) else: logging.error("Could not parse date '%s'" % human_date) parsed_date = None # }}} call_from = cells[2].value deviata = bool(cells[3].value) call_to = cells[4].value duration = cells[5].value yield {'status': (ok, call_type_desc), 'date': human_date, 'datetime': parsed_date, 'deviata': deviata, 'from': call_from, 'to': call_to, 'duration': duration} def set_forward(self, to): b = self.browser b.visit(self.BASE + '/voip/numeri') if to is False: print '\n'.join([meth for meth in dir(b) if '_by_' in meth]) b.find_by_name('deviazione').find_by_value('disattiva').click() else: # assumiamo che il numero sia valido b.find_by_name('deviazione').find_by_value('numero').click() b.find_by_id('numdev').fill(to) b.find_by_css('button[type="submit"]').click() def get_forward(self): b = self.browser b.visit(self.BASE + '/voip/numeri') fw_type = b.find_by_name('deviazione').first.value fw_all = bool(int(b.find_by_id('tutte').value)) fw_to = b.find_by_id('numdev').value return {'type': fw_type, 'all': fw_all, 'to': fw_to} def get_currency(self): b = self.browser b.visit(self.BASE + '/voip/numeri') moneystring = b.find_by_css('.currency span').first.value # FIXME: should do a proper parsing based on LC_CURRENCY return float(moneystring.replace(',', '.'))