diff --git a/.gitignore b/.gitignore index 961886a..9ddc8eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ .darksky-*.cache +.ratp-*.cache screen.png \ No newline at end of file diff --git a/fonts/Parisine-Bold.ttf b/fonts/Parisine-Bold.ttf new file mode 100644 index 0000000..721b609 Binary files /dev/null and b/fonts/Parisine-Bold.ttf differ diff --git a/fonts/Parisine-Regular.ttf b/fonts/Parisine-Regular.ttf new file mode 100644 index 0000000..1d2b0eb Binary files /dev/null and b/fonts/Parisine-Regular.ttf differ diff --git a/icons/buses.png b/icons/buses.png new file mode 100644 index 0000000..1794b34 Binary files /dev/null and b/icons/buses.png differ diff --git a/icons/metros.png b/icons/metros.png new file mode 100644 index 0000000..9ac8785 Binary files /dev/null and b/icons/metros.png differ diff --git a/icons/rers.png b/icons/rers.png new file mode 100644 index 0000000..1a5ba05 Binary files /dev/null and b/icons/rers.png differ diff --git a/icons/tramways.png b/icons/tramways.png new file mode 100644 index 0000000..917cdf4 Binary files /dev/null and b/icons/tramways.png differ diff --git a/main.py b/main.py index 0a7e5ed..e79b487 100644 --- a/main.py +++ b/main.py @@ -73,6 +73,11 @@ def main(): mod = WeatherAlertsModule().draw_module(config, 480, 200) image.paste(mod, (0, 580-mod.height-5), mod) + # RATP weather + from modules.ratp import RATPWeatherModule + ratp = RATPWeatherModule().draw_module(config, int(480/1.6), 94) + image.paste(ratp, (480-int(480/1.6), 205)) + # températures from modules.weather import WeatherTemperatureModule image.paste(WeatherTemperatureModule().draw_module(config, 480, 200), (0, 580)) diff --git a/modules/ratp.py b/modules/ratp.py new file mode 100644 index 0000000..4bc8ddd --- /dev/null +++ b/modules/ratp.py @@ -0,0 +1,87 @@ +from datetime import datetime, timedelta, timezone +import json +import os +import urllib.parse +import urllib.request + +from PIL import Image, ImageDraw, ImageFont + +class RATPAPI: + + fnt_R_path = "./fonts/Parisine-Regular.ttf" + fnt_RB_path = "./fonts/Parisine-Bold.ttf" + + def __init__(self): + self.baseurl = "https://ratp.p0m.fr/api" + + self._cached_file = ".ratp-%s.cache" + + def get_weather(self): + # Read the mod time + statinfo = None + try: + statinfo = os.stat(self._cached_file % "traffic") + except: + pass + + if statinfo is None or datetime.fromtimestamp(statinfo.st_mtime, tz=timezone.utc) + timedelta(minutes=5) < datetime.now(tz=timezone.utc): + # Do the request and save it + with urllib.request.urlopen(self.baseurl + "/traffic") as f: + with open(self._cached_file % "traffic", 'wb') as fd: + fd.write(f.read()) + + # Retrieve cached data + res = {} + with open(self._cached_file % "traffic") as f: + res = json.load(f) + + return res["result"] + + +class RATPWeatherModule: + + def draw_module(self, config, width, height, line_height=19): + image = Image.new('RGB', (width, height), '#fff') + draw = ImageDraw.Draw(image) + + fnt_icon = ImageFont.truetype(RATPAPI.fnt_RB_path, line_height-1) + + weather = RATPAPI().get_weather() + + align_x = 0 + align_y = 0 + for mode in weather: + if mode != "rers": + align_x = 0 + + + # display mode icon + icon = Image.open("icons/" + mode + ".png").resize((line_height, line_height)) + image.paste(icon, (align_x,align_y), icon) + + align_x += line_height + 10 + + for line in weather[mode]: + if align_x + line_height >= width: + align_x = line_height + 10 + align_y += line_height + 6 + + fill = "gray" if line["slug"] == "normal" else "black" + + if mode == "metros": + draw.ellipse((align_x - 1, align_y - 1, align_x + line_height + 1, align_y + line_height + 1), fill=fill) + elif mode == "tramways": + draw.rectangle((align_x - 2, align_y - 2, align_x + line_height, align_y), fill=fill) + draw.rectangle((align_x - 1, align_y + line_height - 1, align_x + line_height + 1, align_y + line_height + 1), fill=fill) + else: + draw.rectangle((align_x - 1, align_y - 1, align_x + line_height + 1, align_y + line_height + 1), fill=fill) + draw.text((align_x + 1 + line_height / 2, align_y + line_height / 2), line["line"], fill="white" if mode != "tramways" else "black", anchor="mm", font=fnt_icon) + + align_x += line_height + 5 + + if mode != "metros": + align_y += line_height + 10 + else: + align_y += 10 + + return image