From 20719cd0e9106a62802ce6a66805a166b46ae43c Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 18 Sep 2018 22:38:29 -0500 Subject: [PATCH] first files --- .gitignore | 3 +++ ctabus.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .gitignore create mode 100644 ctabus.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2a5d37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/cta_api_key.json +__pycache__ +*.pyc diff --git a/ctabus.py b/ctabus.py new file mode 100644 index 0000000..dbd4d8d --- /dev/null +++ b/ctabus.py @@ -0,0 +1,21 @@ +from urllib import request +import json +import datetime +import saveto +from timer import timer +bus_timer = timer(t='Bus times',e=True,o=False,a=True) +api = saveto.load('cta_api_key') +def get_times(stop_id,api_key = api): + url = "http://www.ctabustracker.com/bustime/api/v2/getpredictions?key={api_key}&stpid={stop_id}&format=json".format(stop_id = stop_id,api_key = api) + data = json.load(request.urlopen(url)) + times = list(map(lambda time: datetime.datetime.strptime(time['prdtm'],"%Y%m%d %H:%M"),data["bustime-response"]["prd"])) + return times +walk = datetime.timedelta(seconds = 60*4) +for i in range(6): + l = get_times(4752) + print(*map(lambda time: (str(time),str(time-walk)),l),sep = '\n',end = '\n\n') + try: + time = next(filter(bool,map(lambda time: time-walk if time-walk > datetime.datetime.today() else False,l))) + bus_timer(time) + except: + print("no times")