/
web.py
121 lines (97 loc) · 4.47 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from datetime import datetime
import re
import logging
import requests
LOGIN_URL = 'https://www.my.telstra.com.au/myaccount/home'
log = logging.getLogger(__file__)
class TelstraWebApi(object):
""" API for accessing Telstra mobile information via a web interface.
This could deserve its own package eventually, though since the methods
here are specific to Telstra mobiles, it lives here for now.
Authenticate using your telstra.com username and password.
**Caution**: all methods use undocumented end-points and are liable
to break or change at any time!
"""
def __init__(self, username, password):
self.session = requests.Session()
login_page = self.session.get(LOGIN_URL)
login_page.raise_for_status()
login_data = {
'encoded': 'false',
'goto': 'https://www.my.telstra.com.au/myaccount/overview',
'gotoOnFail': 'https://www.my.telstra.com.au/myaccount/home/error?flag=EMAIL',
'gx_charset': 'UTF-8',
'password': password,
'username': username
}
login_response = \
self.session.post('https://signon.telstra.com.au/login',
data=login_data)
login_response.raise_for_status()
@staticmethod
def java_time_to_python(milliseconds):
""" Convert System.currentTimeMillis() into a Unix timestamp.
"""
return datetime.fromtimestamp(milliseconds/1000.0)
def logout(self):
self.session.get('https://www.my.telstra.com.au/myaccount/log-out')
def _get_json(self, url):
response = self.session.get(url)
content_type = response.headers.get('content-type')
if response.url == url and 'json' in content_type:
return response.json()
else:
log.critical('Could not retrieve service metadata. '
'Content type is %s.' % content_type)
return {}
@property
def _prepaid_metadata(self, phone_number=None):
""" Obtain account and service metadata for a prepaid service.
"""
url = 'https://www.my.telstra.com.au/myaccount/prepaid/getCreditSummaryData.json'
return self._get_json(url)
def prepaid_expiry(self, phone_number=None):
timestamp = self._prepaid_metadata.get('creditExpireDate')
return self.java_time_to_python(timestamp) if timestamp else None
def prepaid_balance(self, phone_number=None):
credit_amount = self._prepaid_metadata.get('creditAmount')
return credit_amount['value'] if credit_amount else None
def prepaid_offer(self, phone_number=None):
offer = self._prepaid_metadata.get('commercialOfferCode')
return offer['value'] if offer else None
def prepaid_bonuspacks(self, phone_number=None):
return self._prepaid_metadata.get('bundleGroupList')
def contact_details(self):
""" Obtains account holder contact details on file.
{"middleName":null,
"lastName":"Smith",
"homeNumber":null,
"firstName":"John"}
"""
return self._get_json('https://www.my.telstra.com.au/myaccount/contactdetail.json')
def prepaid_recharge_history(self, phone_number=None):
""" Access and process recharge history for a prepaid service.
[{'date': 1405250719000,
'amount': {'value': 10},
...]
There's other information in the JSON structure, but it is all
redundant.
"""
data = self._get_json('https://www.my.telstra.com.au/myaccount/prepaid/getPrepaidRechargeHistory.json')
if 'result' in data:
for recharge in data['result']:
print(self.java_time_to_python(recharge['date'],
recharge['amount']['value']))
def prepaid_history(self, phone_number=None):
""" Access the last 30 days of call and service records.
Processing of this response requires screen scraping HTML.
"""
response = self.session.get('https://www.my.telstra.com.au/myaccount/data-usage-pre-paid/prepaid-usage-fragment?serviceId=')
print(response.content)
def prepaid_puk(self, phone_number=None):
""" Get the PUK for the given account via scraping HTML.
"""
response = self.session.get('https://www.my.telstra.com.au/myaccount/plan-details-pre-paid')
match = re.search('<span class="puk">(.*?)</span>', response.content)
if match:
return match.groups()[0]