-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedisoc.py
executable file
·311 lines (267 loc) · 8.07 KB
/
medisoc.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = "Georg Wallisch"
__contact__ = "gw@phpco.de"
__copyright__ = "Copyright © 2019 by Georg Wallisch"
__credits__ = ["Georg Wallisch"]
__date__ = "2019/08/09"
__deprecated__ = False
__email__ = "gw@phpco.de"
__license__ = "open source software"
__maintainer__ = "Georg Wallisch"
__status__ = "alpha"
__version__ = "0.2"
import requests
import re
from datetime import datetime
from datetime import timedelta
import logging
from lxml import etree
import HTMLParser
import logging
#reload(sys)
#sys.setdefaultencoding("utf-8")
class MedisocAccount:
"""Query Medisoc Host
"""
def __init__(self, userid, password, host="www.medisoc.de", protocol = 'https', logger = None):
if logger is None:
console = logging.StreamHandler()
self._log = logging.getLogger('MedisocAccount')
self._log.addHandler(console)
else:
self._log = logger
self._log.info("Constructing new MedisocAccount object")
self.userid = userid
self.password = password
self.host = host
self.protocol = protocol
self.session = requests.session()
self.customers = None
self.customers_header = None
def login(self):
hosturl = self.get_hosturl('/login/')
self._log.info("Logging in..")
self._log.debug("querying {}".format(hosturl))
payload = {'kundennummer': self.userid, 'passwort': self.password}
r = self.session.post(hosturl, data=payload)
if r:
self._log.info("Login successful.")
return r
else:
self._log.error("Login failed.")
return None
def get_hosturl(self, uri):
return "{}://{}{}".format(self.protocol, self.host, uri)
def get_xhrpage(self, uri, referrer = None):
hosturl = self.get_hosturl(uri)
self._log.debug("Getting XHR Page {}".format(hosturl))
if referrer is None:
referrer = hosturl
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0',
'X-Requested-With': 'XMLHttpRequest',
'Host': self.host,
'Referer': referrer}
r = self.session.get(hosturl, headers=headers)
if r:
return r
else:
self._log.error("Page does not exist!")
return None
def post_xhr(self, uri, payload, referrer = None):
hosturl = self.get_hosturl(uri)
self._log.debug("Posting XHR Request {}".format(hosturl))
if referrer is None:
referrer = hosturl
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0',
'X-Requested-With': 'XMLHttpRequest',
'Host': self.host,
'Referer': referrer}
r = self.session.post(hosturl, headers=headers, data=payload)
if r:
return r
else:
self._log.error("Post Request not successful!")
return None
def get_customers(self):
t = TableParser()
next_uri = '/startseite/massbestellung/'
i = 0
while(next_uri is not None):
i += 1
self._log.info("Reading Page {}".format(i))
p = PaginationParser()
r = self.get_xhrpage(next_uri)
if r:
t.header_firstline = True
t.feed(r.text)
p.feed(r.text)
next_uri = p.next_uri
self.customers = t.content
self.customers_header = t.header
def get_customer_history(self, pnr, include_current_orders = True):
self._log.info("Getting Customer product history for PNR {}".format(pnr))
t = TableParser()
next_uri = "/patient/altmass/?pnr={}".format(pnr)
i = 0
while(next_uri is not None):
i += 1
self._log.info("Reading Page {}".format(i))
p = PaginationParser()
r = self.get_xhrpage(next_uri,'/startseite/massbestellung/')
if r:
t.header_firstline = True
t.feed(r.text)
p.feed(r.text)
next_uri = p.next_uri
if include_current_orders:
self._log.info("Getting current orders of Customer PNR {}".format(pnr))
next_uri = "/patient/altmass/?pnr={}&show=offen".format(pnr)
i = 0
while(next_uri is not None):
i += 1
self._log.info("Reading Page {}".format(i))
p = PaginationParser()
r = self.get_xhrpage(next_uri,'/startseite/massbestellung/')
if r:
t.header_firstline = True
t.feed(r.text)
p.feed(r.text)
next_uri = p.next_uri
return t.content
def get_customer_data(self, pnr):
self._log.info("Getting Customer data for PNR {}".format(pnr))
r = self.get_xhrpage("/patient/?pnr={}".format(pnr))
f = FormParser()
f.feed(r.text)
d = {'attrs':f.form_attrs, 'data':f.form_data}
return d
def set_customer_data(self, pnr, data):
self._log.info("Setting Customer data for PNR {}".format(pnr))
r = self.post_xhr("/patient/?pnr={}&save=true".format(pnr), data, "/patient/?pnr={}".format(pnr))
return r
def set_customer_inactive(self, pnr, info=None):
c = self.get_customer_data(pnr)
data = c['data']
if 'active' in data:
data.pop('active')
if info is not None:
if 'patienteninfo' in data and data['patienteninfo'] != "":
data['patienteninfo'] += "\n" + info
else:
data['patienteninfo'] = info
r = self.set_customer_data(pnr, data)
return r
class TableParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.in_td = False
self.in_table = False
self.in_tr = False
self.header_firstline = True
self.header = []
self.content = []
self.row = []
def handle_starttag(self, tag, attrs):
if tag == 'table':
self.in_table = True
elif tag == 'tr':
self.in_tr = True
elif tag == 'td':
self.in_td = True
def handle_data(self, data):
d = data.strip(" \n\r\t")
if self.in_td:
if self.header_firstline:
self.header.append(d)
else:
self.row.append(d)
def handle_endtag(self, tag):
if tag == 'table':
self.in_table = False
elif tag == 'tr':
self.in_tr = False
if self.header_firstline:
self.header_firstline = False
else:
self.content.append(self.row)
self.row = []
elif tag == 'td':
self.in_td = False
class PaginationParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.div_next = False
self.next_uri = None
#<div class="pagination-next">
#<a href="/startseite/massbestellung/?&page=2&show=&suche=">Vor</a>
#</div>
def handle_starttag(self, tag, attrs):
if self.div_next:
if tag == 'a':
for attr in attrs:
if attr[0] == 'href':
if 'page=1&' not in attr[1]:
self.next_uri = attr[1]
else:
if tag == 'div':
for attr in attrs:
if attr[0] == 'class' and attr[1] == 'pagination-next':
self.div_next = True
def handle_endtag(self, tag):
if self.div_next and tag == 'div':
self.div_next = False
class FormParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.in_form = False
self.in_textarea = False
self.in_select = False
self.form_attrs = None
self.form_data = {}
self.textarea_attrs = None
self.select_attrs = None
def parse_attrs(self, attrs):
data = {}
for attr in attrs:
data[attr[0]] = attr[1]
return data
def handle_starttag(self, tag, attrs):
data = self.parse_attrs(attrs)
if self.in_form:
if tag == 'textarea':
self.in_textarea = True
self.textarea_attrs = data
elif tag == 'select':
self.in_select = True
self.select_attrs = data
elif tag == 'option':
if 'selected' in data:
if 'name' in data and 'value' in data:
self.form_data[data['name']] = data['value']
elif tag == 'input':
if data['type'] == 'text':
if 'name' in data and 'value' in data:
self.form_data[data['name']] = data['value']
elif data['type'] == 'checkbox':
if 'checked' in data:
if 'name' in data and 'value' in data:
self.form_data[data['name']] = data['value']
else:
if 'name' in data:
self.form_data[data['name']] = ''
else:
if tag == 'form':
self.in_form = True
self.form_attrs = data
def handle_data(self, data):
d = data.strip(" \n\r\t")
if self.in_textarea:
self.form_data[self.textarea_attrs['name']] = d
def handle_endtag(self, tag):
if tag == 'form':
self.in_form = False
elif tag == 'textarea':
self.in_textarea = False
elif tag == 'select':
self.in_select = False