aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-03-10 22:05:02 +0100
committerMartin Polden <mpolden@mpolden.no>2022-03-10 22:05:02 +0100
commite006c85f90b4275f901ac7960e9905f64c6af505 (patch)
tree0249c466a69a66a450935385cb91135d7b6f3418
parent285a331e9809088c547e218eea96e791dc7a8f88 (diff)
vaernesekspressen: Support stop IDs
-rw-r--r--docs/WIDGETS.md8
-rw-r--r--jarvis/jobs/vaernesekspressen.py11
-rwxr-xr-xjarvis/tests.py17
3 files changed, 28 insertions, 8 deletions
diff --git a/docs/WIDGETS.md b/docs/WIDGETS.md
index af81f15..282d6b2 100644
--- a/docs/WIDGETS.md
+++ b/docs/WIDGETS.md
@@ -289,7 +289,8 @@ departures from the configured bus stop.
JOBS["vaernesekspressen"] = {
"enabled": True,
"interval": 600,
- "from_stop": "FB 73 Nidarosdomen",
+ "from_stop_id": 133,
+ # or "from_stop": "FB 73 Nidarosdomen",
}
```
@@ -299,6 +300,11 @@ See the [Vaernesekspressen website](https://www.vaernesekspressen.no) for valid
stop names. Trondheim airport is the only possible destination and is thus not
configurable.
+The `from_stop_id` field is the ID of a bust stop. When set this takes
+precedence over `from_stop`. This is likely more robust in practice as the stop
+name may change occasionally. Stop IDs can be found at
+https://www.vaernesekspressen.no/Umbraco/Api/TicketOrderApi/GetStops?routeId=31.
+
yr
--
Displays weather data from https://www.yr.no. The `url` field specifies the
diff --git a/jarvis/jobs/vaernesekspressen.py b/jarvis/jobs/vaernesekspressen.py
index 8990b48..214a937 100644
--- a/jarvis/jobs/vaernesekspressen.py
+++ b/jarvis/jobs/vaernesekspressen.py
@@ -9,12 +9,15 @@ from jobs import AbstractJob
class Vaernesekspressen(AbstractJob):
def __init__(self, conf):
- self.airport_id = 113 # Vaernes is the the only supported destionation
- self.from_stop = conf["from_stop"]
+ self.airport_id = 113 # Vaernes is the the only supported destination
+ self.from_stop = conf.get("from_stop")
+ self.from_stop_id = conf.get("from_stop_id")
self.interval = conf["interval"]
self.timeout = conf.get("timeout")
self.base_url = conf.get("base_url", "https://www.vaernesekspressen.no")
self.now = datetime.now
+ if self.from_stop is None and self.from_stop_id is None:
+ raise ValueError("Either from_stop or from_stop_id must be set")
def _find_stop_id(self):
url = "{}/Umbraco/Api/TicketOrderApi/GetStops".format(self.base_url)
@@ -73,7 +76,9 @@ class Vaernesekspressen(AbstractJob):
return re.sub(r"^FB \d+ ", "", name)
def get(self):
- stop_id = self._find_stop_id()
+ stop_id = self.from_stop_id
+ if stop_id is None:
+ stop_id = self._find_stop_id()
now = self.now()
departures = self._departures(stop_id, now)
if len(departures) < 2:
diff --git a/jarvis/tests.py b/jarvis/tests.py
index 5e00580..eca28a9 100755
--- a/jarvis/tests.py
+++ b/jarvis/tests.py
@@ -451,10 +451,19 @@ class Vaernesekspressen(unittest.TestCase):
self.p = Process(target=self.server.serve_forever)
self.p.start()
- def test_get(self):
- f = vaernesekspressen.Vaernesekspressen(
- {"interval": None, "from_stop": "fb 73 nidarosdomen", "base_url": self.url}
- )
+ def test_get_by_name(self):
+ self._get(stop_name="fb 73 nidarosdomen")
+
+ def test_get_by_id(self):
+ self._get(stop_id=131)
+
+ def _get(self, stop_id=None, stop_name=None):
+ config = {"interval": None, "base_url": self.url}
+ if stop_id is not None:
+ config["from_stop_id"] = stop_id
+ if stop_name is not None:
+ config["from_stop"] = stop_name
+ f = vaernesekspressen.Vaernesekspressen(config)
f.now = lambda: datetime(2020, 2, 1, 10)
data = f.get()
self.assertEqual(9, len(data["departures"]))