Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Transport for EMF HAFAS #15

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ config.json
*.pyc
.venv
*.swp
#*.ttf
*.TTF
8 changes: 8 additions & 0 deletions .idea/.gitignore
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add .idea to your global gitignore? We should not have editor configs in this repository.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/package-hafas.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions COPYRIGHT
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed we're missing licensing information for the newly added fonts and the NRE logo.

Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ silkscreen.ttf:

<http://kottke.org/plus/type/silkscreen/>

bus.png, high_speed_rail.png, low_speed_rail.png, tram.png, u_bahn.png:

Part of twemoji <https://github.com/twitter/twemoji>
Licensed under CC-BY 4.0

s_bahn.png

<https://commons.wikimedia.org/wiki/File:S-Bahn-Logo.svg>
Expand Down
Binary file added CymruSansTransport-Medium.otf
Binary file not shown.
Binary file added NRE_Powered_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ json files somewhere else.

In info-beamer just input the path to the cached json files. Authentication
is not supported as of now.

## Emoji

Iconography from [Mutant Standard Emoji](https://mutant.tech).
Binary file modified bus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cache_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def fetch_stop(stop, endpoint):
stop=stop,
minutes=MINUTES,
key=KEY,
language=CONFIG["language"],
)
)
r.raise_for_status()
Expand Down
94 changes: 63 additions & 31 deletions hafas_event.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import re
from datetime import datetime, timedelta

import hashlib
import pytz
from datetime import datetime, timedelta, tzinfo
from helper import Helper
from hosted import CONFIG
from hosted import CONFIG, log
from mapping import CATEGORY_MAPPING, COLOUR_MAPPING, OPERATOR_LABEL_MAPPING

REMOVE = re.escape(CONFIG["remove_string"].strip())
REMOVE = re.escape(CONFIG["remove_string"].strip()) if CONFIG["remove_string"] else None

class FixedOffset(tzinfo):
def __init__(self, offset, name):
self.__offset = timedelta(minutes = offset)
self.__name = name

def utcoffset(self, _dt):
return self.__offset

def tzname(self, _dt):
return self.__name

def dst(self, _dt):
return timedelta(0)


class HAFASEvent:
Expand All @@ -20,6 +35,7 @@ def __init__(self, data):
if product.get("name") and product.get("catCode"):
self.category = product["catCode"]
self.operator = product.get("operatorCode", None)
self.operatorName = product.get("operatorInfo", {}).get("name", None)
self.icon = product.get("icon", None)

symbol = product["name"]
Expand All @@ -33,6 +49,7 @@ def __init__(self, data):
self.symbol = ""
self.category = -1
self.operator = None
self.operatorName = None
self.icon = None

if CONFIG["api_provider"] in CATEGORY_MAPPING:
Expand All @@ -43,12 +60,20 @@ def __init__(self, data):
self.category_icon = ""

scheduled = datetime.strptime(
data["date"] + " " + data["time"], "%Y-%m-%d %H:%M:%S"
data["date"] + " " + data["time"], "%Y-%m-%d %H:%M:%S",
)
if data.get("tz", None) is not None:
scheduled = scheduled.replace(tzinfo=FixedOffset(data["tz"], ""))
else:
scheduled = scheduled.replace(tzinfo=pytz.timezone(CONFIG["timezone"]))
if "rtTime" in data and "rtDate" in data:
self.realtime = datetime.strptime(
data["rtDate"] + " " + data["rtTime"], "%Y-%m-%d %H:%M:%S"
)
if data.get("rtTz", None) is not None:
self.realtime = self.realtime.replace(tzinfo=FixedOffset(data["rtTz"], ""))
else:
self.realtime = self.realtime.replace(tzinfo=pytz.timezone(CONFIG["timezone"]))
diff = self.realtime - scheduled
self.delay = int(diff.total_seconds() / 60)
else:
Expand All @@ -63,18 +88,19 @@ def _clean(self, key):
if key not in self.json:
return None
else:
for possible_match in (
"^(" + REMOVE + "[, -]+)",
"( *\(" + REMOVE + "\))",
"(" + REMOVE + " +)",
):
if re.search(possible_match, self.json[key].strip()):
return re.sub(
possible_match,
"",
self.json[key].strip(),
flags=re.IGNORECASE,
).strip()
if REMOVE:
for possible_match in (
"^(" + REMOVE + "[, -]+)",
"( *\(" + REMOVE + "\))",
"(" + REMOVE + " +)",
):
if re.search(possible_match, self.json[key].strip()):
return re.sub(
possible_match,
"",
self.json[key].strip(),
flags=re.IGNORECASE,
).strip()
return self.json[key].strip()

@property
Expand Down Expand Up @@ -111,13 +137,15 @@ def line_colour(self):
][self.symbol]
elif provider in COLOUR_MAPPING and self.symbol in COLOUR_MAPPING[provider]:
(r, g, b), (font_r, font_g, font_b) = COLOUR_MAPPING[provider][self.symbol]
elif provider in COLOUR_MAPPING and self.operator in COLOUR_MAPPING[provider]:
(r, g, b), (font_r, font_g, font_b) = COLOUR_MAPPING[provider][self.operator]
elif self.icon is not None:
r, g, b = Helper.hex2rgb(self.icon["backgroundColor"]["hex"][1:])
font_r, font_g, font_b = Helper.hex2rgb(
self.icon["foregroundColor"]["hex"][1:]
)
else:
name_hash = md5(self.json["name"]).hexdigest()
name_hash = hashlib.md5(self.json["name"]).hexdigest()
r, g, b = Helper.hex2rgb(name_hash[:6])
h, s, v = Helper.rgb2hsv(r * 255, g * 255, b * 255)
if v > 0.75:
Expand All @@ -140,13 +168,14 @@ def line_colour(self):
@property
def notes(self):
notes = []
for note in self.json["Notes"]["Note"]:
# Apparently:
# A: Accessibility Information
# I: Internal Stuff
# R: Travel information ("faellt aus" etc.)
if note["type"].upper() in ("R",):
notes.append(note["value"])
if "Notes" in self.json:
for note in self.json["Notes"]["Note"]:
# Apparently:
# A: Accessibility Information
# I: Internal Stuff
# R: Travel information ("faellt aus" etc.)
if note["type"].upper() in ("R",):
notes.append(note["value"])
if notes:
return ' | '.join(notes)
return None
Expand All @@ -158,10 +187,13 @@ def stop(self):
@property
def platform(self):
if "platform" in self.json:
return "{} {}".format(
self.json["platform"].get("type", ""),
self.json["platform"].get("text", ""),
).strip()
return {
"type": self.json["platform"].get("type", "X"),
"value": self.json["platform"].get("text", ""),
}
if "track" in self.json:
return self.json["track"]
return ""
return {
"type": "PL",
"value": self.json["track"]
}
return None
27 changes: 18 additions & 9 deletions hafas_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import json
import pytz
from itertools import islice

from requests import get

from hafas_event import HAFASEvent
from helper import Helper, log
from hosted import CONFIG
from mapping import API_MAPPING
from mapping import API_MAPPING, SYMBOL_IS_GROUP


class HAFASFetcher:
def __init__(self):
self.data_sources = CONFIG["data_sources"]
self.tz = pytz.timezone(CONFIG["timezone"])
self.departures = []
self.arrivals = []
self.events = []
Expand All @@ -25,12 +27,17 @@ def fetch_and_parse(self, stop_id):
departures = sorted(departures)
for n, dep in enumerate(departures):
for follow in islice(departures, n + 1, None):
if dep.symbol == follow.symbol and (
(dep.platform != "" and dep.platform == follow.platform)
or dep.destination == follow.destination
):
dep.follow = follow
break
if SYMBOL_IS_GROUP[CONFIG["api_provider"]]:
if dep.symbol == follow.symbol and (
(dep.platform != "" and dep.platform == follow.platform)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems wrong here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what clicking "reformat code" in PyCharm outputs

or dep.destination == follow.destination
):
dep.follow = follow
break
else:
if dep.category == follow.category and dep.destination == follow.destination and dep.id != follow.id:
dep.follow = follow
break
self.departures.extend(departures)

arrivals = []
Expand Down Expand Up @@ -86,6 +93,7 @@ def _fetch(self, stop_id):
stop=stop_id,
minutes=CONFIG["request_hours"] * 60,
key=key,
language=CONFIG["language"],
)

if not self.data_sources == "arrivals":
Expand Down Expand Up @@ -154,17 +162,18 @@ def write_json(self):
"icon": dep.category_icon,
"id": dep.id,
"next_time": (
dep.follow.realtime.strftime("%H:%M") if dep.follow else ""
dep.follow.realtime.astimezone(self.tz).strftime("%H:%M") if dep.follow else ""
),
"next_timestamp": (
Helper.to_unixtimestamp(dep.follow.realtime) if dep.follow else 0
),
"notes": dep.notes,
"operator": dep.operator,
"operator_name": dep.operatorName,
"platform": dep.platform,
"stop": dep.stop,
"symbol": dep.symbol,
"time": dep.realtime.strftime("%H:%M"),
"time": dep.realtime.astimezone(self.tz).strftime("%H:%M"),
"timestamp": Helper.to_unixtimestamp(dep.realtime),
}
departure.update(dep.line_colour)
Expand Down
4 changes: 3 additions & 1 deletion helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime
import pytz
from sys import stderr
from time import mktime

Expand Down Expand Up @@ -46,4 +48,4 @@ def int2rgb(ri, gi, bi):

@staticmethod
def to_unixtimestamp(dt):
return int(mktime(dt.timetuple()))
return int(mktime(dt.astimezone(pytz.utc).utctimetuple()))
Binary file modified high_speed_rail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified low_speed_rail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions mapping.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from mapping_rmv import MAPPING_RMV
from mapping_vbb import MAPPING_VBB
from mapping_tfemf import MAPPING_TFEMF

COLOUR_MAPPING = {
"rmv": MAPPING_RMV,
"vbb": MAPPING_VBB,
"vbb-test": MAPPING_VBB,
"tfemf": MAPPING_TFEMF,
}

API_MAPPING = {
"rmv": "https://www.rmv.de/hapi/{endpoint}?id={stop}&duration={minutes}&format=json&accessId={key}",
"vbb": "https://fahrinfo.vbb.de/fahrinfo/restproxy/2.32/{endpoint}?id={stop}&duration={minutes}&format=json&accessId={key}",
"vbb-test": "https://vbb.demo.hafas.de/fahrinfo/restproxy/2.32/{endpoint}?id={stop}&duration={minutes}&format=json&accessId={key}",
"tfemf": "https://tracking.tfemf.uk/hafas/{endpoint}?id={stop}&duration={minutes}&format=json&lang={language}",
}

CATEGORY_MAPPING = {
Expand All @@ -31,6 +34,15 @@
"5": "high_speed_rail",
"6": "low_speed_rail",
},
"tfemf": {
"unadvertised_ordinary_passenger": "low_speed_rail",
"ordinary_passenger": "low_speed_rail",
"unadvertised_express_passenger": "high_speed_rail",
"express_passenger": "high_speed_rail",
"metro": "u_bahn",
"bus": "bus",
"replacement_bus": "bus"
}
}
CATEGORY_MAPPING["vbb-test"] = CATEGORY_MAPPING["vbb"]

Expand All @@ -39,3 +51,10 @@
"^(Bus )": "",
},
}

SYMBOL_IS_GROUP = {
"vbb": True,
"vbb-test": True,
"rmv": True,
"tfemf": False,
}
46 changes: 46 additions & 0 deletions mapping_tfemf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from helper import Helper

