Skip to content

Instantly share code, notes, and snippets.

@rdhyee
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdhyee/26784caa441d30894712 to your computer and use it in GitHub Desktop.
Save rdhyee/26784caa441d30894712 to your computer and use it in GitHub Desktop.
bart calculation
import requests
import urllib
from datetime import timedelta
from lxml.etree import fromstring
from dateutil.parser import parse
from dateutil.tz import tzlocal
from pandas import DataFrame
import numpy as np
try:
# to allow import of a registered key if it exists
from settings import BART_API_KEY
except:
BART_API_KEY = "MW9S-E7SL-26DU-VV8V"
from pytz import timezone
pacific_tz = timezone("US/Pacific")
def etd(orig, key=BART_API_KEY):
url = "http://api.bart.gov/api/etd.aspx?" + \
urllib.urlencode({'cmd':'etd',
'orig':orig,
'key':key})
r = requests.get(url)
doc = fromstring(r.content)
estimations_list = []
# parse the datetime for the API call
s = doc.find('date').text + " " +doc.find('time').text
call_dt = parse(s)
# turn the results into a rectangular format
# parse the station
for station in doc.findall('station'):
etds = station.findall('etd')
for etd in etds:
estimates = etd.findall('estimate')
for estimate in estimates:
estimate_tuple = [(child.tag, child.text) for child in estimate.iterchildren()]
estimate_tuple += [('call_dt', call_dt),
('station', station.find('abbr').text),
('destination', etd.find('abbreviation').text)]
estimations_list.append(dict(estimate_tuple))
return estimations_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment