diff --git a/main.py b/main.py index b75dcbd..f897f1f 100644 --- a/main.py +++ b/main.py @@ -180,7 +180,7 @@ def main(only_on_coming_evt=False, ignore_module=[], force_coming_event=True, ex image.paste(WeatherTemperatureModule().draw_module(config, 480, 200), (0, 580)) if expand_alerts and more_alerts > 0: - mod, more_alerts = AlertsModule(config, alerts, ignore_module).draw_module(config, 480, 785 - NEXT_STOP_Y - occuped_space) + mod, more_alerts = AlertsModule(alerts, ignore_module).draw_module(config, 480, 785 - NEXT_STOP_Y - occuped_space) image.paste(mod, (0, NEXT_STOP_Y + occuped_space), mod) diff --git a/modules/ratp.py b/modules/ratp.py index e8cd829..bcce290 100644 --- a/modules/ratp.py +++ b/modules/ratp.py @@ -87,7 +87,7 @@ class IDFMAPI: self.fnt_R_path = os.path.join(config.fonts_dir, IDFMAPI.fnt_R_path) self.fnt_RB_path = os.path.join(config.fonts_dir, IDFMAPI.fnt_RB_path) - self.baseurl = "https://prim.iledefrance-mobilites.fr/marketplace/disruptions_bulk" + self.baseurl = "https://prim.iledefrance-mobilites.fr/marketplace/v2" self.apikey = apikey or os.environ["TOKEN_IDFM"] self._cached_file = ".ratp-%s.cache" @@ -156,7 +156,7 @@ class IDFMAPI: if statinfo is None or datetime.fromtimestamp(statinfo.st_mtime, tz=timezone.utc) + timedelta(minutes=self.cache_timeout) < datetime.now(tz=timezone.utc): # Do the request and save it - req = urllib.request.Request(self.baseurl + "/disruptions/v2") + req = urllib.request.Request(self.baseurl + "/navitia/line_reports/line_reports?count=2000") req.headers["apikey"] = self.apikey try: with urllib.request.urlopen(req) as f: @@ -192,25 +192,15 @@ class IDFMAPI: return ret def get_line_weather(self, res, disruptions, mode, line): - for l in res["lines"]: - if l["id"] == "line:IDFM:" + IDFMAPI.lines[mode][line]: - for impactedObject in l["impactedObjects"]: - for disruptionId in impactedObject["disruptionIds"]: - status = "past" - for ap in disruptions[disruptionId]["applicationPeriods"]: - end = datetime.strptime(ap["end"], "%Y%m%dT%H%M%S") - if end < datetime.now(): - continue + for l in res["line_reports"]: + if "line" not in l: + continue - begin = datetime.strptime(ap["begin"], "%Y%m%dT%H%M%S") - if begin > datetime.now(): - status = "future" - continue - - status = "active" - break - disruptions[disruptionId]["status"] = status - yield disruptions[disruptionId] + if "id" in l["line"]: + if l["line"]["id"] == "line:IDFM:" + IDFMAPI.lines[mode][line]: + for link in l["line"]["links"]: + if disruptions[link["id"]]["status"] != "past": + yield disruptions[link["id"]] return None @@ -268,41 +258,40 @@ class RATPWeatherModule: return icon for disruption in weather[mode][line]: - if "message" not in disruption: + if "messages" not in disruption: continue - if disruption["id"] in id_seens: - continue - - if disruption["status"] != "active" and "applicationPeriods" not in disruption: + if disruption["status"] != "active" and "application_periods" not in disruption: continue + title = "" subtitle = "" + content = "" oneline = False - if "applicationPeriods" in disruption: + if "application_periods" in disruption: now = datetime.now() yesterday = now + timedelta(days=-1) nextweek = now + timedelta(days=1) application_periods = [] - for ap in disruption["applicationPeriods"]: - begin = datetime.strptime(ap["begin"], "%Y%m%dT%H%M%S") - end = datetime.strptime(ap["end"], "%Y%m%dT%H%M%S") + for ap in disruption["application_periods"]: + ap["begin"] = datetime.strptime(ap["begin"], "%Y%m%dT%H%M%S") + ap["end"] = datetime.strptime(ap["end"], "%Y%m%dT%H%M%S") - if end < now: + if ap["end"] < now: continue - elif begin > nextweek: + elif ap["begin"] > nextweek: continue - elif len(application_periods) > 0 and application_periods[0]["begin"] + timedelta(hours=16) < begin: + elif len(application_periods) > 0 and application_periods[0]["begin"] + timedelta(hours=16) < ap["begin"]: continue else: - application_periods.append({"begin": begin, "end": end}) + application_periods.append(ap) - if begin < yesterday: + if ap["begin"] < yesterday: oneline = True - if "cause" in disruption and disruption["cause"].lower() == "travaux" and begin > now: + if "cause" in disruption and disruption["cause"] == "travaux" and ap["begin"] > now: oneline = True if len(application_periods) == 0: @@ -313,11 +302,17 @@ class RATPWeatherModule: elif application_periods[0]["end"] > now: subtitle = "Fin " + whenStr(application_periods[0]["end"], now) - id_seens.append(disruption["id"]) + for msg in disruption["messages"]: + if msg["channel"]["name"] == "titre": + title = msg["text"] + elif msg["channel"]["name"] == "moteur": + content = IDFMAPI.fromHTMLDisruption(msg["text"]) + elif len(content) == "": + content = IDFMAPI.fromHTMLDisruption(msg["text"]) yield { - "title": disruption["title"], + "title": title, "subtitle": subtitle, - "description": IDFMAPI.fromHTMLDisruption(disruption["message"]), + "description": content, "icon": alert_icon(mode, line), "oneline": oneline, } @@ -347,27 +342,13 @@ class RATPWeatherModule: states = [] for disruption in weather[mode][line]: - status = "past" - for ap in disruption["applicationPeriods"]: - end = datetime.strptime(ap["end"], "%Y%m%dT%H%M%S") - if end < datetime.now(): - continue - - begin = datetime.strptime(ap["begin"], "%Y%m%dT%H%M%S") - if begin > datetime.now(): - status = "future" - continue - - status = "active" - break - - if status != "active": + if disruption["status"] != "active": continue if "severity" in disruption: - if disruption["cause"] == "TRAVAUX": - states.append(disruption["severity"] + " " + disruption["cause"]) + if disruption["cause"] == "travaux" and "line" not in [x["pt_object"]["embedded_type"] for x in disruption["impacted_objects"]]: + states.append(disruption["severity"]["effect"] + " " + disruption["cause"]) else: - states.append(disruption["severity"]) + states.append(disruption["severity"]["effect"]) fill = "darkgray" if "NO_SERVICE" in states: