Skip to content

Commit

Permalink
Get near stops (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw authored Aug 28, 2023
1 parent d4a32b8 commit a0a8090
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ttss/TTSS.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ttss.extractors import extract_autocomplete_stops, extract_autocomplete_stops_json, extract_stops, \
extract_stop_points, extract_stop, extract_stop_point, extract_stop_passages, extract_stop_point_passages, \
extract_trip_passages, extract_routes, extract_route_stops, extract_route_paths, extract_vehicle_paths, \
extract_vehicles, extract_stops_by_character, extract_lookup_fulltext
extract_vehicles, extract_stops_by_character, extract_lookup_fulltext, extract_near_stops
from ttss.utils import timestamp_ms


Expand Down Expand Up @@ -66,6 +66,16 @@ def get_stops_by_character(self, character: str) -> List[Stop]:
response.raise_for_status()
return extract_stops_by_character(response.json())

def get_near_stops(self, latitude: float, longitude: float) -> List[Stop]:
url = f'{self.base_url}/internetservice/services/lookup/autocomplete/nearStops/json'
params = {
'lat': latitude,
'lon': longitude,
}
response = requests.get(url, params, **self.options)
response.raise_for_status()
return extract_near_stops(response.json())

def get_stops(self, *,
min_latitude: float = -90.0, max_latitude: float = 90.0,
min_longitude: float = -180.0, max_longitude: float = 180.0) -> List[Stop]:
Expand Down
8 changes: 8 additions & 0 deletions src/ttss/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def extract_stops_by_character(data: dict, /) -> List[Stop]:
]


def extract_near_stops(data: dict, /) -> List[Stop]:
return [
Stop(name=unescape(item['name']), number=item['id'])
for item in data
if item['type'] == 'stop'
]


def extract_stops(data: dict, /) -> List[Stop]:
return [
Stop(id=stop['id'],
Expand Down
1 change: 1 addition & 0 deletions tests/resources/lookup_autocomplete_nearStops_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"Przystanki","count":20,"type":"divider"},{"name":"Prokocim Szpital","id":"682","type":"stop"},{"name":"Teligi","id":"681","type":"stop"},{"name":"Prokocim","id":"637","type":"stop"},{"name":"Nowy Prokocim","id":"2582","type":"stop"},{"name":"Wlotowa","id":"634","type":"stop"},{"name":"Ćwiklińskiej","id":"679","type":"stop"},{"name":"Nowy Bieżanów","id":"2580","type":"stop"},{"name":"Nowy Bieżanów P+R","id":"3175","type":"stop"},{"name":"Dauna","id":"632","type":"stop"},{"name":"Bieżanowska","id":"630","type":"stop"},{"name":"Piaski Nowe","id":"716","type":"stop"},{"name":"Kabel","id":"624","type":"stop"},{"name":"Nowosądecka","id":"715","type":"stop"},{"name":"Mały Płaszów","id":"1263","type":"stop"},{"name":"Mały Płaszów P+R","id":"3310","type":"stop"},{"name":"Dworzec Płaszów Estakada","id":"2870","type":"stop"},{"name":"Rzebika","id":"1262","type":"stop"},{"name":"Lipska","id":"561","type":"stop"},{"name":"Dworcowa","id":"623","type":"stop"},{"name":"Witosa","id":"718","type":"stop"}]
14 changes: 14 additions & 0 deletions tests/test_TTSS.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ def test_get_stops_by_character(ttss: TTSS, requests_mock: Mocker) -> None:
]


def test_get_near_stops(ttss: TTSS, requests_mock: Mocker) -> None:
with open(resources_dir / 'lookup_autocomplete_nearStops_json.json', 'r', encoding='utf-8') as f:
data = f.read()
requests_mock.get(f'{base_url}/internetservice/services/lookup/autocomplete/nearStops/json', text=data)

near_stops = ttss.get_near_stops(latitude=50, longitude=20)

assert len(near_stops) == 20

assert near_stops[0] == Stop(name='Prokocim Szpital', number='682')
assert near_stops[1] == Stop(name='Teligi', number='681')
assert near_stops[7] == Stop(name='Nowy Bieżanów P+R', number='3175')


def test_get_stops(ttss: TTSS, requests_mock: Mocker) -> None:
with open(resources_dir / 'geoserviceDispatcher_stopinfo_stops.json', 'r', encoding='utf-8') as f:
data = f.read()
Expand Down

0 comments on commit a0a8090

Please sign in to comment.