-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.py
180 lines (163 loc) · 6.81 KB
/
operators.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
import requests
import json
class MT:
def __init__(self):
self.stations = {
"U": {"name": "Uppsala C", "id": "cf09cbb1-fd82-4b83-9c09-87bc8fc2f018"},
"Cst": {
"name": "Stockholm C",
"id": "f4d25596-a9f9-41a1-b200-713439d92fc4",
},
"Srv": {"name": "Storvreta", "id": "ddf64ca1-b3b3-4820-94fb-137f17fbefc3"},
"Fvk": {"name": "Furuvik", "id": "8751e52c-2214-4c1d-b64b-1d3eefc524b9"},
"Gä": {"name": "Gävle", "id": "c1ed2e95-5cc2-4e9d-a89b-fb27f01ad527"},
"Kn": {"name": "Knivsta", "id": "18df975c-61be-4029-94cd-fc565b0da4d9"},
"Mr": {"name": "Märsta", "id": "57d62e84-ab78-437f-bd59-7e5839de3ce4"},
}
def submit(
self, ticket, from_station, to_station, departure_date, departure_time, customer
):
r = requests.post(
"https://evf-regionsormland.preciocloudapp.net/api/Claims",
json=self._create_request_body(
ticket,
from_station,
to_station,
self._get_fake_iso_string(departure_date, departure_time),
customer,
)
)
return True
def _create_request_body(
self, ticket, dep_station, arr_station, departure, customer
):
return {
"id": "00000000-0000-0000-0000-000000000000",
"customer": customer
| {
"id": "00000000-0000-0000-0000-000000000000",
"bankAccountNumber": "",
"clearingNumber": "",
},
"ticketNumber": ticket,
"ticketType": 1,
"departureStationId": self.stations[dep_station]["id"],
"arrivalStationId": self.stations[arr_station]["id"],
"departureDate": departure,
"comment": "",
"status": 0,
"trainNumber": self._get_train_number(dep_station, arr_station, departure),
"refundType": {
"id": "00000000-0000-0000-0000-000000000000",
"name": "Payment via Swedbank SUS",
},
"claimReceipts": [],
}
def _get_train_number(self, departure_station, arrival_station, departure_time):
r = requests.get(
"https://evf-regionsormland.preciocloudapp.net/api/TrainStations/GetDistance",
params={
"departureStationId": departure_station,
"arrivalStationId": arrival_station,
"departureDate": departure_time,
},
)
return r.json()["data"]["trafikverketTrainId"]
def _get_fake_iso_string(self, departure_date, departure_time):
return f"{departure_date}T{departure_time}.000Z"
class SJ:
def __init__(self):
self.session = requests.Session()
self.session.headers.update(
{
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0",
"Ocp-Apim-Subscription-Key": "78e7aad0e7b042b685d70e0131d897ca"
}
)
self.stations = {
"U": {"name": "Uppsala C", "id": "740000005"},
"Cst": {"name": "Stockholm Central", "id": "740000001"},
"Kn": {"name": "Knivsta", "id": "740000559"},
"Mr": {"name": "Märsta", "id": "740000027"},
}
def submit(
self, ticket, from_station, to_station, departure_date, departure_time, customer
):
self._register_ticket(ticket)
self._add_travel_details(
from_station, to_station, departure_date, departure_time
)
self._add_traveller_details(customer)
self._add_payout_details(customer["identityNumber"], customer["mobileNumber"])
return self._confirm()
def _register_ticket(self, ticket):
r = self.session.post(
"https://prod-api.adp.sj.se/public/delay-compensation/v1/compensation/delaycompensationtokens",
data=json.dumps(
{
"commuterCardType": "Movingo 30 dgr på SJ kort",
"commuterCardNumber": ticket,
},
ensure_ascii=False,
).encode("utf-8"),
headers={"Content-Type": "application/json; charset=UTF-8"},
)
self.token = r.json()["delayCompensationToken"]
def _add_travel_details(
self, from_station, to_station, departure_date, departure_time
):
r = self.session.put(
f"https://prod-api.adp.sj.se/public/delay-compensation/v1/compensation/{self.token}/traveldetails",
files={
"file": (
"data",
json.dumps(
{
"journeyDetail": {
"departureLocation": self.stations[from_station],
"arrivalLocation": self.stations[to_station],
"journeyDate": {"date": departure_date},
"journeyTime": {"time": departure_time[:5]},
},
"expenses": [],
},
ensure_ascii=False,
).encode("utf-8"),
"application/json",
)
},
)
self.token = r.json()["delayCompensationToken"]
def _add_traveller_details(self, customer):
r = self.session.put(
f"https://prod-api.adp.sj.se/public/delay-compensation/v1/compensation/{self.token}/contactinformation",
data=json.dumps(
{
"emailAddress": customer["email"],
"mobilePhoneNumber": customer["mobileNumber"].replace("-", ""),
"personName": {
"firstName": customer["firstName"],
"lastName": customer["surName"],
},
"personalIdentityNumber": customer["identityNumber"],
},
ensure_ascii=False,
).encode("utf-8"),
headers={"Content-Type": "application/json"},
)
self.token = r.json()["delayCompensationToken"]
def _add_payout_details(self, ssn, mobileNumber):
r = self.session.post(
"https://prod-api.adp.sj.se/public/delay-compensation/v1/compensation/bankaccountrecords",
json={
"personalIdentityNumber": ssn,
"swishPhoneNumber": mobileNumber.replace("-", ""),
},
)
self.bar_id = r.json()["barId"]
def _confirm(self):
r = self.session.post(
f"https://prod-api.adp.sj.se/public/delay-compensation/v1/compensation/{self.token}/confirmations",
json={"paynovaBarIds": {"ticketCompensation": self.bar_id}},
)
return r