forked from ark930/shanghai-bus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.py
68 lines (49 loc) · 1.6 KB
/
router.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
#-*-coding:utf-8 -*-
from flask import Flask
from flask import request
from flask import jsonify
from bus import Bus
from InvalidParameterException import InvalidParameterException
app = Flask(__name__)
@app.route('/')
def hello_world():
return '<h1>Hello World</h1>'
@app.route('/bus/<router_name>/stop/<stop_id>')
def query_stop(router_name, stop_id):
direction = request.args.get('direction', '0')
router_name = router_name.encode('utf-8')
bus = Bus()
res = bus.query_stop(router_name, direction, stop_id)
return jsonify(res)
@app.route('/bus/<router_name>')
def query_router(router_name):
direction = request.args.get('direction', '0')
router_name = router_name.encode('utf-8')
bus = Bus()
routers = bus.query_router(router_name, direction)
return jsonify(routers)
@app.route('/bus/<router_name>/details')
def query_router_details(router_name):
direction = request.args.get('direction', '0')
router_name = router_name.encode('utf-8')
bus = Bus()
router_details = bus.query_router_details(router_name, direction)
return jsonify(router_details)
@app.errorhandler(404)
def not_found(e):
return jsonify({
'error': 'not_found',
'error_msg': '链接不存在'
}), 404
@app.errorhandler(500)
def internal_server_error(e):
return jsonify({
'error': 'internal_server_error',
'error_msg': '服务器内部错误'
}), 500
@app.errorhandler(InvalidParameterException)
def handle_invalid_parameter(e):
response = jsonify(e.to_dict())
response.status_code = e.status_code
return response
app.run(port=8888)