Created
August 5, 2019 22:58
-
-
Save derwiki/e9189fa887d53a7d3dcfe7834a28b154 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import re | |
URLS = { | |
'10 Inbound Wisconsin & Madera': 'http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=sf-muni&r=10&s=6966', # noqa E501 | |
'48 Inbound Wisconsin & 25th': 'http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=sf-muni&r=48&s=3513', # noqa E501 | |
} | |
SECONDS_MINUTES_RE = re.compile('seconds="(\d+)" minutes="(\d+)"') | |
def format_prediction(seconds, minutes): | |
return '{minutes}m{seconds}s'.format( | |
minutes=minutes, | |
seconds=int(seconds) % 60, | |
) | |
def request_predictions(): | |
predictions = {} | |
for stop, url in URLS.items(): | |
connection = urllib.urlopen(url) | |
raw = connection.read() | |
connection.close() | |
rs = SECONDS_MINUTES_RE.findall(raw) | |
predictions[stop] = [format_prediction(*tuple) for tuple in rs] | |
return predictions | |
print(request_predictions()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment