Skip to content

Commit

Permalink
Allow remaining seats to be toggled
Browse files Browse the repository at this point in the history
  • Loading branch information
leonghui committed Jan 10, 2023
1 parent f75d3a5 commit 4112cce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gwr_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def get_item_listing(query):
fare_price, f"({fare_type_name})"]

# 'availablespaces' appears to be defaulted to 9 so we will ignore that
if remaining_seats and remaining_seats != 9:
if query.seats_left and remaining_seats and remaining_seats != 9:
fare_text.insert(2, f"({remaining_seats} left)")

result_dict[departure_dt] = ' '.join(fare_text)
Expand Down
15 changes: 15 additions & 0 deletions gwr_feed_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class _BaseQuery:
timestamp: datetime = None
weeks_ahead_str: str = '0'
weeks_ahead: int = 0
seats_left_str: str = 'false'
seats_left: bool = False

def init_station_ids(self, feed_config):
self.from_id = get_station_id(self.from_code, feed_config)
Expand All @@ -86,6 +88,11 @@ def validate_departure_time(self):
if not all(time_rules):
self.status.errors.append('Invalid departure time')

def init_seats_left(self):
if self.seats_left_str:
self.seats_left = bool(
self.seats_left_str.lower() in ('true', 'y', 'yes'))

def validate_departure_date(self):
if self.date_str:
date_rules = [self.date_str.isnumeric(), len(self.date_str) == 8]
Expand All @@ -109,6 +116,12 @@ def validate_weeks_ahead(self):
if not self.weeks_ahead_str.isnumeric():
self.status.errors.append('Invalid week count')

def validate_seats_left(self):
if self.seats_left_str:
if not self.seats_left_str.isalpha():
self.status.errors.append(
'seats_left should be either true or false')


@dataclass()
class GwrQuery(_BaseQuery):
Expand All @@ -118,11 +131,13 @@ def __post_init__(self):
self.validate_departure_time()
self.validate_departure_date()
self.validate_weeks_ahead()
self.validate_seats_left()
self.status.refresh()

if self.status.ok:
self.init_station_ids(self.config)
self.init_journey()
self.init_timestamp()
self.init_weeks_ahead()
self.init_seats_left()
self.status.refresh()
3 changes: 2 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def process_listing():
'to_code': rq.args.get('to') or GwrQuery.to_code,
'time_str': rq.args.get('at') or GwrQuery.time_str,
'date_str': rq.args.get('on') or GwrQuery.date_str,
'weeks_ahead_str': rq.args.get('weeks') or GwrQuery.weeks_ahead_str
'weeks_ahead_str': rq.args.get('weeks') or GwrQuery.weeks_ahead_str,
'seats_left_str': rq.args.get('seats_left') or GwrQuery.seats_left_str
}

# access_token expires after 45 mins, get a new token for each query
Expand Down

0 comments on commit 4112cce

Please sign in to comment.