"""Plex-style F1 race + season poster rendering. Produces 1000x1500 PNGs (Plex's recommended TV-poster ratio) by compositing: - Top + bottom checker-flag strips - "FORMULA 1" wordmark (red) - Round / year ribbon - Grand Prix name (bold, large) - Rasterised track outline (white) for race posters - Race date Output is cached under cache/posters/ keyed by (season, round, layout_slug) so cold paths stay cheap. """ import hashlib import io import json import logging import math import os import threading import time import urllib.request from concurrent.futures import ThreadPoolExecutor from enum import Enum from pathlib import Path from typing import Optional import cairosvg from PIL import Image, ImageDraw, ImageFont from circuits_service import CircuitService from ergast_service import ErgastService # Where season background images live. Hosted by the dilbert files-api module — # one landscape JPG per F1 season (key for the season-card grid in Dilbert's UI). SEASON_BG_URL_TEMPLATE = "https://files-api.novox.be/dilbert/sports/formula1/cars/{season}.jpg" class BackgroundStyle(str, Enum): """Selectable backdrop for race posters.""" AERIAL = "aerial" # Esri World Imagery satellite tiles (default) OSM = "osm" # OpenStreetMap Carto street-map tiles DARK = "dark" # Esri Dark Gray Canvas — minimalist black-and-grey map LIGHT = "light" # Esri Light Gray Canvas — minimalist light/grey map NONE = "none" # Plain black, no map fetch — fast fallback # XYZ tile templates. None require API keys at our volume. CARTO's basemaps do since 2026: they # answer every tile with an "API KEY REQUIRED" picture, so dark and light are Esri's grey canvases. # max_zoom is the deepest level a provider draws; deeper, Esri's canvases answer a "Map data not yet # available" picture, so a poster never asks deeper than its provider's max_zoom. TILE_PROVIDERS = { BackgroundStyle.AERIAL: { "url": "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}", "attribution": "Esri, Maxar, Earthstar Geographics", "max_zoom": 19, "concurrency": 6, }, BackgroundStyle.OSM: { "url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png", "attribution": "© OpenStreetMap contributors", "max_zoom": 19, "concurrency": 2, # OpenStreetMap's tile usage policy: at most two connections }, BackgroundStyle.DARK: { "url": "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Base/MapServer/tile/{z}/{y}/{x}", "attribution": "Esri, HERE, Garmin, © OpenStreetMap contributors", "max_zoom": 16, "concurrency": 6, }, BackgroundStyle.LIGHT: { "url": "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}", "attribution": "Esri, HERE, Garmin, © OpenStreetMap contributors", "max_zoom": 16, "concurrency": 6, }, } # Tiles are kept on disk under cache/tiles/