-
Notifications
You must be signed in to change notification settings - Fork 5
/
relais_colis.py
65 lines (54 loc) · 2.3 KB
/
relais_colis.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
from tools.date_parser import get_local_time
from tracking.courier import Courier, get_simple_validation
from tracking.requests_handler import RequestsHandler
from windows.localization import TXT
class RelaisColis(Courier):
name = "Relais Colis"
idship_validation, idship_validation_msg = get_simple_validation(10, 16)
url = "https://www.relaiscolis.com/suivi-de-colis/index/tracking/"
def get_url_for_browser(self, idship):
return "https://www.relaiscolis.com/suivi-de-colis/"
@Courier.driversToShow.get()
def open_in_browser(self, idship, driver):
url = self.get_url_for_browser(idship)
driver.get(url)
driver.execute_script(
f'document.getElementById("valeur").value="{idship}";validationForm();'
)
@RequestsHandler()
def get_content(self, idship, request):
return request.request_json(
"POST", self.url, data={"valeur": idship, "typeRecherche": "EXP"}
)
def parse_content(self, content):
events = []
product = None
delivered = False
shipment = content.get("Colis", {}).get("Colis")
if shipment:
vendor = shipment.get("Enseigne")
if vendor:
product = f"{TXT.package_product} {vendor.capitalize()}"
timeline = shipment.get("ListEvenements", {}).get("Evenement", ())
for event in timeline:
status = None
label = event["Libelle"]
date = get_local_time(event["Date"])
event_delivered = False
if event.get("CodeJUS") == "LIV":
delivered = True
event_delivered = True
relais = content.get("Relais", {}).get("Relais")
if relais:
status = label
label = ", ".join(
txt
for k in ("Nom", "Adresse", "CodePostal", "Commune")
if (txt := relais.get(k)) is not None
)
events.append(
dict(
date=date, status=status, label=label, delivered=event_delivered
)
)
return events, dict(product=product, delivered=delivered)