grainParisArt/app.py

125 lines
3.8 KiB
Python
Raw Permalink Normal View History

2024-12-31 11:53:08 +00:00
from flask import Flask, render_template, request, redirect, url_for
2024-10-09 17:15:56 +00:00
from datetime import timedelta
from dotenv import load_dotenv
2024-12-31 11:53:08 +00:00
from zoneinfo import ZoneInfo
2024-12-30 02:31:25 +00:00
from threading import Thread
2024-10-09 17:15:56 +00:00
from os import getenv
2024-12-21 00:56:35 +00:00
import html
2024-09-16 11:29:02 +00:00
2024-10-09 17:15:56 +00:00
load_dotenv()
# IMPORT DES MODULES
2024-12-30 18:07:15 +00:00
from modules.api import *
from modules.curl import *
import modules.monitoring as monitoring
2024-09-18 16:41:31 +00:00
2024-10-09 17:15:56 +00:00
theaters = [Theater(data["node"]) for data in
requests.get("https://www.allocine.fr/_/localization_city/Brest").json()["values"]["theaters"]]
2024-12-21 00:56:35 +00:00
theaters += [Theater(data["node"]) for data in requests.get("https://www.allocine.fr/_/localization_city/Landerneau").json()["values"]["theaters"]]
2024-11-27 11:47:14 +00:00
2024-12-31 11:53:08 +00:00
timezone = ZoneInfo(getenv("TIMEZONE"))
2024-09-18 16:41:31 +00:00
def getShowtimes(date):
2024-10-09 17:15:56 +00:00
showtimes: list[Showtime] = []
2024-09-18 16:41:31 +00:00
for theater in theaters:
showtimes.extend(theater.getShowtimes(date))
data = {}
for showtime in showtimes:
movie = showtime.movie
theater = showtime.theater
if showtime.movie.title not in data.keys():
data[movie.title] = {
"title": movie.title,
"duree": movie.runtime,
"genres": ", ".join(movie.genres),
"casting": ", ".join(movie.cast),
"realisateur": movie.director,
2024-12-21 00:56:35 +00:00
"synopsis": html.unescape(movie.synopsis),
2024-09-18 16:41:31 +00:00
"affiche": movie.affiche,
"director": movie.director,
2024-09-18 18:32:49 +00:00
"wantToSee": movie.wantToSee,
"url": f"https://www.allocine.fr/film/fichefilm_gen_cfilm={movie.id}.html",
2024-09-18 16:41:31 +00:00
"seances": {}
}
if theater.name not in data[movie.title]["seances"].keys():
data[movie.title]["seances"][theater.name] = []
data[movie.title]["seances"][theater.name].append(showtime.startsAt.strftime("%H:%M"))
2024-09-18 18:32:49 +00:00
data = data.values()
data = sorted(data, key=lambda x: x["wantToSee"], reverse=True)
2024-09-18 16:41:31 +00:00
return data
2024-09-18 16:55:11 +00:00
showtimes = []
for i in range(0, 7):
2024-12-31 11:53:08 +00:00
day_showtimes = getShowtimes(datetime.now(timezone) + timedelta(days=i))
2024-09-19 18:22:41 +00:00
showtimes.append(day_showtimes)
2024-10-09 17:15:56 +00:00
print(f"{len(day_showtimes)} séances récupéré {i + 1}/7!")
2024-09-18 16:41:31 +00:00
2024-12-30 01:49:08 +00:00
def translate_month(num: int) -> str:
months = ["janv", "févr", "mars", "avr", "mai", "juin",
"juil", "août", "sept", "oct", "nov", "déc"]
return months[num - 1] if 1 <= num <= len(months) else "???"
def translate_day(weekday: int) -> str:
days = ["lun", "mar", "mer", "jeu", "ven", "sam", "dim"]
return days[weekday] if 0 <= weekday < len(days) else "???"
2024-09-16 11:29:02 +00:00
2024-10-09 17:15:56 +00:00
app = Flask(__name__)
2024-12-30 02:06:15 +00:00
@app.route('/healthcheck')
def healthcheck():
return 'ok'
2024-12-31 11:53:08 +00:00
@app.route('/<int:day>')
def special_day(day:int):
return redirect(url_for("home", delta=day))
2024-09-16 11:29:02 +00:00
@app.route('/')
def home():
2024-09-18 16:41:31 +00:00
delta = request.args.get("delta", default=0, type=int)
2024-09-16 11:29:02 +00:00
2024-09-18 16:41:31 +00:00
if delta > 6: delta = 6
if delta < 0: delta = 0
2024-09-16 11:29:02 +00:00
2024-12-30 18:07:15 +00:00
useragent = request.headers.get('User-Agent')
2024-12-30 02:31:25 +00:00
Thread(target=monitoring.log, kwargs={
'ip': request.environ.get("HTTP_X_FORWARDED_FOR", request.remote_addr),
2024-12-30 18:07:15 +00:00
'useragent': useragent,
2024-12-30 02:31:25 +00:00
'day': delta
}).start()
2024-12-30 01:57:47 +00:00
2024-12-30 18:07:15 +00:00
if useragent.startswith("curl/"):
2024-12-31 11:53:08 +00:00
day = datetime.now(timezone) + timedelta(delta)
return handle_curl(showtimes[delta], f"{day.day} {translate_month(day.month)}")
2024-12-30 18:07:15 +00:00
2024-09-18 16:41:31 +00:00
dates = []
2024-09-16 11:29:02 +00:00
2024-10-09 17:15:56 +00:00
for i in range(0, 7):
2024-12-31 11:53:08 +00:00
day = datetime.now(timezone) + timedelta(i)
2024-09-18 16:41:31 +00:00
dates.append({
2024-12-30 01:49:08 +00:00
"jour": translate_day(day.weekday()),
2024-09-18 16:41:31 +00:00
"chiffre": day.day,
2024-12-30 01:49:08 +00:00
"mois": translate_month(day.month),
2024-10-09 17:15:56 +00:00
"choisi": i == delta,
2024-09-18 16:41:31 +00:00
"index": i
})
2024-09-16 11:29:02 +00:00
2024-10-09 17:15:56 +00:00
return render_template('index.html',
films=showtimes[delta],
dates=dates,
JAWG_API_KEY=getenv("JAWG_API_KEY"))
2024-09-16 11:29:02 +00:00
if __name__ == '__main__':
2024-10-19 12:47:30 +00:00
app.run(host=getenv("HOST"), port=getenv("PORT"))