Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Adding get_stops
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryLudemann committed Aug 19, 2021
1 parent a26e0f7 commit 2969521
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 45 deletions.
53 changes: 34 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Metlink-Python
Wrapper for the offical metlink API (Requires free api key from metlink)
Unofficial wrapper for the official metlink API (Requires free api key from metlink)

### Install:
```
pip install metlink-python
```
### Get API KEY
1. Register here at [Metlink](https://gwrc-opendata.auth.ap-southeast-2.amazoncognito.com/signup?response_type=token&client_id=4bmn2icphpqls57ijr7k4okv55&redirect_uri=https://opendata.metlink.org.nz/index.html?action=login)
1. Register at [Metlink](https://gwrc-opendata.auth.ap-southeast-2.amazoncognito.com/signup?response_type=token&client_id=4bmn2icphpqls57ijr7k4okv55&redirect_uri=https://opendata.metlink.org.nz/index.html?action=login)
2. Login
3. Get API key from [My Dashboard](https://opendata.metlink.org.nz/dashboard)
4. Optionally check out the [API's](https://opendata.metlink.org.nz/apis)

### Examples
#### Basic Use
#### Setup Use
```python
from metlink import metlink

Expand Down Expand Up @@ -65,17 +64,9 @@ for pred in stop_predictions:
```

### Functions:
| Function Name | Description |
| ------------- |:-------------:|
| get_trip_updates | Trip Updates - Delays, cancellations, changed routes. Given nothing, returns list of dictionaries. returns empty list if no trip delays or changes |
| get_vehicle_positions | Vehicle Positions - API to get Information about vehicles including location. Given nothing, returns list of dictionaries. if no busses are active returns empty list |
| get_service_alerts | Trip Updates - Information about unforeseen events affecting routes, stops, or the network. Given nothing, returns list of dictionaries. |
| get_stop_predictions | Passed stop_id, returns list of dictionary's |
| get_routes | Returns list of dictionarys of route infomation, optionally given stop_id as filter |


#### Returned Dictionary Fields:
* **get_stop_predictions**
* **get_stop_predictions(stop_id=None)**
Passed stop_id, returns list of dictionary's
**Param**: stop_id
* service_id
* name
* vehicle_id
Expand All @@ -90,7 +81,9 @@ for pred in stop_predictions:
* departure
* arrival

* **get_service_alerts**
* **get_service_alerts()**
Trip Updates - Information about unforeseen events affecting routes, stops, or the network. Given nothing, returns list of dictionaries.
**Param**: N/A
* active_period
* effect
* cause
Expand All @@ -99,20 +92,26 @@ for pred in stop_predictions:
* severity_level
* informed_entity

* **get_vehicle_positions**
* **get_vehicle_positions()**
Vehicle Positions - API to get Information about vehicles including location. Given nothing, returns list of dictionaries. if no busses are active returns empty list
**Param**: N/A
* vehicle_id
* bearing
* latitude
* longitude

* **get_trip_updates**
* **get_trip_updates()**
Trip Updates - Delays, cancellations, changed routes. Given nothing, returns list of dictionaries. returns empty list if no trip delays or changes
**Param**: N/A
* stop_id
* arrival_delay
* arrival_time
* trip_start_time
* vehicle_id

* **get_routes**
* **get_routes(stop_id=None)**
Returns list of dictionarys of route infomation, optionally given stop_id as filter
**Param**: Optional stop_id
* id
* route_id
* agency_id
Expand All @@ -123,3 +122,19 @@ for pred in stop_predictions:
* route_color
* route_text_color
* route_url

* **get_stops(trip_id=None, route_id=None)**
Returns list of dictionarys of stops infomation, optionally given trip_id and or route_id
**Param**: Optional stop_id
* id
* stop_id
* stop_code
* stop_name
* stop_desc
* zone_id
* stop_lat
* stop_lon
* location_type
* parent_station
* stop_url
* stop_timezone
82 changes: 57 additions & 25 deletions metlink/metlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import certifi

class metlink():
__version__ = '0.0.3'
__version__ = '0.0.4'
base_url = 'https://api.opendata.metlink.org.nz/v1/'

def __init__(self, API_KEY = None):
Expand Down Expand Up @@ -34,12 +34,43 @@ def get_metlink_data(self, API_Path):

# GTFS APIS Static public transport data for Wellington in GTFS (General Transit Feed Specification).


def get_stops(self, trip_id = None, route_id = None):
""" Returns list of dictionarys of stops infomation, optionally given route_id and or trip_id as filter """
if trip_id and route_id:
response = self.get_metlink_data(f'https://api.opendata.metlink.org.nz/v1/gtfs/stops?route_id={route_id}&trip_id={trip_id}')
elif trip_id:
response = self.get_metlink_data(f'https://api.opendata.metlink.org.nz/v1/gtfs/stops?trip_id={trip_id}')
elif route_id:
response = self.get_metlink_data(f'https://api.opendata.metlink.org.nz/v1/gtfs/stops?route_id={route_id}')
else:
response = self.get_metlink_data('https://api.opendata.metlink.org.nz/v1/gtfs/stops')
routes = []
for entity in response:
route = {
'id': entity['id'],
'stop_id': entity['stop_id'],
'stop_code': entity['stop_code'],
'stop_name': entity['stop_name'],
'stop_desc': entity['stop_desc'],
'zone_id': entity['zone_id'],
'stop_lat': entity['stop_lat'],
'stop_lon': entity['stop_lon'],
'location_type': entity['location_type'],
'parent_station': entity['parent_station'],
'stop_url': entity['stop_url'],
'stop_timezone': entity['stop_timezone'],
}
routes.append(route)
return routes


def get_routes(self, stop_id = None):
""" Returns list of dictionarys of route infomation, optionally given stop_id as filter """
if stop_id:
response = self.get_metlink_data('https://api.opendata.metlink.org.nz/v1/gtfs/routes?stop_id=' + str(stop_id))
else:
response = self.get_metlink_data('https://api.opendata.metlink.org.nz/v1/gtfs/routes?stop_id=')
response = self.get_metlink_data('https://api.opendata.metlink.org.nz/v1/gtfs/routes')
routes = []
for entity in response:
route = {
Expand Down Expand Up @@ -123,27 +154,28 @@ def get_service_alerts(self):

# stop predicion API

def get_stop_predictions(self, stop_id):
def get_stop_predictions(self, stop_id = None):
""" Passed stop_id, returns list of dictionary's """
if not stop_id: raise ValueError('stop_id must be given')
response = self.get_metlink_data('https://api.opendata.metlink.org.nz/v1/stop-predictions?stop_id=' + str(stop_id))
stop_predictions = []
for stop in response['departures']:
prediction = {
'service_id': stop['service_id'],
'name': stop['name'],
'vehicle_id': stop['vehicle_id'],
'direction': stop['direction'],
'status': stop['status'],
'trip_id': stop['trip_id'],
'delay': stop['delay'],
'monitored': stop['monitored'],
'operator': stop['operator'],
'origin': stop['origin'],
'wheelchair_accessible': stop['wheelchair_accessible'],
'departure': stop['departure'],
'arrival': stop['arrival']
}
stop_predictions.append(prediction)

return stop_predictions
if stop_id:
response = self.get_metlink_data('https://api.opendata.metlink.org.nz/v1/stop-predictions?stop_id=' + str(stop_id))
stop_predictions = []
for stop in response['departures']:
prediction = {
'service_id': stop['service_id'],
'name': stop['name'],
'vehicle_id': stop['vehicle_id'],
'direction': stop['direction'],
'status': stop['status'],
'trip_id': stop['trip_id'],
'delay': stop['delay'],
'monitored': stop['monitored'],
'operator': stop['operator'],
'origin': stop['origin'],
'wheelchair_accessible': stop['wheelchair_accessible'],
'departure': stop['departure'],
'arrival': stop['arrival']
}
stop_predictions.append(prediction)
return stop_predictions

raise ValueError('stop_id must be given for get_stop_predictions')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
long_description = "\n" + fh.read()

VERSION = '0.0.3'
VERSION = '0.0.4'
DESCRIPTION = 'API Wrapper for Metlink API'
LONG_DESCRIPTION = 'A python package to easily access infomation from the official metlink api'

Expand Down

0 comments on commit 2969521

Please sign in to comment.