PyTransportNSWv2 2.0.1__tar.gz → 2.0.3__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: 2.0.1
3
+ Version: 2.0.3
4
4
  Summary: Get detailed per-trip transport information from TransportNSW
5
5
  Home-page: https://github.com/andystewart999/TransportNSW
6
6
  Author: andystewart999
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyTransportNSWv2
3
- Version: 2.0.1
3
+ Version: 2.0.3
4
4
  Summary: Get detailed per-trip transport information from TransportNSW
5
5
  Home-page: https://github.com/andystewart999/TransportNSW
6
6
  Author: andystewart999
@@ -12,6 +12,15 @@ import re
12
12
  import json #For the output
13
13
  import time
14
14
 
15
+ # Global API variable
16
+ api_calls = 0
17
+
18
+ def increment_api_counter(source):
19
+ global api_calls
20
+ api_calls += 1
21
+ #print(str(api_calls) + " " + source)
22
+
23
+ # Constants
15
24
  ATTR_DUE_IN = 'due'
16
25
 
17
26
  ATTR_ORIGIN_STOP_ID = 'origin_stop_id'
@@ -117,7 +126,10 @@ class TransportNSWv2(object):
117
126
  error_code = 0
118
127
 
119
128
  if not skip_api_calls:
129
+ # Make the call and increment the API counter
120
130
  response = requests.get(url, headers=header, timeout=5)
131
+ increment_api_counter('stop_finder')
132
+
121
133
  else:
122
134
  # An earlier call resulted in an API key error so no point trying again
123
135
  response.status_code = 401
@@ -195,7 +207,7 @@ class TransportNSWv2(object):
195
207
  origin_transport_type = [origin_transport_type]
196
208
 
197
209
  if isinstance(destination_transport_type, int):
198
- destiation_transport_type = [destination_transport_type]
210
+ destination_transport_type = [destination_transport_type]
199
211
 
200
212
  if isinstance(alert_type, str):
201
213
  alert_type = alert_type.split('|')
@@ -242,6 +254,7 @@ class TransportNSWv2(object):
242
254
  # Otherwise store the response for the next steps
243
255
  try:
244
256
  response = requests.get(url, headers=header, timeout=10)
257
+ increment_api_counter('trip')
245
258
 
246
259
  except Exception as ex:
247
260
  raise TripError (f"Error '{str(ex)}' calling trip API for journey {name_origin} to {name_destination}")
@@ -256,7 +269,7 @@ class TransportNSWv2(object):
256
269
  raise APIRateLimitExceeded("Error 'API rate limit exceeded' calling trip API for journey {name_origin} to {name_destination}")
257
270
 
258
271
  else:
259
- raise TripError(f"Error '{str(response.status_cude)}' calling trip API for journey {name_origin} to {name_destination}")
272
+ raise TripError(f"Error '{str(response.status_code)}' calling trip API for journey {name_origin} to {name_destination}")
260
273
 
261
274
  result = response.json()
262
275
 
@@ -426,7 +439,7 @@ class TransportNSWv2(object):
426
439
 
427
440
  current_journey_index = next_journey_index
428
441
 
429
- json_output='{"journeys_to_return": ' + str(journeys_to_return) + ', "journeys_with_data": ' + str(found_journeys) + ', "journeys": [' + json_output + ']}'
442
+ json_output='{"journeys_to_return": ' + str(journeys_to_return) + ', "journeys_with_data": ' + str(found_journeys) + ', "api_calls": ' + str(api_calls) + ', "journeys": [' + json_output + ']}'
430
443
  return json_output
431
444
 
432
445
 
@@ -439,14 +452,16 @@ class TransportNSWv2(object):
439
452
  return None, None, None, None, None
440
453
 
441
454
  for journey_index in range (start_journey_index, journey_count, 1):
442
- origin_leg = self._find_first_leg(journeys[journey_index]['legs'], origin_transport_type, strict, route_filter)
455
+ journey = journeys[journey_index]
456
+
457
+ origin_leg = self._find_first_leg(journey['legs'], origin_transport_type, strict, route_filter)
443
458
 
444
459
  if origin_leg is not None:
445
- destination_leg = self._find_last_leg(journeys[journey_index]['legs'], destination_transport_type, strict)
460
+ destination_leg = self._find_last_leg(journey['legs'], destination_transport_type, strict)
446
461
 
447
462
  if origin_leg is not None and destination_leg is not None:
448
- changes = self._find_changes(journeys[journey_index]['legs'], origin_leg, destination_leg)
449
- return journeys[journey_index]['legs'], journey_index + 1, origin_leg, destination_leg, changes
463
+ changes = self._find_changes(journey['legs'], origin_leg, destination_leg)
464
+ return journey['legs'], journey_index + 1, origin_leg, destination_leg, changes
450
465
  else:
451
466
  return None, None, None, None, None
452
467
 
@@ -510,24 +525,28 @@ class TransportNSWv2(object):
510
525
 
511
526
 
512
527
  def _find_changes(self, legs, origin_leg, destination_leg):
513
- # Find out how often we have to change
528
+ # Find out how often we have to change. Immediately return 0 if the origin and destination legs are the same
529
+ if origin_leg == destination_leg:
530
+ return 0
531
+
532
+ # Count the changes, each time we hit new non-walking leg is considered to be a change
514
533
  changes = 0
515
534
  bInJourney = False
516
535
 
517
536
  for leg in legs:
518
- if leg == destination_leg:
519
- # We've found the last leg (which oould be the same as the first leg), stop counting
520
- return changes
521
-
522
537
  if leg == origin_leg:
523
- # We've found the first leg, start counting
538
+ # We're in the journey so start capturing changes
524
539
  bInJourney = True
525
- else:
526
- if bInJourney:
527
- leg_class = leg['transportation']['product']['class']
528
- if leg_class < 99:
540
+
541
+ elif bInJourney:
542
+ leg_class = leg['transportation']['product']['class']
543
+ if leg_class < 99:
544
+ if leg != destination_leg:
529
545
  changes += 1
530
546
 
547
+ if leg == destination_leg:
548
+ return changes
549
+
531
550
  # We should never get here!
532
551
  return 999
533
552
 
@@ -559,12 +578,14 @@ class TransportNSWv2(object):
559
578
  header = {'Accept': 'application/json', 'Authorization': auth}
560
579
 
561
580
  url_base_path = self._get_base_url(mode)
562
-
563
581
  url_mode_list = self._get_mode_list(mode, agencyid)
582
+
564
583
  if not url_mode_list is None:
565
584
  for mode_url in url_mode_list:
566
585
  url = url_base_path + mode_url
567
586
  response = requests.get(url, headers=header, timeout=10)
587
+ increment_api_counter(url)
588
+
568
589
  # Only try and process the results if we got a good return code
569
590
  if response.status_code == 200:
570
591
  # Search the feed and see if we can match realtimetripid to trip_id
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name="PyTransportNSWv2",
8
- version="2.0.1",
8
+ version="2.0.3",
9
9
  author="andystewart999",
10
10
  author_email="andy.stewart@live.com",
11
11
  description="Get detailed per-trip transport information from TransportNSW",