-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
86 lines (70 loc) · 3.43 KB
/
parser.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
from bs4 import BeautifulSoup
import re
import logging
class Parser:
@staticmethod
def get_reservation_params(petition):
parser = BeautifulSoup(petition.text, 'lxml')
try:
begin, end = parser.find_all('option', {'selected': 'selected'})
begin = begin.get('value')
end = end.get('value')
if begin > end:
end, begin = begin, end
userId = parser.find('input', {'id': 'userId'}).get('value')
beginDate = parser.find('input', {'id': 'formattedBeginDate'}).get('value')
beginPeriod = begin
endDate = parser.find('input', {'id': 'formattedEndDate'}).get('value')
endPeriod = end
scheduleId = parser.find('input', {'id': 'scheduleId'}).get('value')
resourceId = parser.find('input', {'id': 'primaryResourceId'}).get('value')
reservationTitle = parser.find('input', {'id': 'reservationTitle'}).get('value')
reservationId = parser.find('input', {'name': 'reservationId'}).get('value')
referenceNumber = parser.find('input', {'id': 'referenceNumber'}).get('value')
reservationAction = parser.find('input', {'name': 'reservationAction'}).get('value')
hdnDeleteReason = parser.find('input', {'id': 'hdnDeleteReason'}).get('value')
hdnSeriesUpdateScope = parser.find('input', {'id': 'hdnSeriesUpdateScope'}).get('value')
csrf_token = parser.find('input', {'id': 'csrf_token'}).get('value')
reservation_data = {
'userId': userId,
'beginDate': beginDate,
'beginPeriod': beginPeriod,
'endDate': endDate,
'endPeriod': endPeriod,
'scheduleId': scheduleId,
'resourceId': resourceId,
'reservationTitle': reservationTitle,
'reservationDescription': '',
'reservationId': reservationId,
'referenceNumber': referenceNumber,
'reservationAction': reservationAction,
'DELETE_REASON': hdnDeleteReason,
'seriesUpdateScope': hdnSeriesUpdateScope,
'CSRF_TOKEN': csrf_token
}
return reservation_data
except ValueError:
error = parser.find('div', {'class': 'error'})
error = error.text.split('\n')[1]
raise ValueError(error.replace('\n', ''))
@staticmethod
def get_reference(petition):
response = BeautifulSoup(petition.text, 'lxml')
try:
reference = re.search(r'[a-f,0-9]{22}$', response.find('div', {'id': 'reference-number'}).text).group()
return reference
except AttributeError:
error = response.find('div', {'class': 'error'})
raise ValueError(error.text.replace('\n', ''))
@staticmethod
def get_details(petition):
# TODO: This function has to be tested
# It's used when executing the 'checkin' option
response = BeautifulSoup(petition.text, 'lxml')
try:
created = response.find('div', {'id': 'created-message'})
reference = response.find('div', {'id': 'reference-number'})
reference = re.search(r'[a-f,0-9]{22}$', reference.text).group()
except AttributeError:
error = response.find('div', {'class': 'error'})
raise ValueError(error.text.replace('\n', ''))