Skip to content

Instantly share code, notes, and snippets.

@rvl
Created October 1, 2012 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvl/3809603 to your computer and use it in GitHub Desktop.
Save rvl/3809603 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
#
# virgin_data_balance.py
#
# Checks how much data you have used on your Virgin Mobile (Australia)
# sim card.
#
# Usage: ./virgin_data_balance.py phone_number pin_code
#
# Can also be used as a CGI script on your web server.
#
# Debian dependencies:
# apt-get install python3 python3-bs4
#
# Author: Rodney Lorrimar <dev@rodney.id.au>
# Date: 1/10/2012
#
from bs4 import BeautifulSoup
from urllib.request import urlopen, build_opener, install_opener, Request, HTTPSHandler, HTTPCookieProcessor
from urllib.parse import urlencode
from collections import OrderedDict
import sys
import os
def main_console(phone_num, pin):
result = do_query(phone_num, pin)
print_dict(result)
return 0
def do_query(phone_num, pin):
data = get_html(phone_num, pin)
result = OrderedDict({"phone_num": phone_num})
parse_html(result, data)
return result
def get_html(phone_num, pin):
login_url = "https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp"
login_data = {"username": phone_num, "password": pin, "submit": "Go"}
data_usage_url = "https://www.virginmobile.com.au/selfcare/dispatch/DataUsageRequest"
cookie_processor = HTTPCookieProcessor()
opener = build_opener(HTTPSHandler, cookie_processor)
install_opener(opener)
req = Request(login_url, data=urlencode(login_data).encode("utf-8"),
headers={"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"})
f = opener.open(req)
f.close()
f = opener.open(data_usage_url)
return f.read().decode('utf-8')
def parse_html(result, data):
soup = BeautifulSoup(data)
def table_get(item):
el = soup.find("div", {"class": item})
return el.find_parent().contents[0].strip()
result["bill_date"] = table_get("bill")
result["phone_plan"] = table_get("phonePlan")
result["value_add"] = table_get("dataVAPlan")
result["excess_charge"] = table_get("excessCharge")
def style_get(prop, el):
styles = dict(tuple(s.split(":", 2)) for s in el.attrs.get("style", "").split(";") if bool(s))
return styles.get(prop, None)
result["usage_string"] = soup.find("div", {"class": "usageMeterHead"}).find("b").get_text()
result["usage_percent"] = style_get("width", soup.find("div", {"class": "dataUsage"}))
return result
def print_dict(d):
width = max(len(k) for k in d)
for k, v in d.items():
print("%s: %s%s" % (k, " " * (width - len(k)), v))
def cgi_script():
import html
import cgi
import cgitb
cgitb.enable()
print("Content-Type: text/html")
print()
print("<html><head><title>Virgin Mobile Data Balance</title></head><body>")
print("<H2>Virgin Mobile Data Balance</H2>")
def print_form(form, msg=None):
print("<form method='POST'><table border='0'>")
if msg:
print("<tr><td colspan='2'>%s</td></tr>" % msg)
print("<tr><td>Phone number:</td><td><input name='phone_num' value='%s'></td></tr>" % html.escape(form.getfirst("phone_num", "")))
print("<tr><td>Password:</td><td><input type='password' name='pin' value='%s'></td></tr>" % html.escape(form.getfirst("pin", "")))
print("<tr><td></td><td align='right'><input type='submit'></td></tr>")
print("</table></form>")
form = cgi.FieldStorage()
if "phone_num" not in form or "pin" not in form:
print_form(form)
else:
result = do_query(str(form.getfirst("phone_num")), str(form.getfirst("pin")))
print("<table border='0'>")
for k, v in result.items():
print("<tr><th>%s:</th><td>%s</td></tr>" % tuple(map(html.escape, (k, v))))
print("</table></form>")
print("</body></html>")
if __name__ == "__main__":
if "SCRIPT_NAME" in os.environ:
cgi_script()
sys.exit(0)
else:
if len(sys.argv) != 3:
print("usage: %s phone_number pin" % sys.argv[0])
sys.exit(1)
sys.exit(main_console(sys.argv[1], sys.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment