PyTransportNSWv2 0.3.0__tar.gz → 0.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyTransportNSWv2
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Get detailed per-trip transport information from TransportNSW
5
5
  Home-page: https://github.com/andystewart999/TransportNSW
6
6
  Author: andystewart999
@@ -23,7 +23,7 @@ An OpenData account and API key is required to request the data. More informatio
23
23
  https://opendata.transport.nsw.gov.au/user-guide. You need to register an application that needs both the Trip Planner and Realtime Vehicle Positions APIs
24
24
 
25
25
  ### Get the stop IDs
26
- The library needs the stop ID for the source and destination, and optionally how many minutes from now the departure should be. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.
26
+ The function needs the stop IDs for the source and destination, and optionally how many minutes from now the departure should be, and if you want to filter trips by a specific transport type. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.
27
27
 
28
28
  If it's available, the general occupancy level and the latitude and longitude of the selected journey's vehicle (train, bus, etc) will be returned.
29
29
 
@@ -32,42 +32,45 @@ The source API details can be found here: https://opendata.transport.nsw.gov.au/
32
32
 
33
33
  ### Parameters
34
34
  ```python
35
- .get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0])
35
+ .get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0], [transport_type = 0])
36
36
  ```
37
37
 
38
- TransportNSW's trip planner works much better if you use the general location IDs (eg Central Station) rather than a specific platform id (eg Central Station, Platform 19). Forcing a specific platform sometimes results in much more complicated trips.
38
+ TransportNSW's trip planner can work better if you use the general location IDs (eg Central Station) rather than a specific Stop ID (eg Central Station, Platform 19) for the destination, depending on the transport type. Forcing a specific end destination sometimes results in much more complicated trips. Also note that the API expects (and returns) the Stop IDs as strings, although so far they all appear to be numeric.
39
39
 
40
40
  ### Sample Code
41
- The following example will return the next trip from a bus stop in St. Ives to Central Station, without specifying a specific destination platform.
41
+
42
+ The following example will return the next trip that starts from a bus stop in St. Ives (207537) five minutes from now, to Central Station's general stop ID (10101100):
42
43
 
43
44
  **Code:**
44
45
  ```python
45
46
  from TransportNSW import TransportNSW
46
47
  tnsw = TransportNSW()
47
- journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY')
48
+ journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY', 5)
48
49
  print(journey)
49
50
  ```
50
51
  **Result:**
51
52
  ```python
52
- {'due': 23, 'origin_stop_id': '207537', 'origin_name': 'St Ives, Mona Vale Rd at Shinfield Ave', 'departure_time': '2020-06-28T10:10:00Z', 'destination_stop_id': '2000338', 'destination_name': 'Sydney, Central Station, Platform 18', 'arrival_time': '2020-06-28T11:02:00Z', 'origin_transport_type': 'Bus', 'origin_transport_name': 'Sydney Buses Network', 'origin_line_name': '195', 'origin_line_name_short': '195', 'changes': 1, 'occupancy': 'UNKNOWN', 'real_time_trip_id': '612993', 'latitude': 'n/a', 'longitude': 'n/a'}
53
+ {"due": 3, "origin_stop_id": "207537", "origin_name": "Mona Vale Rd at Shinfield Ave, St Ives", "departure_time": "2024-05-20T21:59:48Z", "destination_stop_id": "2000338", "destination_name": "Central Station, Platform 18, Sydney", "arrival_time": "2024-05-20T22:47:36Z", "origin_transport_type": "Bus", "origin_transport_name": "Sydney Buses Network", "origin_line_name": "195", "origin_line_name_short": "195", "changes": 1, "occupancy": "MANY_SEATS", "real_time_trip_id": "2096551", "latitude": -33.72665786743164, "longitude": 151.16305541992188}
53
54
  ```
55
+ Fun fact: TransportNSW's raw API output calls itself JSON, but it uses single quotes for strings in defiance of the JSON standards. When using this wrapper the output is formatted such that `jq`, for example, is happy with it.
54
56
 
55
- * due: the time (in minutes) before the journey starts
57
+ * due: the time (in minutes) before the journey starts
56
58
  * origin_stop_id: the specific departure stop id
57
59
  * origin_name: the name of the departure location
58
- * departure_time: the departure time
60
+ * departure_time: the departure time, in UTC
59
61
  * destination_stop_id: the specific destination stop id
60
62
  * destination_name: the name of the destination location
61
- * arrival_time: the arrival time at the origin
63
+ * arrival_time: the planned arrival time at the origin, in UTC
62
64
  * origin_transport_type: the type of transport, eg train, bus, ferry etc
63
- * origin_transport_name: the full name of transport providere
65
+ * origin_transport_name: the full name of the transport provider
64
66
  * origin_line_name & origin_line_name_short: the full and short names of the journey
65
- * changes: how many transport changes are needed
67
+ * changes: how many transport changes are needed on the journey
66
68
  * occupancy: how full the vehicle is, if available
67
- * real_time_trip_id: the unique TransportNSW id for that specific journey
69
+ * real_time_trip_id: the unique TransportNSW id for that specific journey, if available
68
70
  * latitude & longitude: The location of the vehicle, if available
69
71
 
70
- Please note that the origin and destination detail is just that. We don't return any intermediate steps, transport change types etc other than the total number of changes. The assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application.
72
+ Please note that the origin and destination detail is just that - information about the first and last stops on the journey at the time the request was made. We don't return any intermediate steps, transport change types etc other than the total number of changes - the assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application.
73
+ Also note that the 'transport_type' filter, if present, only makes sure that at least one leg of the journey includes that transport type.
71
74
 
72
75
  ## Thank you
73
76
  Thank you Dav0815 for your TransportNSW library that the vast majority of this fork is based on. I couldn't have done it without you!
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyTransportNSWv2
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Get detailed per-trip transport information from TransportNSW
5
5
  Home-page: https://github.com/andystewart999/TransportNSW
6
6
  Author: andystewart999
@@ -23,7 +23,7 @@ An OpenData account and API key is required to request the data. More informatio
23
23
  https://opendata.transport.nsw.gov.au/user-guide. You need to register an application that needs both the Trip Planner and Realtime Vehicle Positions APIs
24
24
 
25
25
  ### Get the stop IDs
26
- The library needs the stop ID for the source and destination, and optionally how many minutes from now the departure should be. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.
26
+ The function needs the stop IDs for the source and destination, and optionally how many minutes from now the departure should be, and if you want to filter trips by a specific transport type. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.
27
27
 
28
28
  If it's available, the general occupancy level and the latitude and longitude of the selected journey's vehicle (train, bus, etc) will be returned.
29
29
 
@@ -32,42 +32,45 @@ The source API details can be found here: https://opendata.transport.nsw.gov.au/
32
32
 
33
33
  ### Parameters
34
34
  ```python
35
- .get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0])
35
+ .get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0], [transport_type = 0])
36
36
  ```
37
37
 
38
- TransportNSW's trip planner works much better if you use the general location IDs (eg Central Station) rather than a specific platform id (eg Central Station, Platform 19). Forcing a specific platform sometimes results in much more complicated trips.
38
+ TransportNSW's trip planner can work better if you use the general location IDs (eg Central Station) rather than a specific Stop ID (eg Central Station, Platform 19) for the destination, depending on the transport type. Forcing a specific end destination sometimes results in much more complicated trips. Also note that the API expects (and returns) the Stop IDs as strings, although so far they all appear to be numeric.
39
39
 
40
40
  ### Sample Code
41
- The following example will return the next trip from a bus stop in St. Ives to Central Station, without specifying a specific destination platform.
41
+
42
+ The following example will return the next trip that starts from a bus stop in St. Ives (207537) five minutes from now, to Central Station's general stop ID (10101100):
42
43
 
43
44
  **Code:**
44
45
  ```python
45
46
  from TransportNSW import TransportNSW
46
47
  tnsw = TransportNSW()
47
- journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY')
48
+ journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY', 5)
48
49
  print(journey)
49
50
  ```
50
51
  **Result:**
51
52
  ```python
52
- {'due': 23, 'origin_stop_id': '207537', 'origin_name': 'St Ives, Mona Vale Rd at Shinfield Ave', 'departure_time': '2020-06-28T10:10:00Z', 'destination_stop_id': '2000338', 'destination_name': 'Sydney, Central Station, Platform 18', 'arrival_time': '2020-06-28T11:02:00Z', 'origin_transport_type': 'Bus', 'origin_transport_name': 'Sydney Buses Network', 'origin_line_name': '195', 'origin_line_name_short': '195', 'changes': 1, 'occupancy': 'UNKNOWN', 'real_time_trip_id': '612993', 'latitude': 'n/a', 'longitude': 'n/a'}
53
+ {"due": 3, "origin_stop_id": "207537", "origin_name": "Mona Vale Rd at Shinfield Ave, St Ives", "departure_time": "2024-05-20T21:59:48Z", "destination_stop_id": "2000338", "destination_name": "Central Station, Platform 18, Sydney", "arrival_time": "2024-05-20T22:47:36Z", "origin_transport_type": "Bus", "origin_transport_name": "Sydney Buses Network", "origin_line_name": "195", "origin_line_name_short": "195", "changes": 1, "occupancy": "MANY_SEATS", "real_time_trip_id": "2096551", "latitude": -33.72665786743164, "longitude": 151.16305541992188}
53
54
  ```
55
+ Fun fact: TransportNSW's raw API output calls itself JSON, but it uses single quotes for strings in defiance of the JSON standards. When using this wrapper the output is formatted such that `jq`, for example, is happy with it.
54
56
 
55
- * due: the time (in minutes) before the journey starts
57
+ * due: the time (in minutes) before the journey starts
56
58
  * origin_stop_id: the specific departure stop id
57
59
  * origin_name: the name of the departure location
58
- * departure_time: the departure time
60
+ * departure_time: the departure time, in UTC
59
61
  * destination_stop_id: the specific destination stop id
60
62
  * destination_name: the name of the destination location
61
- * arrival_time: the arrival time at the origin
63
+ * arrival_time: the planned arrival time at the origin, in UTC
62
64
  * origin_transport_type: the type of transport, eg train, bus, ferry etc
63
- * origin_transport_name: the full name of transport providere
65
+ * origin_transport_name: the full name of the transport provider
64
66
  * origin_line_name & origin_line_name_short: the full and short names of the journey
65
- * changes: how many transport changes are needed
67
+ * changes: how many transport changes are needed on the journey
66
68
  * occupancy: how full the vehicle is, if available
67
- * real_time_trip_id: the unique TransportNSW id for that specific journey
69
+ * real_time_trip_id: the unique TransportNSW id for that specific journey, if available
68
70
  * latitude & longitude: The location of the vehicle, if available
69
71
 
70
- Please note that the origin and destination detail is just that. We don't return any intermediate steps, transport change types etc other than the total number of changes. The assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application.
72
+ Please note that the origin and destination detail is just that - information about the first and last stops on the journey at the time the request was made. We don't return any intermediate steps, transport change types etc other than the total number of changes - the assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application.
73
+ Also note that the 'transport_type' filter, if present, only makes sure that at least one leg of the journey includes that transport type.
71
74
 
72
75
  ## Thank you
73
76
  Thank you Dav0815 for your TransportNSW library that the vast majority of this fork is based on. I couldn't have done it without you!
@@ -0,0 +1,62 @@
1
+ # TransportNSW
2
+ Python lib to access Transport NSW information.
3
+
4
+ ## How to Use
5
+
6
+ ### Get an API Key
7
+ An OpenData account and API key is required to request the data. More information on how to create the free account can be found here:
8
+ https://opendata.transport.nsw.gov.au/user-guide. You need to register an application that needs both the Trip Planner and Realtime Vehicle Positions APIs
9
+
10
+ ### Get the stop IDs
11
+ The function needs the stop IDs for the source and destination, and optionally how many minutes from now the departure should be, and if you want to filter trips by a specific transport type. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.
12
+
13
+ If it's available, the general occupancy level and the latitude and longitude of the selected journey's vehicle (train, bus, etc) will be returned.
14
+
15
+ ### API Documentation
16
+ The source API details can be found here: https://opendata.transport.nsw.gov.au/sites/default/files/2023-08/Trip%20Planner%20API%20manual-opendataproduction%20v3.2.pdf
17
+
18
+ ### Parameters
19
+ ```python
20
+ .get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0], [transport_type = 0])
21
+ ```
22
+
23
+ TransportNSW's trip planner can work better if you use the general location IDs (eg Central Station) rather than a specific Stop ID (eg Central Station, Platform 19) for the destination, depending on the transport type. Forcing a specific end destination sometimes results in much more complicated trips. Also note that the API expects (and returns) the Stop IDs as strings, although so far they all appear to be numeric.
24
+
25
+ ### Sample Code
26
+
27
+ The following example will return the next trip that starts from a bus stop in St. Ives (207537) five minutes from now, to Central Station's general stop ID (10101100):
28
+
29
+ **Code:**
30
+ ```python
31
+ from TransportNSW import TransportNSW
32
+ tnsw = TransportNSW()
33
+ journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY', 5)
34
+ print(journey)
35
+ ```
36
+ **Result:**
37
+ ```python
38
+ {"due": 3, "origin_stop_id": "207537", "origin_name": "Mona Vale Rd at Shinfield Ave, St Ives", "departure_time": "2024-05-20T21:59:48Z", "destination_stop_id": "2000338", "destination_name": "Central Station, Platform 18, Sydney", "arrival_time": "2024-05-20T22:47:36Z", "origin_transport_type": "Bus", "origin_transport_name": "Sydney Buses Network", "origin_line_name": "195", "origin_line_name_short": "195", "changes": 1, "occupancy": "MANY_SEATS", "real_time_trip_id": "2096551", "latitude": -33.72665786743164, "longitude": 151.16305541992188}
39
+ ```
40
+ Fun fact: TransportNSW's raw API output calls itself JSON, but it uses single quotes for strings in defiance of the JSON standards. When using this wrapper the output is formatted such that `jq`, for example, is happy with it.
41
+
42
+ * due: the time (in minutes) before the journey starts
43
+ * origin_stop_id: the specific departure stop id
44
+ * origin_name: the name of the departure location
45
+ * departure_time: the departure time, in UTC
46
+ * destination_stop_id: the specific destination stop id
47
+ * destination_name: the name of the destination location
48
+ * arrival_time: the planned arrival time at the origin, in UTC
49
+ * origin_transport_type: the type of transport, eg train, bus, ferry etc
50
+ * origin_transport_name: the full name of the transport provider
51
+ * origin_line_name & origin_line_name_short: the full and short names of the journey
52
+ * changes: how many transport changes are needed on the journey
53
+ * occupancy: how full the vehicle is, if available
54
+ * real_time_trip_id: the unique TransportNSW id for that specific journey, if available
55
+ * latitude & longitude: The location of the vehicle, if available
56
+
57
+ Please note that the origin and destination detail is just that - information about the first and last stops on the journey at the time the request was made. We don't return any intermediate steps, transport change types etc other than the total number of changes - the assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application.
58
+ Also note that the 'transport_type' filter, if present, only makes sure that at least one leg of the journey includes that transport type.
59
+
60
+ ## Thank you
61
+ Thank you Dav0815 for your TransportNSW library that the vast majority of this fork is based on. I couldn't have done it without you!
62
+ https://github.com/Dav0815/TransportNSW
@@ -8,6 +8,7 @@ import requests.exceptions
8
8
  import requests
9
9
  import logging
10
10
  import re
11
+ import json #For the output
11
12
 
12
13
  ATTR_DUE_IN = 'due'
13
14
 
@@ -131,6 +132,14 @@ class TransportNSW(object):
131
132
  # Look for a trip with a matching class filter in at least one of its legs. Could possibly be more stringent here, if ANY part of the journey fits the filter, it will be returned.
132
133
  journey = self.find_first_journey(result['journeys'], transport_type)
133
134
 
135
+ if journey is None:
136
+ logger.warning("No journey information returned")
137
+ return self.info
138
+
139
+ if journey['legs'] is None:
140
+ logger.warning("No journey information returned")
141
+ return self.info
142
+
134
143
  legs = journey['legs']
135
144
  first_leg = self.find_first_leg(legs, transport_type)
136
145
  last_leg = self.find_last_leg(legs, transport_type)
@@ -168,8 +177,13 @@ class TransportNSW(object):
168
177
  realtimetripid = transportation['properties']['RealtimeTripId']
169
178
 
170
179
  # Line info
171
- origin_line_name_short = transportation['disassembledName']
172
- origin_line_name = transportation['number']
180
+ origin_line_name_short = "unknown"
181
+ if 'disassembledName' in transportation:
182
+ origin_line_name_short = transportation['disassembledName']
183
+
184
+ origin_line_name = "unknown"
185
+ if 'number' in transportation:
186
+ origin_line_name = transportation['number']
173
187
 
174
188
  # Occupancy info, if it's there
175
189
  occupancy = 'UNKNOWN'
@@ -242,7 +256,7 @@ class TransportNSW(object):
242
256
  ATTR_LATITUDE : latitude,
243
257
  ATTR_LONGITUDE : longitude
244
258
  }
245
- return self.info
259
+ return json.dumps(self.info)
246
260
 
247
261
  def find_first_journey(self, journeys, journeytype):
248
262
  # Find the first journey whose first leg is of the requested type
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name="PyTransportNSWv2",
8
- version="0.3.0",
8
+ version="0.3.2",
9
9
  author="andystewart999",
10
10
  description="Get detailed per-trip transport information from TransportNSW",
11
11
  long_description=long_description,
@@ -1,59 +0,0 @@
1
- # TransportNSW
2
- Python lib to access Transport NSW information.
3
-
4
- ## How to Use
5
-
6
- ### Get an API Key
7
- An OpenData account and API key is required to request the data. More information on how to create the free account can be found here:
8
- https://opendata.transport.nsw.gov.au/user-guide. You need to register an application that needs both the Trip Planner and Realtime Vehicle Positions APIs
9
-
10
- ### Get the stop IDs
11
- The library needs the stop ID for the source and destination, and optionally how many minutes from now the departure should be. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.
12
-
13
- If it's available, the general occupancy level and the latitude and longitude of the selected journey's vehicle (train, bus, etc) will be returned.
14
-
15
- ### API Documentation
16
- The source API details can be found here: https://opendata.transport.nsw.gov.au/sites/default/files/2023-08/Trip%20Planner%20API%20manual-opendataproduction%20v3.2.pdf
17
-
18
- ### Parameters
19
- ```python
20
- .get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0])
21
- ```
22
-
23
- TransportNSW's trip planner works much better if you use the general location IDs (eg Central Station) rather than a specific platform id (eg Central Station, Platform 19). Forcing a specific platform sometimes results in much more complicated trips.
24
-
25
- ### Sample Code
26
- The following example will return the next trip from a bus stop in St. Ives to Central Station, without specifying a specific destination platform.
27
-
28
- **Code:**
29
- ```python
30
- from TransportNSW import TransportNSW
31
- tnsw = TransportNSW()
32
- journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY')
33
- print(journey)
34
- ```
35
- **Result:**
36
- ```python
37
- {'due': 23, 'origin_stop_id': '207537', 'origin_name': 'St Ives, Mona Vale Rd at Shinfield Ave', 'departure_time': '2020-06-28T10:10:00Z', 'destination_stop_id': '2000338', 'destination_name': 'Sydney, Central Station, Platform 18', 'arrival_time': '2020-06-28T11:02:00Z', 'origin_transport_type': 'Bus', 'origin_transport_name': 'Sydney Buses Network', 'origin_line_name': '195', 'origin_line_name_short': '195', 'changes': 1, 'occupancy': 'UNKNOWN', 'real_time_trip_id': '612993', 'latitude': 'n/a', 'longitude': 'n/a'}
38
- ```
39
-
40
- * due: the time (in minutes) before the journey starts
41
- * origin_stop_id: the specific departure stop id
42
- * origin_name: the name of the departure location
43
- * departure_time: the departure time
44
- * destination_stop_id: the specific destination stop id
45
- * destination_name: the name of the destination location
46
- * arrival_time: the arrival time at the origin
47
- * origin_transport_type: the type of transport, eg train, bus, ferry etc
48
- * origin_transport_name: the full name of transport providere
49
- * origin_line_name & origin_line_name_short: the full and short names of the journey
50
- * changes: how many transport changes are needed
51
- * occupancy: how full the vehicle is, if available
52
- * real_time_trip_id: the unique TransportNSW id for that specific journey
53
- * latitude & longitude: The location of the vehicle, if available
54
-
55
- Please note that the origin and destination detail is just that. We don't return any intermediate steps, transport change types etc other than the total number of changes. The assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application.
56
-
57
- ## Thank you
58
- Thank you Dav0815 for your TransportNSW library that the vast majority of this fork is based on. I couldn't have done it without you!
59
- https://github.com/Dav0815/TransportNSW