summaryrefslogblamecommitdiffstats
path: root/server.py
blob: e7b5bc3801cead7cb56fccf6916b0b892f780f4e (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                                      







                                                 








                                                                             



                               
 
                   



                                                
               

                                    
 








                                                                                                                                      


                    




                                                                                                                                      





                                             
                                     










                                                                  



                          


                                                                   
 
                                       
                                 

                                 

                           
                                           
                                             





                                                                                        



                                                                                                 





                                                                                             
                                
            


                                                                     

                                                             








                                                              
 

                          
from flask import Flask, render_template, request, send_from_directory
from helpers import aux
from discord_webhook import DiscordWebhook
import datetime
from flask_limiter import Limiter
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")
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")


def niceTime():
    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 discordLog(msg):
    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"],
    storage_uri="memory://",
)


def setHeaders(request):
    headers = request.form["headers"]
    with open("headers.txt", "w") as f:
        f.write(headers)
    discordLog(f"Headers so bili spremenjeni ob {niceDateTime()}")
    return "OK"


@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("/global", methods=["POST"])
@app.route("/", methods=["POST"])
def apcall():
    if "headers" in request.form:
        setHeaders(request)
        return "OK"
    registrska = request.form["registrska"]
    drzava = request.form.get("drzava", "SI")
    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()}")
        user_language = request.accept_languages.best_match(["sl", "en"])
        if user_language == "en":
            return render_template("index_global.html", valid_until=ar, license_plate=registrska)
        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")

@app.route("/", methods=["GET"])
def index():
    user_language = request.accept_languages.best_match(["sl", "en"])
    if user_language == "en":
        return render_template("index_global.html")
    app.logger.info("Zahteva za domačo stran")
    # discordLog(f"Zahteva za domačo stran ob {niceTime()}")
    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()