-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·216 lines (182 loc) · 6.48 KB
/
__init__.py
File metadata and controls
executable file
·216 lines (182 loc) · 6.48 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
from flask.ext.sqlalchemy import SQLAlchemy
from flask.json import jsonify
from flask import Flask, send_file, send_from_directory, safe_join, request, Response, abort
from jinja2 import TemplateNotFound
from tests import makeJSON
import getpass
import sys
import random
import requests
# https://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python
print(getpass.getuser())
# sys.exit()
app = Flask(__name__)
# 'postgresql://maxilius@localhost:5432/scrape'
# Load config.py
#app.config.from_object('config')
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://api2k15:@127.0.0.1:3306/nba_flask'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://' + getpass.getuser() + '@localhost:5432/nba_flask'
print(app.config['SQLALCHEMY_DATABASE_URI'])
# sys.exit()
#app.config['WHOOSH_BASE'] = 'path/to/whoosh/base'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://api2k15:@127.0.0.1:3306/nba_flask_test'
db = SQLAlchemy(app)
import api_handlers
# Allows for any URL to be handled by AngularJS
# http://flask.pocoo.org/snippets/57/
@app.route('/', defaults={'path':''})
@app.route('/<path:path>')
def index(**kwargs):
return send_file('index.html')
# Allows us to not have to rely on Flask's default directory structure
# http://stackoverflow.com/questions/23685687/flask-html-js-css-img-reference-404-error
@app.route('/<any(assets, vendors):folder>/<path:filename>')
def toplevel_static(folder, filename):
filename = safe_join(folder, filename)
cache_timeout = app.get_send_file_max_age(filename)
return send_file(filename)
# The following are API calls
@app.route('/resources/players', methods=['GET','POST'])
def players_collection():
"""
This will process the collection of players by filtering
on the provided request arguments (within the HTTP Request
object). Optimally we will handle this async.
"""
if request.method == 'POST':
return 'Not yet implemented.'
# GET request:
# So SQLAlchemy takes filter_by arguments for queries by dict,
# so we can simply unpack the request params and pass them along
# Since this isn't raw SQL we don't need to sanitize the request,
# it'll be handled internally.
return api_handlers.players_collection_handler(request.args)
@app.route('/resources/player/view/<id>')
def player_view_by_id(id):
view = api_handlers.player_view_by_id_handler(id)
if view:
return view
else:
abort(404)
@app.route('/resources/player/<id>')
def player_by_id(id):
"""
This function will query the database for a given player
by name, and optionally will further filter the data
based on the provided attribute request arguments (HTTP request
object).
"""
player_data = api_handlers.player_by_id_handler(id)
resp = Response(player_data, status=200, mimetype='application/json')
if player_data:
return resp
else:
abort(404)
@app.route('/resources/teams', methods=['GET','POST'])
def teams_collection():
"""
This function will query the database for all teams and
filter based on the provided HTTP Request arguments.
"""
if request.method == 'POST':
return 'Not yet implemented'
# GET
return api_handlers.teams_collection_handler(request.args)
@app.route('/resources/team/view/<team_name>')
def team_view_by_id(team_name):
view = api_handlers.team_view_by_name_handler(team_name)
if view:
return view
else:
abort(404)
@app.route('/resources/team/<team_name>')
def team_by_name(team_name):
"""
This function will query the database for the team with
the given team_name and will further filter that request
based on optional parameters in the HTTP request object.
"""
team_data = api_handlers.team_by_name_handler(team_name)
if team_data:
return team_data
else:
abort(404)
@app.route('/resources/team/<team_name>/schedule')
def team_schedule(team_name):
"""
This function will return the schedule of games played
by the team given by team_name.
"""
# Requests all games filtered by home_team OR away_team
# equal team_name. Done with SQLAlchemy filter(_or..
schedule = api_handlers.team_schedule_handler(team_name)
if schedule:
return schedule
else:
abort(404)
@app.route('/resources/team/<team_name>/top_starters')
def team_top_starters(team_name):
"""
This function will return the top five players of given
team_name based on number of games started.
"""
return api_handlers.team_top_starters_handler(team_name)
@app.route('/resources/team/<team_name>/wins')
def team_wins(team_name):
"""
Returns the list of games won by team_name
"""
return api_handlers.team_wins_handler(team_name)
@app.route('/resources/team/<team_name>/losses')
def team_losses(team_name):
"""
Returns the list of games lost by team_name
"""
return api_handlers.team_losses_handler(team_name)
@app.route('/resources/games', methods=['GET','POST'])
def games_collection():
"""
This function will return all games, filtered by the parameters
provided in the HTTP request object.
"""
return api_handlers.games_collection_handler(request.args)
@app.route('/resources/game/view/<game_id>')
def game_view_by_id(game_id):
view = api_handlers.game_view_by_id_handler(game_id)
if view:
return view
else:
abort(404)
@app.route('/resources/game/<game_id>')
def game_by_id(game_id):
"""
This function will return a game object by ID.
"""
return api_handlers.game_by_id_handler(game_id)
@app.route('/resources/game/month/<month_id>')
def games_by_month(month_id):
"""
This function will return all games played in a given month.
"""
return api_handlers.game_by_month_handler(month_id)
@app.route('/resources/game/site/<site>')
def games_by_site(site):
"""
This function will return all games played at a given arena.
"""
return api_handlers.game_by_site_handler(site)
@app.route('/unittestbutton')
def test_results():
return makeJSON()
@app.route("/resources/search/<query>")
def search(query):
return api_handlers.search_by_query(query)
@app.route("/resources/otherAPI/get_other_data")
def get_other_data():
id = random.randint(1,14241)
#http://cfdb.me:5000/punt/players/"+id
return requests.get('http://cfdb.me:5000/punt/players/'+str(id)).content
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0", port=80)