Skip to content

Instantly share code, notes, and snippets.

Embed
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 = 'https://www.three.co.uk/New_My3/Data_allowance?id=My3_CheckYourDataAllowance'
def login(self, url):
text = s.get(url).text # Redirects to login page
url = re.search('<iframe id="loginIframe" src="([^"]*)"', text).group(1)
text = s.get(url).text # Fetch the actual login form, in an iframe
lt = re.search('name="lt" value="([^"]*)"', text).group(1)
text = s.post(url, 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 = re.search('<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)
ThreeWebsite().show_usage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment