Repository: stigok/ruterstop

Issue:
Sett en justerbar minstegrense for tid til neste avgang
Dersom det tar 2 minutter å gå til stoppestedet er jeg ikke interessert i å se de som går om 0 min

Discussion:

Patch:
diff --git a/ruterstop.py b/ruterstop.py
index 1ad7ea5..8475531 100644
--- a/ruterstop.py
+++ b/ruterstop.py
@@ -161,6 +161,8 @@ def main(argv, *, stdout=sys.stdout):
                      help="find stops at https://stoppested.entur.org (guest:guest)")
     par.add_argument('--direction', choices=["inbound", "outbound"],
                      help="filter direction of departures")
+    par.add_argument('--min-eta', type=int, default=0,
+                     help="minimum ETA of departures to return")
     par.add_argument('--server', action="store_true",
                      help="start a HTTP server")
     par.add_argument('--host', type=str, default="0.0.0.0",
@@ -186,19 +188,27 @@ def main(argv, *, stdout=sys.stdout):
     else:
         directions = [args.direction]
 
+    def get_departures():
+        raw_stop = get_cached_realtime_stop(stop_id=args.stop_id)
+        deps = parse_departures(raw_stop)
+
+        # Filter departures with minimum time treshold
+        treshold = datetime.now() + timedelta(minutes=args.min_eta)
+
+        return [d for d in deps if d.eta >= treshold]
+
     if args.server:
         @bottle.route("/")
         def _():
-            raw_stop = get_cached_realtime_stop(stop_id=args.stop_id)
-            deps = parse_departures(raw_stop)
+            deps = get_departures()
             return '\n'.join([str(d) for d in deps if d.direction in directions])
 
         bottle.run(host=args.host, port=args.port)
 
     # Otherwise print out stop information and exit
     else:
-        raw_stop = get_cached_realtime_stop(stop_id=args.stop_id)
-        for dep in parse_departures(raw_stop):
+        deps = get_departures()
+        for dep in deps:
             print(dep, file=stdout)
 
 


Test patch:
diff --git a/tests/test_ruterstop.py b/tests/test_ruterstop.py
index a95aefc..5368631 100644
--- a/tests/test_ruterstop.py
+++ b/tests/test_ruterstop.py
@@ -173,3 +173,12 @@ class CommandLineInterfaceTestCase(unittest.TestCase):
 
             actual = filter(None, stdout.getvalue().split('\n')) # remove empty lines
             self.assertEqual(list(actual), self.expected_output)
+
+    def test_adjustable_minimum_time(self):
+        stdout = StringIO()
+
+        with freeze_time(self.first_departure_time):
+            # Call CLI with custom args
+            api.main([None, "--stop-id", "1337", "--min-eta", "2"], stdout=stdout)
+            lines = filter(None, stdout.getvalue().split('\n')) # remove empty lines
+            self.assertEqual(list(lines), self.expected_output[3:]) # skip first 3