diff --git a/.gitignore b/.gitignore index 4754fdb..4870e80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /cta_api_key.json +/output __pycache__ *.pyc *_out.json diff --git a/ctabus.py b/ctabus.py index bd7812e..2b9b6c5 100644 --- a/ctabus.py +++ b/ctabus.py @@ -3,9 +3,11 @@ from requests import get import json import datetime import argparse +from editdistance import eval as editdistance test = False test = True api = "viCgbucwRPUTGAf4mZmsCNiDm" + def get_data(type,api_key = api,**args): base_url = "http://www.ctabustracker.com/bustime/api/v2/{type}?{query}" args['key'] = api_key @@ -17,16 +19,21 @@ def get_data(type,api_key = api,**args): data = json.loads(response.text) return data['bustime-response'] # print(url) + def print2d(values): pass def get_times(stop_id,api_key = api): return get_data('getpredictions',api_key,stpid=stop_id) + def get_routes(api_key = api): return get_data('getroutes',api_key) + def get_directions(route,api_key = api): return get_data('getdirections',api_key,rt=route) + def get_stops(route,direction,api_key = api): return get_data('getstops',api_key,rt = route,dir=direction) + if __name__ == "__main__": if test: data = get_times('4752') diff --git a/print2d.py b/print2d.py new file mode 100644 index 0000000..076850b --- /dev/null +++ b/print2d.py @@ -0,0 +1,40 @@ +import datetime +from pydoc import pager +def str_coerce(s,**kwargs): + if isinstance(s,datetime.datetime): + return s.strftime(kwargs['datetime_format']) + else: + return str(s) +def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer = ' ',bottom = '=',l_end = '|',r_end = '|',interactive = False): + l = [[str_coerce(s,datetime_format = datetime_format) for s in row] for row in l] + + max_col = [] + for row in l: + for i,col in enumerate(row): + try: + max_col[i] = max(max_col[i],len(col)) + except IndexError: + max_col.append(len(col)) + + if l_end and r_end: + fmt_row = f'{l_end} {{content}} {r_end}' + else: + fmt_row = '{content}' + + done = [] + for row in l: + content = seperator.join(col.ljust(max_col[i],spacer) for i,col in enumerate(row)) + done.append(fmt_row.format(content = content)) + + if bottom: + bottom = bottom*len(done[0]) + row_sep = ('\n'+bottom+'\n') + else: + row_sep = '\n' + final = row_sep.join(done) + if bottom: + final = '\n'.join((bottom,final,bottom)) + if interactive: + pager(final) + else: + return final \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..414733f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +editdistance +requests \ No newline at end of file