summaryrefslogtreecommitdiffstats
path: root/helpers.py
blob: 843d257eb3c79ab64b5bf8bc961691d3f7aa52d1 (plain) (blame)
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
import requests
import json
from datetime import datetime, timezone, timedelta
from functools import lru_cache


def formatEndDate(st):
    if st is None:
        return (None, None)
    dt = datetime.strptime(st, "%Y-%m-%dT%H:%M:%S.%f0%z")
    formatted = dt.strftime("%d.%m.%Y")

    # print days from today to date
    daysRemaining = (dt - datetime.now(timezone.utc)).days

    return (formatted, daysRemaining)


purl = "https://evinjeta.dars.si/selfcare/api/eshop/shopping-cart/validate"

json_dump = open("payload.txt", "r").read()
payload = json.loads(json_dump)


def get_headers():
    hdump = [x.split(":") for x in [t for t in open("headers.txt", "r")][1:]]
    headers = {h[0]: h[1].strip() for h in hdump}
    return headers


country_codes = {
    "SI": "Slovenija",
    "HR": "Hrvaška",
    "AT": "Avstrija",
    "HU": "Madžarska",
    "IT": "Italija",
    "DE": "Nemčija",
    "RS": "Srbija",
    "CH": "Švica",
    "BA": "Bosna in Hercegovina",
}


@lru_cache(maxsize=None)
def veljavnost(registrska: str, drzava: str):
    print(registrska, drzava)
    today = datetime.now()
    one_year_after = today + timedelta(days=365)
    payload["registrationNumber"] = registrska
    payload["registrationNumberAgain"] = registrska
    payload["vignetteValidityStart"] = today.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    payload["vignetteValidityEnd"] = one_year_after.strftime("%Y-%m-%dT%H:%M:%S.%f0%z")
    payload["registrationCountryCode"] = {
        "text": country_codes[drzava],
        "value": drzava,
        "codebook": "Country",
    }
    print("\n", payload, "\n------------------\n\n\n")
    r = requests.post(purl, json=payload, headers=get_headers(), verify=False)
    # print("\n"+r.text+"\n------------------\n\n\n")
    print(r.status_code, r.reason, r.url)
    return r.json()


def aux(registrska, drzava="SI"):
    if drzava == "SI":
        registrska = registrska.upper().replace(" ", "").replace("-", "").strip()
        if len(registrska) > 8 or len(registrska) < 5 or not registrska.isalnum():
            return None
        if registrska == "FTEST":
            t = 1 / 0
        if registrska[:3] == "XXX":
            return None
    jdump = veljavnost(registrska, drzava)["vignetteValidationResult"]
    if "exemptedVehicles" in jdump and len(jdump["exemptedVehicles"]) > 0:
        return f'Oproščeno {jdump["exemptedVehicles"][0]["exemptionReasonId"]["text"]}'

    # for k, v in jdump.items():
    #     print(f"{k}: {v}")
    max_date = None
    for v in jdump["vignettes"]:
        print(v)
        date_1 = v["vignetteValidityStart"]
        date_2 = v["vignetteValidityEnd"]
        if max_date is None or date_2 > max_date:
            max_date = date_2
        # resp.append(f"Obstaja vinjeta veljavna od {formatEndDate(date_1)[0]} do {formatEndDate(date_2)[0]}, torej je veljavna še {formatEndDate(date_2)[1]} dni")
    return formatEndDate(max_date)[0]


if __name__ == "__main__":
    pass