From ef703bdf734c1c3da44eb3859d2c3d572e418fdb Mon Sep 17 00:00:00 2001 From: Gregor Kikelj Date: Sat, 18 Mar 2023 20:37:18 +0100 Subject: tuje tablice --- helpers.py | 46 ++++-- server.py | 118 ++++++++++---- templates/index.html | 389 ++++++++++++++++++++++++-------------------- templates/index_global.html | 234 ++++++++++++++++++++++++++ 4 files changed, 565 insertions(+), 222 deletions(-) create mode 100644 templates/index_global.html diff --git a/helpers.py b/helpers.py index 414cb87..843d257 100644 --- a/helpers.py +++ b/helpers.py @@ -3,6 +3,7 @@ import json from datetime import datetime, timezone, timedelta from functools import lru_cache + def formatEndDate(st): if st is None: return (None, None) @@ -26,30 +27,51 @@ def get_headers(): 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): +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["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): - 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)["vignetteValidationResult"] +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"]}' diff --git a/server.py b/server.py index aebf98c..6f968f3 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,4 @@ -from flask import Flask, jsonify, render_template, request, send_from_directory -import os +from flask import Flask, render_template, request, send_from_directory from helpers import aux from discord_webhook import DiscordWebhook import datetime @@ -8,36 +7,53 @@ from flask_limiter.util import get_remote_address import logging # Logs to both console and app.log file -logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s") -formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s") -logger = logging.getLogger('server') -handler = logging.FileHandler('app.log') +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s", +) +formatter = logging.Formatter( + "%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s" +) +logger = logging.getLogger("server") +handler = logging.FileHandler("app.log") handler.setFormatter(formatter) handler.setLevel(logging.INFO) logger.addHandler(handler) + def niceDateTime(): - time = datetime.datetime.now() - return time.strftime("%H:%M:%S -- %d/%m/%Y") + time = datetime.datetime.now() + return time.strftime("%H:%M:%S -- %d/%m/%Y") + + def niceTime(): - time = datetime.datetime.now() - return time.strftime("%H:%M:%S") + time = datetime.datetime.now() + return time.strftime("%H:%M:%S") -def spamDiscord(logs = ""): - content = f"@everyone, napaka na strani ob {niceDateTime()}: \n {logs}" - webhook = DiscordWebhook(url='https://discord.com/api/webhooks/1062432166950219838/tgl04gFFIMkrASeWX2AlETdsgdTHU3CWufzE1NZQMLQ2eZGVeQyGXj4AAt0k4Dj467B'+'d', content=content) - webhook.execute() + +def spamDiscord(logs=""): + content = f"@everyone, napaka na strani ob {niceDateTime()}: \n {logs}" + webhook = DiscordWebhook( + url="https://discord.com/api/webhooks/1062432166950219838/tgl04gFFIMkrASeWX2AlETdsgdTHU3CWufzE1NZQMLQ2eZGVeQyGXj4AAt0k4Dj467B" + + "d", + content=content, + ) + webhook.execute() def discordLog(msg): - DiscordWebhook(url="https://discord.com/api/webhooks/1062474351196262571/V6M_vfhPy9QEEbM-b1W7qxBr2k5olpyeOY1O2RoKZ178c0Fs9_vYzKqJwES3o3suLSF"+"o", content=msg).execute() + DiscordWebhook( + url="https://discord.com/api/webhooks/1062474351196262571/V6M_vfhPy9QEEbM-b1W7qxBr2k5olpyeOY1O2RoKZ178c0Fs9_vYzKqJwES3o3suLSF" + + "o", + content=msg, + ).execute() app = Flask(__name__, static_folder="static") limiter = Limiter( get_remote_address, app=app, - default_limits=["50 per minute"], + default_limits=["50 per minute"], storage_uri="memory://", ) @@ -49,34 +65,68 @@ def setHeaders(request): discordLog(f"Headers so bili spremenjeni ob {niceDateTime()}") return "OK" -@app.route('/favicon.ico') -@app.route('/robots.txt') -@app.route('/sitemap.xml') + +@app.route("/favicon.ico") +@app.route("/robots.txt") +@app.route("/sitemap.xml") def static_from_root(): return send_from_directory(app.static_folder, request.path[1:]) -@app.route('/', methods=['POST']) + +@app.route("/", methods=["POST"]) def apcall(): if "headers" in request.form: - setHeaders(request) - return "OK" + setHeaders(request) + return "OK" registrska = request.form["registrska"] try: - ar = aux(registrska) - app.logger.info(f"Registrska \"{registrska}\" je veljavna do {ar} ob {niceTime()}") - # discordLog(f"Registrska \"{registrska}\" je veljavna do {ar} ob {niceTime()}") - return render_template('index.html', valid_until = ar, license_plate = registrska) + ar = aux(registrska) + app.logger.info( + f'Registrska "{registrska}" je veljavna do {ar} ob {niceTime()}' + ) + # discordLog(f"Registrska \"{registrska}\" je veljavna do {ar} ob {niceTime()}") + return render_template("index.html", valid_until=ar, license_plate=registrska) except Exception as e: - spamDiscord(str(e)) - app.logger.error(f"\nNapaka pri {registrska} ob {niceTime()}") - # discordLog(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!\nNapaka pri {registrska} ob {niceTime()}") - return render_template('failure.html') + spamDiscord(str(e)) + app.logger.error(f"\nNapaka pri {registrska} ob {niceTime()}") + # discordLog(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!\nNapaka pri {registrska} ob {niceTime()}") + return render_template("failure.html") + -@app.route('/', methods=['GET']) +@app.route("/global", methods=["POST"]) +def apcall_global(): + print(request.form) + registrska = request.form["registrska"] + drzava = request.form["drzava"] + try: + ar = aux(registrska, drzava) + app.logger.info( + f'Registrska "{registrska}" je veljavna do {ar} ob {niceTime()}' + ) + # discordLog(f"Registrska \"{registrska}\" je veljavna do {ar} ob {niceTime()}") + return render_template( + "index_global.html", valid_until=ar, license_plate=registrska + ) + except Exception as e: + spamDiscord(str(e)) + app.logger.error(f"\nNapaka pri {registrska} ob {niceTime()}") + # discordLog(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!\nNapaka pri {registrska} ob {niceTime()}") + return render_template("failure.html") + + +@app.route("/", methods=["GET"]) def index(): app.logger.info("Zahteva za domačo stran") # discordLog(f"Zahteva za domačo stran ob {niceTime()}") - return render_template('index.html') + return render_template("index.html") + + +@app.route("/global", methods=["GET"]) +def global_index(): + app.logger.info("Zahteva za globalno stran") + # discordLog(f"Zahteva za globalno stran ob {niceTime()}") + return render_template("index_global.html") + -if __name__ == '__main__': - app.run() \ No newline at end of file +if __name__ == "__main__": + app.run() diff --git a/templates/index.html b/templates/index.html index d552e94..a6c365c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,182 +1,219 @@ - - - - - - - - - - - eVinjeta: Hitro preveri veljavnost evinjete - - - - - - - - - - - - -
-

eVINJETA

-
- Preveri veljavnost svoje eVinjete: - * Podatke dobimo iz DARS strežnikov. Nismo povezani z DARSom. -
-
-
-

Registrska tablica:

- -
-
- -
-
- {% if valid_until %} -
-

Vinjeta za registrsko tablico {{ license_plate }} je veljavna vključno do: {{ valid_until }}

-
- {% elif license_plate %} -
-

Registrska tablica "{{ license_plate }}" nima veljavne vinjete!

-
- {% endif %} -