MAPPING_TFEMF = {
"AW": (Helper.int2rgb(255, 1, 0), (1, 1, 1)), # Transport for Wales
"CC": (Helper.int2rgb(188, 0, 135), (1, 1, 1)), # c2c
"CH": (Helper.int2rgb(13, 155, 213), Helper.int2rgb(28, 45, 71)), # Chiltern Railways
"CS": (Helper.int2rgb(0, 57, 65), (1, 1, 1)), # Caledonian Sleeper
"EM": (Helper.int2rgb(43, 12, 35), (1, 1, 1)), # East Midlands Railway
"ES": (Helper.int2rgb(0, 40, 106), (1, 1, 1)), # Eurostar
"GC": (Helper.int2rgb(51, 49, 50), (1, 1, 1)), # Grand Central
"GN": (Helper.int2rgb(67, 22, 92), (1, 1, 1)), # Great Northern
"GR": (Helper.int2rgb(206, 14, 45), (1, 1, 1)), # London North Eastern Railway
"GW": (Helper.int2rgb(10, 73, 62), (1, 1, 1)), # Great Western Railway
"GX": (Helper.int2rgb(200, 40, 40), (1, 1, 1)), # Gatwick Express
"HC": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Heathrow Connect
"HT": (Helper.int2rgb(0, 0, 51), (1, 1, 1)), # Hull Trains
"HX": (Helper.int2rgb(93, 34, 108), (1, 1, 1)), # Heathrow Express
"IL": (Helper.int2rgb(0, 146, 203), (1, 1, 1)), # Island Line
"LD": (Helper.int2rgb(29, 0, 250), (1, 1, 1)), # Lumo
"LE": (Helper.int2rgb(218, 26, 53), (1, 1, 1)), # Greater Anglia
"LM": (Helper.int2rgb(255, 130, 0), (1, 1, 1)), # West Midlands Trains
"LO": (Helper.int2rgb(2380, 118, 35), (1, 1, 1)), # London Overground
"LT": (Helper.int2rgb(225, 37, 31), (1, 1, 1)), # London Underground
"ME": (Helper.int2rgb(255, 235, 55), (0, 0, 0)), # Merseyrail
"NT": (Helper.int2rgb(38, 34, 98), (1, 1, 1)), # Northern Trains
"NY": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # North Yorkshire Moors Railway
"PC": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Private Charter
"RT": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Network Rail
"SE": (Helper.int2rgb(50, 190, 240), Helper.int2rgb(30, 30, 80)), # Southeastern
"SJ": (Helper.int2rgb(0, 155, 119), (1, 1, 1)), # Sheffield Supertram
"SN": (Helper.int2rgb(0, 63, 46), (1, 1, 1)), # Southern
"SP": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Swanage
"SR": (Helper.int2rgb(0, 38, 100), (1, 1, 1)), # ScotRail
"SW": (Helper.int2rgb(0, 146, 203), (1, 1, 1)), # South Western Railway
"TL": (Helper.int2rgb(226, 17, 133), (1, 1, 1)), # Thameslink
"TP": (Helper.int2rgb(32, 35, 78), (1, 1, 1)), # TransPennine Express
"TW": (Helper.int2rgb(255, 201, 73), (0, 0, 0)), # Tyne and Wear Metro
"VT": (Helper.int2rgb(19, 30, 41), (1, 1, 1)), # Avanti West Coast
"WR": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # West Coast Railway Company
"XC": (Helper.int2rgb(202, 18, 63), (1, 1, 1)), # CrossCountry
"XR": (Helper.int2rgb(119, 61, 189), (1, 1, 1)), # Elizabeth Line
"ZB": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Bus Operator
"ZF": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Ferry Operator
"ZM": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # West Somerset Railway
"ZZ": (Helper.int2rgb(0, 0, 0), (1, 1, 1)), # Unknown
}
Loading