This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusstop.py
131 lines (96 loc) · 3.07 KB
/
busstop.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import urlparse
from gevent.pywsgi import WSGIServer
import jinja2
from lxml import etree
import requests
BASE_URL = (
'http://old.mybustracker.co.uk/getBusStopDepartures.php'
'?refreshCount=0&clientType=b&busStopCode=%s&busStopDay=0&busStopService=0'
'&numberOfPassage=2&busStopTime=&busStopDestination=0')
jenv = jinja2.Environment(loader=jinja2.FileSystemLoader('templates/'))
def query_string(environ, qs, default=None):
query_string = urlparse.parse_qs(environ.get('QUERY_STRING', ''))
return query_string.get(qs, default)
def fetch_feed(bus_stop_code):
response = requests.post(BASE_URL % (bus_stop_code, ))
return response.text
def parse_feed_data(feed_data):
cleaned_data = feed_data\
.replace('<?xml version="1.0" encoding="iso-8859-1"?>', '')\
.replace('xmlns', 'xmlnamespace')
root = etree.fromstring(cleaned_data)
arrivals = []
for row in root.findall('.//pre'):
text = ''.join(row.xpath("./text()"))
line = text.strip().split()
service, time = line[0], line[-1]
if time == 'DUE':
time = '00'
if len(time) == 1:
time = '0' + time
arrivals.append((service, time, ))
return arrivals
def time_comparator(a, b):
a = a.strip('*')
b = b.strip('*')
if ':' in a and ':' in b:
return cmp(a, b)
if ':' in a:
return 1
if ':' in b:
return -1
return cmp(a, b)
def csv_formatter(arrivals):
r = []
if arrivals:
r.append('Serv,Time')
for serv, time in sorted(
arrivals, cmp=lambda a, b: time_comparator(a[1], b[1])):
if ':' not in time:
if time == '00':
time = 'Due'
else:
time += 'mins'
r.append(','.join([serv, time]))
return '\n'.join(r)
def html_formatter(title, arrivals):
data = []
for serv, time in sorted(
arrivals, cmp=lambda a, b: time_comparator(a[1], b[1])):
colour = '999'
try:
time_int = int(time, 10)
if time_int <= 5:
colour = 'f33'
elif time_int <= 10:
colour = 'f93'
except:
pass
if ':' not in time:
if time == '00':
time = 'Due'
else:
time += ' mins'
data.append((colour, serv, time, ))
return jenv.get_template('busstop.html').render(
title=title, data=data).encode()
def application(environ, start_response):
status = '200 OK'
headers = [
('Content-Type', 'text/html')
]
path = environ.get('PATH_INFO').strip('/')
code, fmt = path.split('.')
title = query_string(environ, 'title')
start_response(status, headers)
formatters = {
'csv': csv_formatter,
'html': lambda d: html_formatter(title, d),
}
formatter = formatters.get(fmt)
if formatter:
yield formatter(parse_feed_data(fetch_feed(code)))
yield ''
if __name__ == '__main__':
server = WSGIServer(('', 8000, ), application)
server.serve_forever()