#!/usr/bin/python
# script to renew all UC Berkeley library books
# Author: Thomas Kho
# Date: March 19, 2007

import pexpect

# configuration
CALID = '12345678' # your calnet id
PIN = '1234' # your birth day and month in MMDD

# code
class loggedpexpect:
  def __init__(self, str):
    self.p = pexpect.spawn(str)

  def expect(self, str):
    print 'Expecting', str
    self.p.expect(str)
    print '   ...done'
    return self.p.match.group()

  def send(self, str):
    print 'Sending', str
    self.p.send('%s\r' % str)
    print '   ...done'

def renew(p, calid, pin):
  p.expect('Enter Choice> ')
  p.send('GLADIS')
  p.expect('===> ')
  p.send('')
  p.expect('===> ')
  p.send('inv %s' % calid)
  p.expect('===> ')
  p.send(pin)
  count = p.expect('[0-9]+ items checked out.').split(" ")[0]
  for i in range(1, int(count) + 1):
    p.expect('===> ')
    p.send('ren %s' % i)

renew(loggedpexpect('telnet gladis.berkeley.edu'), CALID, PIN)


