gancio-flyers/app.py
2023-10-09 17:52:29 +02:00

61 خطوط
1.9 KiB
Python
Executable file

#!/usr/bin/env python3
import requests
import json
import datetime
from jinja2 import Environment, FileSystemLoader
from flask import Flask
from flask import render_template, request
app = Flask(__name__)
gancio_instance = 'balotta.org'
template_name = 'simple'
events_ep = f'https://{gancio_instance}/api/events'
def day2giorno(day):
days = {
'Monday': 'Lunedì',
'Tuesday': 'Martedì',
'Wednesday': 'Mercoledì',
'Thursday': 'Giovedì',
'Friday': 'Venerdì',
'Saturday': 'Sabato',
'Sunday': 'Domenica'
}
if day in days:
return days[day]
def fetch_events(api_endpoint):
events = requests.get(events_ep).json()
for event in events:
start_dt = datetime.datetime.fromtimestamp(event['start_datetime'])
start_day = f"{day2giorno(start_dt.strftime('%A'))} {start_dt.strftime('%d')}"
start_hour = start_dt.strftime("%H:%M")
when = f"{start_day} dalle {start_hour}"
if event['end_datetime']:
end_dt = datetime.datetime.fromtimestamp(event['end_datetime'])
end_day = f"{day2giorno(start_dt.strftime('%A'))} {end_dt.strftime('%d')}"
end_hour = end_dt.strftime("%H:%M")
if event['multidate']:
when = f"da {when} a {end_day}"
when += " alle "
when += end_hour
event['when'] = when
event['media'][0]['thumbnailPosition'] = f"{(event['media'][0]['focalpoint'][0] + 1) * 50}% {(event['media'][0]['focalpoint'][1] + 1) * 50}%"
return events
@app.route("/")
def main():
events = fetch_events(events_ep)
return render_template('index.html', events=events)
@app.route("/flyer")
def flyer():
relevant_events = request.args.getlist('events', type=int)
events = fetch_events(events_ep)
events = [ e for e in events if e['id'] in relevant_events ]
return render_template(f'flyers/{template_name}.html', events=events)