|
|
|
@ -10,11 +10,14 @@ import time |
|
|
|
# for logging |
|
|
|
import os.path as osp |
|
|
|
import sys |
|
|
|
import re |
|
|
|
CHICAGO_TZ = tz.gettz("America/Chicago") |
|
|
|
# https://stackoverflow.com/a/5967539 |
|
|
|
|
|
|
|
|
|
|
|
def atoi(text): |
|
|
|
return int(text) if text.isdigit() else text |
|
|
|
|
|
|
|
|
|
|
|
def numb_sort(text): |
|
|
|
''' |
|
|
|
alist.sort(key=natural_keys) sorts in human order |
|
|
|
@ -23,9 +26,11 @@ def numb_sort(text): |
|
|
|
''' |
|
|
|
return [atoi(c) for c in re.split(r'(\d+)', text)] |
|
|
|
|
|
|
|
|
|
|
|
def clearscr(): |
|
|
|
os.system('cls' if os.name == 'nt' else 'clear') |
|
|
|
|
|
|
|
|
|
|
|
def pprint_delta(delta): |
|
|
|
delta = str(delta) |
|
|
|
days = None |
|
|
|
@ -42,17 +47,20 @@ def pprint_delta(delta): |
|
|
|
if minute: |
|
|
|
if time and not time.endswith(', '): |
|
|
|
time += ', ' |
|
|
|
time += '{minute} minute'.format(minute=minute) + ('s' if minute != 1 else '') |
|
|
|
time += '{minute} minute'.format(minute=minute) + \ |
|
|
|
('s' if minute != 1 else '') |
|
|
|
if second: |
|
|
|
if time and not time.endswith(', '): |
|
|
|
time += ', ' |
|
|
|
time += '{second} second'.format(second=second) + ('s' if second != 1 else '') |
|
|
|
time += '{second} second'.format(second=second) + \ |
|
|
|
('s' if second != 1 else '') |
|
|
|
ret = '' |
|
|
|
if days: |
|
|
|
ret = days + ', ' if time else '' |
|
|
|
ret += time |
|
|
|
return ret |
|
|
|
|
|
|
|
|
|
|
|
def gen_list(objs, data, *displays, key=None, sort=0, num_pic=True): |
|
|
|
k = displays[sort] |
|
|
|
display_data = {obj[k]: obj[data] for obj in objs} |
|
|
|
@ -97,11 +105,14 @@ def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): |
|
|
|
pass |
|
|
|
return ret |
|
|
|
|
|
|
|
|
|
|
|
config = '''\ |
|
|
|
{route} - {end} ({direction}) |
|
|
|
{nm}, stop {stop_id} |
|
|
|
{delta} ({t})\ |
|
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
def show(data, rt_filter=None, _clear=False): |
|
|
|
times = data['prd'] |
|
|
|
today = datetime.datetime.now(CHICAGO_TZ) |
|
|
|
@ -110,25 +121,29 @@ def show(data,rt_filter=None,_clear=False): |
|
|
|
arrivals = filter(lambda arrival: arrival['rt'] == rt_filter, arrivals) |
|
|
|
if _clear: |
|
|
|
clearscr() |
|
|
|
for time in arrivals: |
|
|
|
before = date_parse(time['prdtm']) |
|
|
|
for bustime in arrivals: |
|
|
|
before = date_parse(bustime['prdtm']) |
|
|
|
arrival = before.replace(tzinfo=CHICAGO_TZ) |
|
|
|
if arrival > today: |
|
|
|
stop_id = time['stpid'] |
|
|
|
stop_id = bustime['stpid'] |
|
|
|
delta = pprint_delta(arrival-today) |
|
|
|
t = arrival.strftime('%H:%M:%S') |
|
|
|
route = time['rt'] |
|
|
|
direction = time['rtdir'] |
|
|
|
end = time['des'] |
|
|
|
nm = time['stpnm'].rstrip() |
|
|
|
route = bustime['rt'] |
|
|
|
direction = bustime['rtdir'] |
|
|
|
end = bustime['des'] |
|
|
|
nm = bustime['stpnm'].rstrip() |
|
|
|
print( |
|
|
|
config.format(**locals()), end='\n'*2 |
|
|
|
) |
|
|
|
print("="*36) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
parser = argparse.ArgumentParser(prog='ctabus') |
|
|
|
parser.add_argument('-l','--lucky',action='store_true',help = 'picks first result') |
|
|
|
parser.add_argument('-p','--periodic',metavar = 'SEC',type=int,help='checks periodically') |
|
|
|
parser.add_argument('-l', '--lucky', action='store_true', |
|
|
|
help='picks first result') |
|
|
|
parser.add_argument('-p', '--periodic', metavar='SEC', |
|
|
|
type=int, help='checks periodically') |
|
|
|
parser.add_argument('-r', '--route', default=None) |
|
|
|
parser.add_argument('-d', '--direction', default=None) |
|
|
|
parser.add_argument('arg', nargs='+', metavar='(stop-id | cross streets)') |
|
|
|
@ -143,7 +158,8 @@ if __name__ == '__main__': |
|
|
|
# routes |
|
|
|
if not args.route: |
|
|
|
data = ctabus.get_routes()['routes'] |
|
|
|
route = gen_list(data,'rt','rt','rtnm',num_pic = False,key=numb_sort) |
|
|
|
route = gen_list(data, 'rt', 'rt', 'rtnm', |
|
|
|
num_pic=False, key=numb_sort) |
|
|
|
else: |
|
|
|
route = args.route |
|
|
|
data = ctabus.get_directions(route)['directions'] |
|
|
|
@ -157,7 +173,8 @@ if __name__ == '__main__': |
|
|
|
stops = ctabus.get_stops(route, direction)['stops'] |
|
|
|
s = StopSearch(args.arg) |
|
|
|
if args.lucky: |
|
|
|
stop_id = sorted(stops,key=lambda stop: s(stop['stpnm']))[0]['stpid'] |
|
|
|
stop_id = sorted(stops, key=lambda stop: s(stop['stpnm']))[ |
|
|
|
0]['stpid'] |
|
|
|
else: |
|
|
|
stop_id = gen_list(stops, 'stpid', 'stpnm', key=s) |
|
|
|
else: |
|
|
|
@ -173,7 +190,7 @@ if __name__ == '__main__': |
|
|
|
e = time.perf_counter() - s |
|
|
|
if e < args.periodic: |
|
|
|
time.sleep(args.periodic-e) |
|
|
|
except KeyboardInterrupt as e: |
|
|
|
except KeyboardInterrupt: |
|
|
|
_done = True |
|
|
|
else: |
|
|
|
show(data, args.route) |