You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
913 B

7 years ago
  1. from urllib import request
  2. import json
  3. import datetime
  4. import saveto
  5. from timer import timer
  6. bus_timer = timer(t='Bus times',e=True,o=False,a=True)
  7. api = saveto.load('cta_api_key')
  8. def get_times(stop_id,api_key = api):
  9. 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)
  10. data = json.load(request.urlopen(url))
  11. times = list(map(lambda time: datetime.datetime.strptime(time['prdtm'],"%Y%m%d %H:%M"),data["bustime-response"]["prd"]))
  12. return times
  13. walk = datetime.timedelta(seconds = 60*4)
  14. for i in range(6):
  15. l = get_times(4752)
  16. print(*map(lambda time: (str(time),str(time-walk)),l),sep = '\n',end = '\n\n')
  17. try:
  18. time = next(filter(bool,map(lambda time: time-walk if time-walk > datetime.datetime.today() else False,l)))
  19. bus_timer(time)
  20. except:
  21. print("no times")