-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.py
73 lines (56 loc) · 1.77 KB
/
balance.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
import httplib
import urllib
from StringIO import StringIO
from lxml import etree
from lxml.etree import fromstring
from lxml.cssselect import CSSSelector
username = ""
password = ""
login_url = "/icok/LoginCheck.action"
balance_url = "/icok/MvnoPakiety.action"
host = "icok.cyfrowypolsat.pl"
HEADERS = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", "Connection": "keep-alive"}
def fix_html(html):
"""
Returns fixed HTML.
:param html:
:return:
"""
parser = etree.HTMLParser()
tree = etree.parse(StringIO(html), parser)
return etree.tostring(tree.getroot(), pretty_print=True, method="html")
def login(username, password):
"""
Returns cookie with session id if authentication is successful.
:param username:
:param password:
:return:
"""
params = urllib.urlencode({'login': username, 'password': password, 'Zaloguj': ''})
conn = httplib.HTTPSConnection(host)
conn.request("POST", login_url, params, HEADERS)
response = conn.getresponse()
return response.getheader('set-cookie')
def get_balance_request(cookie):
"""
Returns body of the website with balance information.
:param cookie:
:return:
"""
headers = HEADERS.copy()
headers.update({"Cookie": cookie})
conn = httplib.HTTPSConnection(host)
conn.request("GET", balance_url, {}, headers)
response = conn.getresponse()
return response.read()
def print_balance(html):
"""
Prints current balance and expiration date.
:param html:
"""
sel = CSSSelector('table.cpIcokInfo')
h = fromstring(fix_html(html))
print sel(h)[0][1][0][1][0].tail, sel(h)[0][1][0][2][0].tail
cookie = login(username, password)
balance_site = get_balance_request(cookie)
print_balance(balance_site)