]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/lib_update_public_transport.py
Make vao_ext_id_to_ifopt_stop_id( working for whole Austria.
[philipp/winterrodeln/wrpylib.git] / wrpylib / lib_update_public_transport.py
index 612e0d499d553ffc82d905050d812f2b5899e934..8136cda1a2c6b0238ed7c642dacb0f7ee86f99a2 100644 (file)
@@ -1,4 +1,6 @@
+import re
 from datetime import date, timedelta
+from typing import Optional
 
 
 def day_of_time_table_change(year: int):
@@ -12,10 +14,21 @@ def day_of_time_table_change(year: int):
 
 
 def default_query_date(today: date) -> date:
-    """Calculates a work_day date in Dezember, before the schedule change on 12th of December"""
-    query_date = date(today.year, 12, 11)
-    if query_date < today:
-        query_date = date(query_date.year+1, query_date.month, query_date.day)
-    while query_date.isoweekday() in [6, 7]:  # Saturday or Sunday
+    """Calculates a work_day after the date given, but before the schedule change on the second Sunday of December."""
+    change_date = day_of_time_table_change(today.year)
+    query_date = change_date - timedelta(days=2)
+    if query_date.day == 8:
         query_date -= timedelta(days=1)
+    if query_date < today:
+        return default_query_date(today + timedelta(days=14))
     return query_date
+
+
+def vao_ext_id_to_ifopt_stop_id(vao_ext_id: str) -> Optional[str]:
+    """Converts a VAO EXT ID like "476164600" to the IFOPT stop ID like "at:47:61646".
+    In case the vao_ext_id cannot be converted, None is returned."""
+    if match := re.match(r'(4[1-9])(\d{5})00$', vao_ext_id):
+        g1, g2 = match.groups()
+        g2 = int(g2)
+        return f"at:{g1}:{g2}"
+    return None