#!/usr/bin/python # vim: set fileencoding=utf-8 : sts=4 : et : sw=4 # Callcentric "click 2 dial" client # # Copyright © 2013 Jeff Epler # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import cookielib import mechanize import os import sys import traceback import bs4 as bs callcentric_cookiefile = os.path.expanduser("~/.callcentric.cookies") moderator = password = login = None try: execfile(os.path.expanduser("~/.callcentricrc")) except: traceback.print_exc() if not login: login = raw_input("Callcentric login: ") if not password: password = getpass.getpass() if len(sys.argv) > 1: dial = sys.argv[1] else: dial = raw_input("Dial number: ") if len(sys.argv) > 2: src = sys.argv[2] if len(src) == 3: moderator = moderator[:11] + src else: moderator = src if moderator is None: moderator = raw_input("Moderator: ") cookies = cookielib.LWPCookieJar(callcentric_cookiefile) if os.path.exists(callcentric_cookiefile): cookies.load(ignore_discard=True) mech = mechanize.Browser() mech.set_cookiejar(cookies) LOGIN_URL = 'https://www.callcentric.com/login/' C2D_URL = "https://my.callcentric.com/click2dial.php" if 0: mech.set_debug_http(True) mech.set_debug_redirects(True) mech.set_debug_responses(True) def is_login_form(f): return f.action == LOGIN_URL def is_c2d_form(f): return f.action == C2D_URL def do_login(do_open = True): if do_open: mech.open(LOGIN_URL) mech.select_form(predicate=is_login_form) mech['l_login'] = login mech['l_passwd'] = password mech.submit() cookies.save(ignore_discard=True) mech.open(C2D_URL) if 'login' in mech.geturl(): do_login(False) if mech.geturl() != C2D_URL: mech.open(C2D_URL) print "Moderator", repr(moderator) print "Dialing", repr(dial) mech.select_form(predicate=is_c2d_form) mech['moderator'] = moderator mech['dial_number1'] = dial mech.submit() b = bs.BeautifulSoup(mech.response()) bad = b.find('input', {'class': 'error'}) if bad: name = bad.get('name') if name == 'dial_number1': name = 'dial' raise SystemExit, "Callcentric reported bad input for %s (possibly an invalid number)" % name if b.find('td', {'class': 'error_msg'}): raise SystemExit, "Callcentric reported an error (possibly an invalid number)" t = b.find('table', bgcolor="#444444") # Stupidest html renderer ever def h2d(t): for i in t: if isinstance(i, basestring): yield i.strip() elif getattr(i, 'name', None) == 'br': yield '\n' else: for j in h2d(i): yield j print "".join(h2d(t))