-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbus.py
More file actions
38 lines (32 loc) · 1002 Bytes
/
bus.py
File metadata and controls
38 lines (32 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import random
import requests
import text
API = 'http://webservices.nextbus.com/service/publicJSONFeed'
messages = (
'%s\nBus safe! Precious cargo!',
'%s\nHave the best day!',
'%s\nSee ya later, alligator!',
'%s\nLove you to bits!',
'%s\nYou are the best!',
)
def bub_am_times():
params = {
'command': 'predictions',
'a': 'sf-muni',
'r': os.environ['BUB_ROUTE_AM'],
's': os.environ['BUB_STOP_AM'],
}
r = requests.get(API, params=params)
data = r.json()['predictions']
times = [int(p['minutes']) for p in data['direction']['prediction']]
last_time = times[-1]
times[-1] = 'or %s %s' % (last_time, 'minute' if last_time == 1 else 'minutes')
return u'{title} in {times}.'.format(
title=data['routeTitle'].replace('-', ' '),
times=', '.join([str(t) for t in times]))
def bub_am():
message = random.choice(messages)
times = bub_am_times()
out = message % times
text.bub(out)