import requests
import os
import untangle
import time
from led_sign.client import SignClient

def get_bart_estimates(station='ROCK', direction=None):
  API_URL = 'http://api.bart.gov/api/etd.aspx'

  data = {
    'cmd': 'etd',
    'key': os.environ['BART_KEY'],
    'orig': station,
    'dir': direction,
  }

  r = requests.get(API_URL, params=data)
  doc = untangle.parse(r.text)

  lines = []

  # for each estimated destination,
  for etd in doc.root.station.etd:
    destination = etd.destination.cdata
    mins = []
    cars = None
    for estimate in etd.estimate:
      mins.append(estimate.minutes.cdata)
      cars = estimate.length.cdata
    lines.append(format_estimate(destination, cars, mins))

  return lines

def format_estimate(destination, cars, mins):
  minutes = ', '.join(mins)
  first_line = '{}: {} car train'.format(destination, cars)
  second_line = '{} mins'.format(minutes)
  return [first_line, second_line]

def main():
  # New client to write to led sign
  pwd = os.path.dirname(os.path.realpath(__file__))
  led_sign_path = '/'.join([pwd, 'led_sign']) 
  glyphs_path = '/'.join([led_sign_path, 'glyphs'])

  sign_client = SignClient(glyphs_path, led_sign_path)

  while True:
    estimates = get_bart_estimates('ROCK', 's')
    for est in estimates:
      sign_client.send_text_to_sign(est)
      time.sleep(3)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
  main()

@@ -9,16 +9,27 @@
 equests%0A
+import sys%0A
 import o
@@ -96,16 +96,72 @@
 gnClient
+%0Afrom datetime import datetime%0Afrom pytz import timezone
 %0A%0Adef ge
@@ -258,16 +258,137 @@
 .aspx'%0A%0A
+  try:%0A    key = os.environ%5B'BART_KEY'%5D%0A  except KeyError:%0A    print('Set the BART_KEY environment variable')%0A    raise%0A%0A
   data =
@@ -423,31 +423,13 @@
 y': 
-os.environ%5B'BART_KEY'%5D,
+key, 
 %0A   
@@ -472,16 +472,18 @@
 on,%0A  %7D%0A
+  
 %0A  r = r
@@ -516,16 +516,144 @@
 s=data)%0A
+%0A  if r.status_code != 200:%0A    print(%22returned %7B%7D%22.format(r.status_code))%0A    return %5B%5B%22error: %7B%7D%22.format(r.status_code),''%5D%5D%0A%0A
   doc = 
@@ -726,16 +726,25 @@
 nation,%0A
+  try:%0A  
   for et
@@ -774,16 +774,18 @@
 td:%0A    
+  
 destinat
@@ -808,24 +808,26 @@
 ation.cdata%0A
+  
     mins = %5B
@@ -828,24 +828,26 @@
 ns = %5B%5D%0A    
+  
 cars = None%0A
@@ -846,16 +846,18 @@
  = None%0A
+  
     for 
@@ -884,24 +884,26 @@
 mate:%0A      
+ 
+ 
 mins.append(
@@ -928,24 +928,26 @@
 data)%0A      
+  
 cars = estim
@@ -967,16 +967,18 @@
 ata%0A    
+ 
+ 
 lines.ap
@@ -1028,23 +1028,318 @@
 s))%0A
-%0A  return lines
+  except:%0A    print(r.text)%0A    print(sys.exc_info()%5B0%5D)%0A    lines = %5B%5B'No trains','until next morning'%5D%5D%0A  lines.append(get_time_lines())  %0A  return lines%0A%0Adef get_time_lines():%0A  pst = timezone('US/Pacific')%0A  pst_time = datetime.now(pst)%0A  return %5Bpst_time.strftime('%25b %25d, %25Y'), pst_time.strftime('%25H:%25M')%5D
 %0A%0Ade
@@ -1432,22 +1432,8 @@
  '%7B%7D
-: %7B%7D car train
 '.fo
@@ -1804,16 +1804,27 @@
 e True:%0A
+    try:%0A  
     esti
@@ -1871,70 +1871,176 @@
     
-for est in estimates:%0A      sign_client.send_text_to_sign(est)
+  sign_client.send_text_to_sign(estimates)%0A      time.sleep(30)%0A    except:%0A      sign_client.send_text_to_sign(%5B'ERROR',''%5D)%0A      print(sys.exc_info()%5B0%5D)%0A      raise
 %0A   

update bart sign to send multiline and add date