Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to tell me how much data I have left on my 3 SIM
#!/usr/bin/env python
import os
import re
import requests
s = requests.Session()
class ThreeWebsite(object):
username = os.environ['PHONE_NUMBER']
password = os.environ['PASSWORD']
data_allowance_url = ''
def login(self, url):
text = s.get(url).text # Redirects to login page
url ='<iframe id="loginIframe" src="([^"]*)"', text).group(1)
text = s.get(url).text # Fetch the actual login form, in an iframe
lt ='name="lt" value="([^"]*)"', text).group(1)
text =, data={'username': self.username, 'password': self.password, 'lt': lt}).text
url = re.findall("self.parent.location.href = '([^']*)'", text)[1] # First is if-cookie-missing
return s.get(url).text
def get(self, url):
text = s.get(url).text
if 'Login successful' in text: # Actually means we need to log in
text = self.login(url)
return text
def show_usage(self):
text = self.get(self.data_allowance_url)
m ='<th[^>]*>Valid until</th>\s*<th[^>]*>Remaining</th>\s*</tr>\s*'
'<tr>\s*<td[^>]*>\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td[^>]*>\s*(.*?)\s*</td>(?s)', text)
expiry, left = m.groups()
print 'You have %sMb left, expires %s' % (left, expiry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment