Skip to content

Commit

Permalink
Merge pull request #66 from stigok/release-0.4.0
Browse files Browse the repository at this point in the history
Versjon 0.4.0
  • Loading branch information
stigok authored Jun 16, 2020
2 parents 8c01757 + 07b959b commit 147f8e5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Et program som viser sanntidsinformasjon for stoppesteder i Oslo og Akershus.
- Bruk filtre som `--direction`, `--grouped` og `--min-eta`
- Start en HTTP server med `--server`
- Søk etter stoppesteder med `--search-stop`
- Sett når du ønsker å se klokkeslett med `--long-eta`
- Bruk `--help` for full hjelp

Innspill, tanker og feilmeldinger mottas med glede!
Expand Down Expand Up @@ -75,17 +76,16 @@ $ ruterstop --server
Stoppested og filtre velges i adressen til spørringen

```
$ curl localhost:4000/6013?direction=outbound
25 Majorstuen naa
31 Snaroeya naa
$ curl localhost:4000/6013?direction=outbound&long_eta=10
31 Fornebu naa
31 Snaroeya 8 min
25 Majorstuen 16 min
31 Fornebu 18 min
31 Snaroeya 28 min
31 Fornebu 38 min
25 Majorstuen 46 min
31 Snaroeya 48 min
31 Snaroeya 5 min
31 Fornebu 8 min
31 Fornebu 10 min
25 Majorstuen 20:21
31 Snaroeya 20:24
31 Snaroeya 20:36
25 Majorstuen 20:36
31 Fornebu 20:42
```

## Utvikling
Expand Down
29 changes: 19 additions & 10 deletions ruterstop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@

from ruterstop.utils import delta, human_delta, norwegian_ascii, timed_cache

__version__ = "0.3.1"
__version__ = "0.4.0"

# Default settings
DEFAULTS = dict(
long_eta=59
)

ENTUR_CLIENT_ID = __version__
ENTUR_STOP_PLACE_ENDPOINT = "https://api.entur.io/stop-places/v1/graphql"
ENTUR_STOP_PLACE_QUERY = """
{
stopPlace(size: 10, query: "%(stop_name)s") {
stopPlace(size: 250, query: "%(stop_name)s") {
id
topographicPlace {
name {
Expand Down Expand Up @@ -145,10 +150,12 @@ def get_stop_search_result(*, name_search):
def parse_stops(raw_dict):
for stop in raw_dict["data"]["stopPlace"]:
numid = stop["id"].rsplit(":", maxsplit=1).pop()
yield StopPlace(numid,
stop["name"]["value"],
stop["topographicPlace"]["name"]["value"],
stop["topographicPlace"]["parentTopographicPlace"]["name"]["value"])
yield StopPlace(
numid,
stop["name"]["value"],
stop["topographicPlace"]["name"]["value"],
stop["topographicPlace"]["parentTopographicPlace"]["name"]["value"])


def parse_departures(raw_dict, *, date_fmt="%Y-%m-%dT%H:%M:%S%z"):
"""
Expand Down Expand Up @@ -188,6 +195,8 @@ def serve_departures(stop_id):
kw["min_eta"] = int(q.min_eta)
if q.grouped:
kw["grouped"] = True
if q.long_eta:
kw["long_eta"] = int(q.long_eta)

deps = get_departures(stop_id=stop_id)
bottle.response.set_header("Content-Type", "text/plain")
Expand All @@ -204,7 +213,7 @@ def get_departures(*, stop_id=None):
return parse_departures(raw_stop)


def format_departure_list(departures, *, min_eta=0, long_eta=-1, directions=None, grouped=False):
def format_departure_list(departures, *, min_eta=0, long_eta=DEFAULTS["long_eta"], directions=None, grouped=False):
"""
Filters, formats and groups departures based on arguments passed.
"""
Expand Down Expand Up @@ -235,7 +244,7 @@ def format_departure_list(departures, *, min_eta=0, long_eta=-1, directions=None

# Build string output
newdeps = list()
for eta, deps in by_eta.items():
for _, deps in by_eta.items():
# Print single departures normally
if len(deps) == 1:
newdeps.append(deps[0])
Expand All @@ -249,7 +258,7 @@ def format_departure_list(departures, *, min_eta=0, long_eta=-1, directions=None
# Create pretty output
s = ""
for dep in deps:
if long_eta >= 0 and delta(dep.eta) > long_eta:
if 0 < long_eta < delta(dep.eta):
s += dep.ts_str() + '\n'
else:
s += str(dep) + '\n'
Expand All @@ -269,7 +278,7 @@ def main(argv=sys.argv, *, stdout=sys.stdout):
help="filter direction of departures")
par.add_argument('--min-eta', type=int, default=0, metavar="<minutes>",
help="minimum ETA of departures to return")
par.add_argument('--long-eta', type=int, default=-1, metavar="<minutes>",
par.add_argument('--long-eta', type=int, default=DEFAULTS["long_eta"], metavar="<minutes>",
help="show departure time when ETA is later than this limit" +
"(disable with -1)")
par.add_argument('--grouped', action="store_true",
Expand Down
2 changes: 1 addition & 1 deletion ruterstop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def wrapper(*_args, **_kwargs):
def delta(until=None, *, since=None):
"""
Return amount of whole minutes until `until` date occurs, since `since`.
Returns -1 if since is later than until.
Returns -1 if `since` is later than `until`.
"""
if not since:
since = datetime.now()
Expand Down

0 comments on commit 147f8e5

Please sign in to comment.