-
Notifications
You must be signed in to change notification settings - Fork 1
/
bikeshare.py
136 lines (119 loc) · 4.25 KB
/
bikeshare.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
132
133
134
135
import syslog
from flask import Flask, request, render_template, send_from_directory
from flask.ext.bootstrap import Bootstrap
import json
import re
import syslog
import urllib2
from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper
import sstoreclient
import requests
app = Flask(__name__)
app.config.setdefault('BOOTSTRAP_SERVE_LOCAL',True)
bootstrap = Bootstrap(app)
# Set debug mode.
debug = False
apiurl = 'http://api.bikeshare.cs.pdx.edu'
# ================
# Main app function definitions
# ================
# This is a GET route to display the "view all stations" page
@app.route('/stations')
def view_all_stations():
apiroute = '/REST/1.0/stations/all'
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json()
return render_template('stations.html',stations_list=data['stations'],
list_len=len(data))
else:
return render_template('500.html')
# This is a GET route to display the "view all bikes" page
@app.route('/bikes')
def view_all_bikes():
apiroute = '/REST/1.0/bikes/all'
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json()
return render_template('bikes.html', bikes=data['bikes'])
else:
return render_template('500.html')
# This is a GET route to display the "view all riders" page
@app.route('/users')
def view_all_riders():
apiroute = '/REST/1.0/users/all'
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json()
return render_template('users.html', users=data['users'])
else:
return render_template('500.html')
# This is a GET route to display the landing page of bikeshare
@app.route('/')
def home():
apiroute = '/REST/1.0/stats'
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json()
return render_template('index.html', bikes=data['BIKES'],
active_bikes = data['ACTIVE_BIKES'], stations=data['STATIONS'],
users=data['USERS'], bikes_per_station=data['BIKES_PER_STATION'])
else:
return render_template('index.html')
# This is a GET route to display a single bike page of a given name
@app.route('/bike/<int:bike_id>')
def view_bike(bike_id):
apiroute = '/REST/1.0/bikes/info/' + str(bike_id)
print apiurl + apiroute
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json()
print data
return render_template('bike.html',bike_id=bike_id,
user_id=data['USER_ID'], lat=data['LATITUDE'],
lon=data['LONGITUDE'])
else:
return render_template('500.html')
# This is a GET route to display a single station page of a given name
@app.route('/station/<int:station_id>')
def view_station(station_id):
apiroute = '/REST/1.0/stations/info/' + str(station_id)
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json()
return render_template('station.html',station_id=station_id,
name=data['STATION_NAME'],addr=data['STREET_ADDRESS'],
lat=data['LATITUDE'],lon=data['LONGITUDE'],
num_bikes=data['CURRENT_BIKES'],num_docks=data['CURRENT_DOCKS'],
discount=data['CURRENT_DISCOUNT'])
else:
return render_template('500.html')
# This is a GET route to display a single user page of a given name
@app.route('/user/<user_name>')
def view_user(user_name):
apiroute = '/REST/1.0/users/info/' + user_name
r = requests.get(apiurl + apiroute)
if r.status_code == 200:
data = r.json ()
return render_template('user.html', user=data)
else:
return render_template('500.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/javascript/<path:path>', methods=['GET','OPTIONS'])
def js_proxy(path):
return send_from_directory(app.root_path + '/javascript/', path)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
if __name__ == '__main__':
if debug:
app.run(host='127.0.0.1', port=8081, debug=True)
else:
app.run(host='0.0.0.0', port=8081, debug=True)