Handle curl

This commit is contained in:
Mathias DUPEUX 2024-12-30 19:07:15 +01:00
parent 215b5cf275
commit f6c60b41dd
4 changed files with 28 additions and 6 deletions

12
app.py
View file

@ -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):

20
modules/curl.py Normal file
View file

@ -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)

View file

@ -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