This repository was archived by the owner on May 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbussim.py
48 lines (43 loc) · 1.58 KB
/
bussim.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
#!/usr/bin/env python
# bussim.py
#
# Simulated access to CTA website for the "Learn Python through Public Data Hacking"
# tutorial. Run this script if you have no internet access or connectivity
# is very poor.
from datetime import datetime
import os
import gzip
import re
import time
try:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from http.server import BaseHTTPRequestHandler, HTTPServer
dirname = os.path.join(os.path.dirname(__file__),'data','buses')
basetime = datetime.today()
files = sorted([name for name in os.listdir(dirname)
if name.endswith('.xml.gz')])
class BusRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
elapsed = datetime.today() - basetime
minutes = (elapsed.seconds // 60)
filename = files[minutes]
fullname = os.path.join(dirname, filename)
f = gzip.open(fullname, 'rb')
data = f.read()
f.close()
data = re.sub(br'<time>(.*?)</time>',
lambda m: b'<time>' + time.ctime().encode('ascii') + b'</time>', data)
self.send_response(200, 'OK')
self.send_header('content-type', 'application/xml')
self.end_headers()
self.wfile.write(data)
from functools import partial
serv = HTTPServer(('', 8800), BusRequestHandler)
print('''
Simulated bus-system API server running at http://localhost:8800.
In your code, change all URLs from http://ctabustracker.com/
to http://localhost:8800/. Note: this server only knows about
a single bus route (#22) as described in the tutorial.
''')
serv.serve_forever()