Created
April 2, 2017 13:51
-
-
Save dracos/d925b5a9f274d562ad178ff48ca1fdc7 to your computer and use it in GitHub Desktop.
Script to tell me how much data I have left on my 3 SIM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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