document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import argparse
  2. import csv
  3. import datetime
  4. import io
  5. import logging
  6. import pip
  7. try:
  8.     import requests
  9. except:
  10.     pip.main([\'install\', \'requests\'])
  11. import StringIO
  12. import tempfile
  13. import zipfile
  14. logging.basicConfig(level=logging.DEBUG)
  15.  
  16. now = datetime.datetime.now()
  17. is_weekday = \'Weekday\'
  18. if now.strftime(\'%w\') == 0 or now.strftime(\'%w\') == 6:
  19.     is_weekday = now.strftime(\'%A\')
  20. logging.debug(\'Schedule is {}\'.format(is_weekday))
  21.  
  22. parser = argparse.ArgumentParser(description=\'Find out when the next Caltrain is\')
  23. parser.add_argument(\'--stop\', type=str, help=\'Stop Name\')
  24. args = parser.parse_args()
  25.  
  26. # store our times in a list along with which way the train is going
  27. times = []
  28.  
  29. # retrieve the information from remote
  30. URL = \'http://www.caltrain.com/Assets/GTFS/caltrain/GTFS-Caltrain-Devs.zip\'
  31. ctx = requests.get(URL, stream=True)
  32.  
  33. tfn = tempfile.NamedTemporaryFile(prefix="caltrain",suffix=".gtfs.zip")
  34. tfn.write(ctx.content)
  35. stop_name = \'\'
  36. with zipfile.ZipFile(tfn, "r") as zipfp:
  37.     csv_ = zipfp.open(\'stops.txt\', \'rU\')
  38.     reader = csv.DictReader(csv_, fieldnames=[\'stop_id\',\'stop_code\',\'stop_name\',\'stop_desc\',\'stop_lat\',\'stop_lon\',\'zone_id\',\'stop_url\',\'location_type\',\'parent_station\',\'platform_code\'])
  39.     for line in reader:
  40.         if args.stop.lower() in line[\'stop_name\'].lower():
  41.             stop1 = \'{0} -- {1}\'.format(line[\'stop_code\'], line[\'platform_code\'])
  42.             stop2 = \'{0} -- {1}\'.format(str(int(line[\'stop_code\'])+1), line[\'platform_code\'])
  43.             stop_name = line[\'stop_name\']
  44.             break
  45.     reader = csv.DictReader(zipfp.open(\'stop_times.txt\',\'rU\'), fieldnames=[\'trip_id\',\'arrival_time\',\'departure_time\',\'stop_id\',\'stop_sequence\',\'pickup_type\',\'drop_off_type\'])
  46.     stop_id1 = stop1[:stop1.index(\' -- \')]
  47.     stop_id2 = stop2[:stop2.index(\' -- \')]
  48.     for line in reader:
  49.         if line[\'stop_id\'] == stop_id1:
  50.             if is_weekday in line[\'trip_id\']:
  51.                 depart_time = line[\'departure_time\']
  52.                 current_time = now.strftime(\'%H:%M:%S\')
  53.                 if depart_time > current_time:
  54.                     times.append(depart_time)
  55.         elif line[\'stop_id\'] == stop_id2:
  56.             if is_weekday in line[\'trip_id\']:
  57.                 depart_time = line[\'departure_time\']
  58.                 current_time = now.strftime(\'%H:%M:%S\')
  59.                 if depart_time > current_time:
  60.                     times.append(depart_time)
  61. times = sorted(times)
  62. print \'{0}, {1} are your next trains from {2}\'.format(times[0], times[1], stop_name)
  63.  
  64. tfn.close()
');