forked from mpapierski/medicover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medicover.py
72 lines (60 loc) · 1.73 KB
/
medicover.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
import json
import os
import requests
import HTMLParser
from bs4 import BeautifulSoup
username = os.environ['MEDICOVER_USERNAME']
password = os.environ['MEDICOVER_PASSWORD']
session = requests.Session()
# Step 1: Open login page.
r = session.get('https://mol.medicover.pl/Users/Account/LogOn')
bs = BeautifulSoup(r.content)
json_text = HTMLParser.HTMLParser().unescape(bs.select('#modelJson')[0].text)
token = json.loads(json_text)['antiForgery']['value']
# Step 2: Send login info.
r = session.post(
r.url,
data={
'username': username,
'password': password,
'idsrv.xsrf': token,
})
r.raise_for_status()
bs = BeautifulSoup(r.content)
def getHiddenField(name):
return bs.select('input[name="%s"]' % name)[0]['value']
# Step 3: Forward auth info to main page.
r = session.post(
'https://mol.medicover.pl/Medicover.OpenIdConnectAuthentication/Account/OAuthSignIn',
data={
'code': getHiddenField('code'),
'id_token': getHiddenField('id_token'),
'scope': getHiddenField('scope'),
'state': getHiddenField('state'),
'session_state': getHiddenField('session_state'),
})
r.raise_for_status()
all_appointments = []
page = 1
while True:
pageSize = 12
r = session.post('https://mol.medicover.pl/api/MyVisits/SearchVisitsToView',
headers={
'X-Requested-With': 'XMLHttpRequest'
},
data={
'Page': page,
'PageSize': pageSize,
})
r.raise_for_status()
json_data = r.json()
all_appointments += json_data['items']
print len(all_appointments), json_data['totalCount']
if len(all_appointments) < json_data['totalCount']:
page += 1
else:
break
r = session.get('https://mol.medicover.pl/Users/Account/LogOff')
r.raise_for_status()
with open('appointments.json', 'wb') as f:
json.dump(all_appointments, f, indent=4)