refactoring, REPL, outbound, forward
This commit is contained in:
parent
a43f93be58
commit
660fede91d
2 changed files with 169 additions and 18 deletions
82
leggi.py
82
leggi.py
|
@ -2,10 +2,10 @@ import json
|
|||
import sys
|
||||
from itertools import imap
|
||||
import os.path
|
||||
|
||||
from splinter import Browser
|
||||
from pprint import pprint
|
||||
|
||||
from dirset import DirSet
|
||||
from messagenet import Messagenet
|
||||
|
||||
|
||||
def read_pass_file(fname):
|
||||
|
@ -15,20 +15,6 @@ def read_pass_file(fname):
|
|||
return user, pwd
|
||||
|
||||
|
||||
def get_calls(user, password, driver='firefox'):
|
||||
with Browser(driver) as b:
|
||||
b.visit('https://www.messagenet.com/')
|
||||
b.fill('userid', user)
|
||||
b.fill('password', password)
|
||||
b.find_by_css('#login button').click()
|
||||
|
||||
b.visit('https://www.messagenet.com/voip/log/?chiamate=ricevute')
|
||||
rows = b.find_by_css('.log .statusKO')
|
||||
for r in rows:
|
||||
cells = r.find_by_tag('td')[1:3]
|
||||
yield tuple(imap(lambda c: c.value, cells))
|
||||
|
||||
|
||||
def save_calls(calls, datadir):
|
||||
s = DirSet(datadir)
|
||||
for call in imap(lambda t: '\t'.join(t), calls):
|
||||
|
@ -36,6 +22,17 @@ def save_calls(calls, datadir):
|
|||
print 'NEW: %s' % call
|
||||
|
||||
|
||||
def replhelp():
|
||||
return '''
|
||||
help - show this message
|
||||
calllog - show every call, done and received
|
||||
IN - show every inbound call
|
||||
OUT - show every outbound call
|
||||
forward $NUMBER - forward calls to number
|
||||
forward STOP - disable forwarding
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = json.load(open(os.path.join(
|
||||
os.path.dirname(sys.argv[0]),
|
||||
|
@ -43,5 +40,54 @@ if __name__ == '__main__':
|
|||
if len(sys.argv) == 2:
|
||||
conf.update(json.load(open(sys.argv[1])))
|
||||
user, password = read_pass_file(conf['credfile'])
|
||||
calls = tuple(get_calls(user, password, conf['browserdriver']))
|
||||
save_calls(calls, conf['datadir'])
|
||||
with Messagenet() as mn: # conf['browserdriver'])
|
||||
mn.login(user, password)
|
||||
# readline {{{2
|
||||
try:
|
||||
import readline
|
||||
readline.read_init_file()
|
||||
readline.read_history_file(os.path.join(
|
||||
os.path.dirname(os.path.realpath(__file__)),
|
||||
'.mncli_history'))
|
||||
except ImportError: # readline not supported on your system
|
||||
pass
|
||||
except:
|
||||
print "Some error with readline; who cares"
|
||||
# }}}
|
||||
|
||||
print "messagenet command-line. type help for help. type quit for quit"
|
||||
while True:
|
||||
try:
|
||||
cmdline = raw_input('> ').strip()
|
||||
except EOFError:
|
||||
break
|
||||
if not cmdline:
|
||||
continue
|
||||
cmd = cmdline.split()[0]
|
||||
args = cmdline.split()[1:]
|
||||
if cmd == 'help':
|
||||
print replhelp()
|
||||
elif cmd in ('quit', 'exit', ':wq'):
|
||||
break
|
||||
elif cmd == 'calllog':
|
||||
if not args or 'IN' in args:
|
||||
inb = tuple(mn.get_inbound_calls())
|
||||
print "=== IN ==="
|
||||
pprint(inb[:5])
|
||||
if not args or 'OUT' in args:
|
||||
outb = tuple(mn.get_outbound_calls())
|
||||
print "=== OUT ==="
|
||||
pprint(outb[:5])
|
||||
elif cmd == 'forward':
|
||||
if not args:
|
||||
print mn.get_forward()
|
||||
elif args[0] == 'STOP':
|
||||
mn.set_forward(False)
|
||||
else:
|
||||
mn.set_forward(args[0])
|
||||
else:
|
||||
print " Invalid command '%s'; type 'help' to get help" % cmd
|
||||
|
||||
# save_calls(calls, conf['datadir'])
|
||||
|
||||
# vim: set fdm=marker:
|
||||
|
|
105
messagenet.py
Normal file
105
messagenet.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
import os
|
||||
|
||||
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
|
||||
date = cells[1].value
|
||||
# avanzato: prendi la data {{{2
|
||||
# }}}
|
||||
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': 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').type(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}
|
Loading…
Reference in a new issue