From f6c60b41dd7055c04f9844386c59f9a4edf97748 Mon Sep 17 00:00:00 2001 From: MathiasDPX Date: Mon, 30 Dec 2024 19:07:15 +0100 Subject: [PATCH] Handle curl --- app.py | 12 ++++++++---- modules/{Classes.py => api.py} | 0 modules/curl.py | 20 ++++++++++++++++++++ monitoring.py => modules/monitoring.py | 2 -- 4 files changed, 28 insertions(+), 6 deletions(-) rename modules/{Classes.py => api.py} (100%) create mode 100644 modules/curl.py rename monitoring.py => modules/monitoring.py (98%) diff --git a/app.py b/app.py index c84fd33..7bcbd4a 100644 --- a/app.py +++ b/app.py @@ -3,13 +3,14 @@ from datetime import timedelta from dotenv import load_dotenv from threading import Thread from os import getenv -import monitoring import html load_dotenv() # IMPORT DES MODULES -from modules.Classes import * +from modules.api import * +from modules.curl import * +import modules.monitoring as monitoring theaters = [Theater(data["node"]) for data in requests.get("https://www.allocine.fr/_/localization_city/Brest").json()["values"]["theaters"]] @@ -55,7 +56,6 @@ def getShowtimes(date): return data - showtimes = [] for i in range(0, 7): day_showtimes = getShowtimes(datetime.today() + timedelta(days=i)) @@ -87,12 +87,16 @@ def home(): if delta > 6: delta = 6 if delta < 0: delta = 0 + useragent = request.headers.get('User-Agent') Thread(target=monitoring.log, kwargs={ 'ip': request.environ.get("HTTP_X_FORWARDED_FOR", request.remote_addr), - 'useragent': request.headers.get('User-Agent'), + 'useragent': useragent, 'day': delta }).start() + if useragent.startswith("curl/"): + return handle_curl(showtimes[delta]) + dates = [] for i in range(0, 7): diff --git a/modules/Classes.py b/modules/api.py similarity index 100% rename from modules/Classes.py rename to modules/api.py diff --git a/modules/curl.py b/modules/curl.py new file mode 100644 index 0000000..00013b4 --- /dev/null +++ b/modules/curl.py @@ -0,0 +1,20 @@ +border_top = "╔" + "═" * 58 + "╗" +border_bottom = "╚" + "═" * 58 + "╝" +separator = "╠" + "═" * 58 + "╣" + +def handle_curl(movies): + table = [border_top, "║{:^58}║".format("CinéBrest"), separator] + + for film in movies: + title_line = f"║ {film['title']:<57}║" + table.append(title_line) + + for cinema, seances in film['seances'].items(): + cinema_line = f"║ ├─ {cinema:<54}║" + seances_line = f"║ │ └─ : {', '.join(seances):<43}║" + table.extend([cinema_line, seances_line]) + + table.append(separator) + + table[-1] = border_bottom + return "\n".join(table) \ No newline at end of file diff --git a/monitoring.py b/modules/monitoring.py similarity index 98% rename from monitoring.py rename to modules/monitoring.py index 6b6a736..c34c3db 100644 --- a/monitoring.py +++ b/modules/monitoring.py @@ -159,7 +159,5 @@ def log(ip:str, useragent:str, day:int) -> bool: os_family = os.family if os != None else "unknown" browser_family = browser.family if browser != None else "unknown" - print(os_family, browser_family) - success = db.execute_query(f"INSERT INTO cinema_queries (ip, time, browser, os, day) VALUES (\'{ip}\', current_timestamp, \'{browser_family}\', \'{os_family}\', {day});") return success \ No newline at end of file