From b0b95a8fc72387ea34b71ec2006e1a146555c13a Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Thu, 20 Sep 2018 19:06:01 -0500 Subject: [PATCH] First release --- .gitignore | 1 + ctabus.py | 10 ++++--- main.py | 88 +++++++++++++++++++++++++++++++++++++++++++++--------- search.py | 16 ++++++++-- 4 files changed, 94 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 1a4bdfd..7e6f6e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /cta_api_key /output +/test.py __pycache__ *.pyc *_out.json diff --git a/ctabus.py b/ctabus.py index daca7a6..a14e39b 100644 --- a/ctabus.py +++ b/ctabus.py @@ -9,11 +9,13 @@ def get_data(type,api_key = api,**args): args['key'] = api_key args['format'] = 'json' url = base_url.format(type = type,query = urlencode(args)) - print(url) - input() response = get(url) - data = json.loads(response.text) - return data['bustime-response'] + data = json.loads(response.text)['bustime-response'] + try: + data['error'] + raise Exception(str(data["error"])) + except KeyError: + return data def get_times(stop_id,api_key = api): return get_data('getpredictions',api_key,stpid=stop_id) diff --git a/main.py b/main.py index 9365c9e..7601c2a 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,10 @@ -# from argparse import parser +import argparse from print2d import print2d import re -# parser = argparse.ArgumentParser(prog = 'ctabus') -# parser.add_argument('arg',metavar = 'stop-id | cross streets') -# parser.add_argument('-r','--route',default = None) -# parser.add_argument('-d','--direction',default = None) -# args = parser.parse_args() +import ctabus +from dateutil.parser import parse as date_parse +import datetime +from search import Search,StopSearch def numb_sort(str): n = 40 try: @@ -14,6 +13,33 @@ def numb_sort(str): print(str) raise E +def pprint_delta(delta): + delta = str(delta) + days= None + s1 = delta.split(', ') + if len(s1) > 1: + days,time = s1 + else: + time = s1[0] + time = time.split('.')[0] + hour,minute,second = map(int,time.split(':')) + time = '' + if hour: + time += f'{hour} hour' + ('s' if hour != 1 else '') + if minute: + if time and not time.endswith(', '): + time += ', ' + time += f'{minute} minute' + ('s' if minute != 1 else '') + if second: + if time and not time.endswith(', '): + time += ', ' + time += f'{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} @@ -23,7 +49,7 @@ def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): [ [obj[d] for d in displays] for obj in objs ], - key = lambda row: key(row[sort]) + key = lambda row: key(row[sort]) if key else row[sort] ) if num_pic: display = [[i] + data for i,data in enumerate(display)] @@ -54,11 +80,45 @@ def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): pass return ret - +config = '''{delta} ({t}) +{route} - {end} ({direction})''' if __name__ == "__main__": - import json - with open('stops_out.json') as file: - d= json.load(file) - - d = d['stops'] - print(gen_list(d,'stpid','stpnm',key=numb_sort,num_pic=True)) + parser = argparse.ArgumentParser(prog = 'ctabus') + parser.add_argument('arg',nargs = '+',metavar = '(stop-id | cross streets)') + parser.add_argument('-r','--route',default = None) + parser.add_argument('-d','--direction',default = None) + args = parser.parse_args() + args.arg = ' '.join(args.arg) + print(args) + if not args.arg.isdecimal(): + #routes + if not args.route: + data = ctabus.get_routes()['routes'] + route = gen_list(data,'rt','rt','rtnm',num_pic = False,key=numb_sort) + else: + route = args.route + data = ctabus.get_directions(route)['directions'] + #direction + if not args.direction: + direction = gen_list(data,'dir','dir') + else: + s = Search(args.direction) + direction = sorted((obj['dir'] for obj in data),key = s)[0] + #direction + stops = ctabus.get_stops(route,direction)['stops'] + s = StopSearch(args.arg) + stop_id = gen_list(stops,'stpid','stpnm',key = s) + else: + stop_id = args.arg + times = ctabus.get_times(stop_id)['prd'] + today = datetime.datetime.today() + for time in times: + arrival = date_parse(time['prdtm']) + delta = pprint_delta(arrival-today) + t = arrival.strftime('%H:%M:%S') + route = time['rt'] + direction = time['rtdir'] + end = time['des'] + print( + config.format(**globals()),end= '\n'*2 + ) diff --git a/search.py b/search.py index f0dd163..03ea6c9 100644 --- a/search.py +++ b/search.py @@ -1,8 +1,19 @@ from editdistance import eval as editdistance import re import json -class StopSearch: +class Search: def __init__(self,query): + self.raw_lower = query.lower() + def __call__(self,arg): + arg = arg.lower() + return editdistance(self.raw_lower,arg) + def __str__(self): + print(self.raw_lower) + def __repr__(self): + return str(self) +class StopSearch(Search): + def __init__(self,query): + super().__init__(query) query = query.lower() parts = re.split(r' ?(?:(?