Skip to content

Instantly share code, notes, and snippets.

@Terrance
Last active June 7, 2021 05:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Terrance/d36756b5ae103410aa79d287fb6fef84 to your computer and use it in GitHub Desktop.
Save Terrance/d36756b5ae103410aa79d287fb6fef84 to your computer and use it in GitHub Desktop.
Methods for parsing free mobile data usage on Three UK and FreedomPop UK. No guarantees that these will work with paid data plans.
from collections import namedtuple
from datetime import datetime, date, timedelta
import re
import requests
from bs4 import BeautifulSoup
Row = namedtuple("Row", ["number", "network", "total", "left", "reset"])
def three(user, pwd):
sess = requests.Session()
# Fetch login form containing a hidden token field.
resp = sess.get("https://sso.three.co.uk/mylogin/",
params={"service": "https://www.three.co.uk/ThreePortal/appmanager/Three/SelfcareUk",
"resource": "portlet"})
form = BeautifulSoup(resp.text, "html.parser").find("form", {"name": "login_form"})
if form:
# (If not, we're already logged in...?)
inputs = form.find_all("input")
# Copy form inputs to data, replacing username and password.
data = dict((i.get("name"), i.get("value")) for i in inputs)
data["username"] = user
data["password"] = pwd
# Submit credentials.
resp = sess.post(resp.url, data=data)
# Expect an <a> with the URL containing a ticket, follow to complete auth.
sess.get(BeautifulSoup(resp.text, "html.parser").find("a").get("href"))
# Now we're logged in, fetch our balance.
isData = True
resp = sess.get("https://www.three.co.uk/New_My3/Data_allowance")
tables = BeautifulSoup(resp.text, "html.parser").find_all("table", class_="balance")
if not tables:
# Not a data SIM? Try the general account page instead.
isData = False
resp = sess.get("https://www.three.co.uk/New_My3/Account_balance")
# Skip account balance at the bottom.
tables = BeautifulSoup(resp.text, "html.parser").find_all("table", class_="balance")[:-1]
if not tables:
# Welp.
return []
left = 0
# We're not told the total value, just assume 200 for data SIMs.
total = "200" if isData else "0"
reset = None
for table in tables:
tds = table.find_all("td")
left += float(tds[2].text.strip())
# This may vary between tables, but... meh.
expires = tds[1].text.split()
reset = date.today() if expires[0] == "Today" else datetime.strptime(expires[1], "%d/%m/%y").date()
return [Row(user, "Three", total, str(left), reset)]
def freedompop(user, pwd):
MB = 1024 * 1024
sess = requests.Session()
# Do the login.
resp = sess.post("https://my.freedompop.com/api/identity", json={"username": user, "password": pwd})
user = resp.json()["sub"]
# Fetch all the info.
usage = []
resp = sess.get("https://my.freedompop.com/api/user/{}/accounts".format(user))
for device in resp.json():
number = "0{}".format(device["fpopPhone"][2:])
ref = device["externalId"]
resp = sess.get("https://my.freedompop.com/api/account/{}/usage/data-activity/current".format(ref))
data = resp.json()
total = data["totalAllocated"]
left = total - data["totalUsed"]
# Full timestamp, but always midnight.
reset = datetime.fromtimestamp(data["endTime"] / 1000).date()
usage.append(Row(number, "FreedomPop", round(total / MB, 3), round(left / MB, 3), reset))
return usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment