Voipbuster balance monitoring

July 29, 2011
Script to check Voipbuster balance. If it goes below 1€, an email will be sent.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# voipcheck.py
#
# Copyright (c) 2011 Ruslan Valiyev
# ruslan@valiyev.com
#
import urllib
import urllib2
import BeautifulSoup
import smtplib
from email.MIMEText import MIMEText

smtpserver = 'smtp.foo.com'
smtpfrom = 'server@valiyev.com'
smtpto = ['linuxoid@gmail.com']
username = 'voipbusteruser'
password = 'voipbusterpass'
url = 'https://www.voipbuster.com/myaccount'
login = url+'/index.php?part=tplogin'

o = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(o)
data = urllib.urlencode({'user' : username, 'pass' : password})
f = o.open(login, data)
s = f.read()
f.close()

q = o.open(url+'/calls.php')
s = BeautifulSoup.BeautifulSoup(q.read())
balance = s.findAll('b')[1].string.replace(' ', '').replace('€', '').replace(' ', '').replace('\n', '')

if float(balance) < 1:
  subj = 'Voipbuster balance for %s is low!' % (username)
  msg_text = 'Current balance: %s euros.' % str(balance)
  msg = MIMEText(msg_text)
  msg['Subject'] = subj
  s = smtplib.SMTP()
  s.connect(smtpserver)
  s.sendmail(smtpfrom, smtpto, msg.as_string())
  s.close()