-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSON_request_scripts.py
More file actions
239 lines (187 loc) · 7.37 KB
/
JSON_request_scripts.py
File metadata and controls
239 lines (187 loc) · 7.37 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# ************************************************************* #
#
# FlickFinder Alpha 0.2
# Copyright (C) 2017 Jacob Hunt (jacobhuntgit@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
#
# ************************************************************* #
# libraries
import requests
from flask import render_template, redirect, request, session, url_for, jsonify
from sqlalchemy import *
from passlib.apps import custom_app_context as pwd_context
# local modules
from helpers import *
from config import *
from sql_tables import *
@app.route("/getFriendList", methods=["GET", "POST"])
def getFriendList():
""" Get a JSON object with the names and usernames of friends """
connection = engine.connect()
friendList = []
flTable = getFlTable(db, session["user_id"])
s = select([flTable])
result = connection.execute(s)
for row in result:
friendDict = {}
friendDict["firstName"] = getUserFirstName(row[1], db)
friendDict["lastName"] = getUserLastName(row[1], db)
friendDict["userName"] = getUser(row[1], db)
friendDict["userID"] = row[1]
friendList.append(friendDict)
connection.close()
return jsonify(friendList = friendList)
# ************************************************************* #
@app.route("/getFriendRequests", methods=["GET", "POST"])
def getFriendRequests():
""" Return a JSON object with pending friend requests the current
user has received """
# shorthand variable
userID = session["user_id"]
# get table prototypes
frTable = getFrTable(db, userID)
usrTable = getUsrTable(db)
# connect to database
connection = engine.connect()
# get user ids and store in array
idList = []
s = select([frTable.c.other_user_id]).where(frTable.c.received == 1)
result = connection.execute(s)
for row in result:
idList.append(row[frTable.c.other_user_id])
# get usernames and store in array
usrNameList = []
for item in idList:
s = select([usrTable.c.user]).where(usrTable.c.sql_id == item)
result = connection.execute(s)
result = result.fetchone()
usrNameList.append("@" + result.user)
connection.close()
return jsonify(idList=idList, usrNameList=usrNameList)
# ************************************************************* #
@app.route("/getGenres", methods=["GET", "POST"])
def getGenres():
""" Returns a JSON object with TMDB genres and corresponding key
values """
query = ("https://api.themoviedb.org/3/genre/movie/list?api_key="
+ API_KEY
+ "&language=en-US")
r = requests.get(query)
r = r.json()
return jsonify(r["genres"])
# ************************************************************* #
@app.route("/getMnList", methods=["GET", "POST"])
def getMnList():
""" Get a user's list of movie night IDs and timestamps """
# get table prototypes
mnTable = getMnTable(db)
userMnTable = getUserMnTable(db, session["user_id"])
# connect to database
connection = engine.connect()
# get user's movie night ids and add results to array
s = select([userMnTable.c.mn_id])
result = connection.execute(s)
if not result:
connection.close()
return jsonify(
success=False,
returnMessage=("Error when retreiving user's "
+ "list of movie nights")
)
userMnIdList = []
for row in result:
userMnIdList.append(row[userMnTable.c.mn_id])
# get user's movie night timestamps and add results to array
userMnDtList = []
for mnID in userMnIdList:
s = (select([mnTable.c.date_created])
.where(mnTable.c.sql_id == mnID))
result = connection.execute(s)
if not result:
connection.close()
return jsonify(
success=False,
returnMessage=("Error when retreiving user's "
+ "list of movie nights")
)
result = result.fetchone()
userMnDtList.append(result.date_created)
# close database connection
connection.close()
# return JSON object with lists
return jsonify(mnIdList=userMnIdList, mnDateTimeList=userMnDtList)
# ************************************************************* #
@app.route("/getUserMnPosition", methods=["GET", "POST"])
def getUserMnPosition():
""" Get a user's position in a given movie night """
# ensure that arguments were received
if not request.args.get("mnID"):
raise RuntimeError("missing script argument 'mnID'")
# shorthand variable
mnID = request.args.get("mnID")
# get table prototype
mnUsersTable = getMnUsersTable(db, mnID)
# connect to database
connection = engine.connect()
# get user's position info
s = (
select([mnUsersTable])
.where(mnUsersTable.c.user_id == session["user_id"])
)
result = connection.execute(s)
result = result.fetchone()
# return JSON object
return jsonify(page=result.user_page, depth=result.user_position)
# ************************************************************* #
@app.route("/getMatches", methods=["GET", "POST"])
def getMatches():
""" Get the matches for a given movie night """
# ensure that arguments were received
if not request.args.get("mnID"):
raise RuntimeError("missing script argument 'mnID'")
# shorthand variables
mnID = request.args.get("mnID")
userID = session["user_id"]
# get table prototypes
mnResultsTable = getMnResultsTable(db, mnID)
mnUsersTable = getMnUsersTable(db, mnID)
# connect to database
connection = engine.connect()
# ensure that logged in user is part of movie night
s = select([mnUsersTable]).where(mnUsersTable.c.user_id == userID)
result = connection.execute(s)
result = result.fetchone()
if not result:
return jsonify(success=False,
returnMessage=("Logged in user not in "
+ "requested movie night")
)
# query mnResultsTable for matches
s = (select([mnResultsTable.c.movie_id])
.where(mnResultsTable.c.match_status > 0))
result = connection.execute(s)
# create empty list to use as a return JSON
matchInfoList = []
# for each result
for row in result:
# query TMDB for movie data
movieInfo = getMovieInfo(API_KEY, row[mnResultsTable.c.movie_id])
# append result to matchInfoList
matchInfoList.append(movieInfo)
# return JSON object
returnMessage = "Successfully retrieved matches for current movie night."
return jsonify(success=True,
returnMessage=returnMessage,
matches=matchInfoList)