/
say-it-louder.py
691 lines (525 loc) · 22 KB
/
say-it-louder.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
from flask import Flask,make_response,render_template,redirect,session,escape,request,url_for
from mysql import MySQLDatabase
from imdb_module import IMDataBase
import json
db = MySQLDatabase('imdb','imdb','imdb','localhost')
# imDB = IMDataBase()
DEBUG = 1
# http://flask.pocoo.org/docs/0.10/quickstart/
app = Flask(__name__)
app._static_folder = '/Users/juan/say-it-louder/static/'
# set the secret key. keep this really secret:
app.secret_key = 'This_is_a_secret'
######################################################################################################################
# Main App and common options #
######################################################################################################################
@app.route('/')
def index():
# if not 'user_name' in session:
# if DEBUG:
# print "Not logged in. Redirecting to /login"
# return redirect("/login")
# user_name = session['user_name']
#
# # Check whether the user is already in a game
# if session.has_key('game_id') :
# if DEBUG:
# print 'User in session[game_id]',session['game_id']
# #
# pass
# #
#
# else:
# # We are not in any game yet
# # Check DB for current games
# game_id = db.check_open_game()
# if DEBUG:
# print 'Return of check_open_game()',game_id
#
# if game_id:
# # There is a game open. Joining!
# db.join_game(user_name,game_id)
# if DEBUG:
# print 'Joining game_id',game_id
# you_open_game = False
# session['game_id'] = game_id
#
# else:
# # No game open. Let's open it!
# game_id = db.create_game(user_name)
# if DEBUG:
# print 'Created game_id',game_id
# you_open_game = True
# session['game_id'] = game_id
#
# return render_template('index.html', user_name = user_name, you_open_game = you_open_game)
return render_template('index.html')
@app.route('/login')
def login_html():
return render_template('login.html')
@app.route('/login_post', methods=['GET', 'POST'])
def login_post():
# Method POST
if request.method == 'POST':
session['user_name'] = request.form['user_name']
user_name = session['user_name']
# Is the user already in the system?
if db.check_for_duplicate_user( session['user_name'] ):
if DEBUG:
print 'User %s already in the system trying POST. Sending back to /login' % session['user_name']
# Destroying session and send to retry to /login
session.pop('user_name', None)
return redirect('/#/login', code=302)
# User not in the system
else:
# We are not in any game yet
# Check DB for current games (Where there is no Player_B)
game_id = db.check_open_game()
if DEBUG:
print 'Return of check_open_game()',game_id
if game_id:
# There is a game open. Joining!
db.join_game(user_name,game_id)
if DEBUG:
print 'Joining game_id',game_id
session['player'] = 'B'
session['game_id'] = game_id
#return redirect('/resolve/movie_selected/'+str(game_id), code=302)
return redirect('/resolve/', code=302)
else:
# No game open. Let's open it!
game_id = db.create_game(user_name)
if DEBUG:
print 'Created game_id',game_id
session['player'] = 'A'
session['game_id'] = game_id
return redirect('/challenge/', code=302)
# Method GET
else:
# User already with session but trying to log-in again
if 'user_name' in session:
user_name = session['user_name']
if DEBUG:
print "GET /login with user_name %s" % user_name
return redirect('#/status', code=302)
# No session detected and no POST? Render login.html
else:
#session.pop('user_name', None)
return render_template('login.html')
@app.route('/logout')
def logout():
# remove the username from the session if it's there
session.pop('user_name', None)
return redirect('/', code=302)
# Main Status page
@app.route('/status.html')
def status_html():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
response = '<h2>Status Page</h2>'
response += '<h3>Hello '+user_name+'!</h3>'
if session['game_id']:
response += '<h4>You are Player_'+session['player']+' in Game_ID:'+str(session['game_id'])+'</h4>'
response += 'This is the current status:'
game = ''
games_total = db.get_games()
response += '<ul>'
for item in games_total:
game_id = item['game_id']
game += '<li>GameID:'+str(game_id)+' Player_A:'+item['player_a']
if item.has_key('player_b'):
game += ' Player_B:'+str(item['player_b'])
if item.has_key('movieID'):
# game += ' MovieID:'+str(item['movieID'])
title,year = db.get_movie_title_by_movieID(item['movieID'])
game += ' Title:"'+str(title)+'" ('+str(year)+')'
# if item.has_key('keywords_a'):
# game += ' Keywords_A:'+str(item['keywords_a'])
# if item.has_key('keywords_b'):
# game += ' Keywords_B:'+str(item['keywords_b'])
#if item.has_key('solved'):
# game += ' Solved:'+str(item['solved'])
# If the game is solved -> No options
if (item['solved'] == 2):
game += ' Solved'
# When the game is not solved + Player_B is empty + Player_A is a different user-> Option to Join
elif (item['solved'] != 2) and ( db.get_player_a_by_game_id(game_id) != user_name ) and ( db.get_player_b_by_game_id(game_id) == False ):
game += ' <a href="/join_game/'+str(game_id)+'">Join</a>'
# When the game is not solved and user_name is Player_B -> Option to continue playing
elif (item['solved'] == None ) and ( db.get_player_b_by_game_id(game_id) == user_name ):
game += ' <a href="/resolve/#/resolve/movie_selected/">Continue</a>'
game += '</li>'
response += game
game = ''
response += '</ul>'
# When the games we have started are all of them solved -> Give the option to create a new one
#if
if DEBUG:
print response
return response
@app.route('/search_movie')
def search_movie():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
#user_name = session['user_name']
return render_template('search_movie.html')
@app.route('/search/<title>')
def imdb_search(title):
result = '<!DOCTYPE html><html><head><title></title><style></style></head><body>'
print 'Search :"'+title+'"'
movies = db.search(title)
if DEBUG:
print movies
if len(movies) == 0:
return 'Sorry. No movies found. Please change your search.'
print 'Total returned:',len(movies)
for item in movies:
line = '<div><a href="/search/'+item['title']+'">'+item['title']+' ('+str(item['year'])+')</a></div>'
result += line
if DEBUG:
print line
result += '</body></html>'
if DEBUG:
print result
return result
# Player_B gives up
@app.route('/give_up')
def give_up():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
game_id = session['game_id']
session['player'] =''
session['game_id'] =''
if DEBUG:
print "User %s is giving up for game_id: %i" % (user_name,game_id)
db.remove_player_b(game_id)
return redirect("#/status", code=302)
# Join Game
@app.route('/join_game/<game_id>')
def join_game(game_id):
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
game_id = int(game_id)
db.join_game(user_name,game_id)
if DEBUG:
print 'Joining game_id %i from Status Page' % game_id
session['player'] = 'B'
session['game_id'] = game_id
return redirect('/resolve/', code=302)
######################################################################################################################
# Challenge App #
######################################################################################################################
@app.route('/challenge/')
def challenge():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
return render_template('challenge.html', user_name = user_name )
@app.route('/challenge/search_movie')
def search_movie_challenge():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
#user_name = session['user_name']
return render_template('search_movie_challenge.html')
@app.route('/challenge/movie_selected/')
def movie_selected_challenge():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
#user_name = session['user_name']
#game_id = session['game_id']
#movieID = int(movieID)
#if DEBUG:
# print "Updating game_id %i with movieID %i" % (game_id, movieID)
#db.update_movieID(movieID,game_id)
#movieID = db.get_movieID_by_game_id(session['game_id'])
#if movieID:
# title, year = db.get_movie_title_by_movieID(movieID)
return render_template('movie_selected_challenge.html')
# Recover Keywords from the Challenge
@app.route('/challenge/keywords', methods=['GET', 'POST'])
def challenge_keywords():
# Method POST
if request.method == 'POST':
result = []
if request.form.has_key('keyword1') and request.form['keyword1'] != '':
keyword1 = request.form['keyword1']
result.append(str(keyword1))
if DEBUG:
print "Keyword1:",keyword1
if request.form.has_key('keyword2') and request.form['keyword2'] != '':
keyword2 = request.form['keyword2']
result.append(str(keyword2))
if DEBUG:
print "Keyword2:",keyword2
if request.form.has_key('keyword3') and request.form['keyword3'] != '':
keyword3 = request.form['keyword3']
result.append(str(keyword3))
if DEBUG:
print "Keyword3:",keyword3
if request.form.has_key('keyword4') and request.form['keyword4'] != '':
keyword4 = request.form['keyword4']
result.append(str(keyword4))
if DEBUG:
print "Keyword4:",keyword4
if request.form.has_key('keyword5') and request.form['keyword5'] != '':
keyword5 = request.form['keyword5']
result.append(str(keyword5))
if DEBUG:
print "Keyword5:",keyword5
if request.form.has_key('keyword6') and request.form['keyword6'] != '':
keyword6 = request.form['keyword6']
result.append(str(keyword6))
if DEBUG:
print "Keyword6:",keyword6
if DEBUG:
print "Keywords from challenge:",result
# Save keywords for the current game
db.update_keywords_a(result,session['game_id'])
# Save keywords for our collection
movieID = db.get_movieID_by_game_id(session['game_id'])
db.store_keywords(movieID,result,session['user_name'])
return redirect("#/status", code=302)
# Method GET
else:
return "Method not allowed"
######################################################################################################################
# Resolve App #
######################################################################################################################
@app.route('/resolve/')
def resolve():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
player_a = db.get_player_a_by_game_id(session['game_id'])
return render_template('resolve.html', user_name = user_name, player_a = player_a )
@app.route('/resolve/movie_selected/')
def movie_selected_resolve():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
player_a = db.get_player_a_by_game_id(session['game_id'])
keywords = db.get_keywords_player_a_by_game_id( session['game_id'] )
return render_template('movie_selected_resolve.html',user_name = user_name, player_a = player_a, words = len(keywords) )
# Recover Keywords to Resolve from POST
@app.route('/resolve/movie_selected/keywords/', methods=['GET', 'POST'])
def resolve_keywords():
# Method POST
if request.method == 'POST':
result = []
if request.form.has_key('keyword1') and request.form['keyword1'] != '':
keyword1 = request.form['keyword1']
result.append(str(keyword1))
if DEBUG:
print "Keyword1:",keyword1
if request.form.has_key('keyword2') and request.form['keyword2'] != '':
keyword2 = request.form['keyword2']
result.append(str(keyword2))
if DEBUG:
print "Keyword2:",keyword2
if request.form.has_key('keyword3') and request.form['keyword3'] != '':
keyword3 = request.form['keyword3']
result.append(str(keyword3))
if DEBUG:
print "Keyword3:",keyword3
if request.form.has_key('keyword4') and request.form['keyword4'] != '':
keyword4 = request.form['keyword4']
result.append(str(keyword4))
if DEBUG:
print "Keyword4:",keyword4
if request.form.has_key('keyword5') and request.form['keyword5'] != '':
keyword5 = request.form['keyword5']
result.append(str(keyword5))
if DEBUG:
print "Keyword5:",keyword5
if request.form.has_key('keyword6') and request.form['keyword6'] != '':
keyword6 = request.form['keyword6']
result.append(str(keyword6))
if DEBUG:
print "Keyword6:",keyword6
if DEBUG:
print "Keywords from resolve",result
# Save keywords for this game
db.update_keywords_b(result,session['game_id'])
# Save keywords for our collection
movieID = db.get_movieID_by_game_id(session['game_id'])
db.store_keywords(movieID,result,session['user_name'])
return redirect("/resolve/#/resolve/movie_selected/keywords/check/", code=302)
# Method GET
else:
return "Method not allowed"
# Compare keywords from player_a and player_b
@app.route('/resolve/movie_selected/keywords/check/')
def resolve_keywords_check():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
user_name = session['user_name']
game_id = session['game_id']
response = '<br>'
response += '<h3> Keywords Check for "{{ title }}" ({{ year }})</h3>'
player_a = db.get_keywords_player_a_by_game_id(game_id)
player_b = db.get_keywords_player_b_by_game_id(game_id)
# if len(player_b) == 1:
# response += '<div><h4>You selected this single keyword:</h4><ul>'
# response += '<li class="thumbnail">"'+str(player_b[0])+'"</li>'
# response += '</ul></div>'
# else:
# response += '<div><h4>You selected these '+str(len(player_b))+' keywords:</h4><ul>'
# for item in player_b:
# response += '<li class="thumbnail">"'+item +'"</li>'
# response += '</ul></div>'
matches = []
response += '<br><div><h4>Matches:</h4>'
for item_b in player_b:
match = ''
for item_a in player_a:
if item_b == item_a:
match = item_a
if match:
response += '<li class="bg-success">"'+match+'"</li>'
matches.append(match)
else:
response += '<li class="bg-danger">"'+item_b+'"</li>'
response += '</ul></div>'
response += '<h4>Total: '+str(len(matches))+'</h4>'
if len(player_a) == len(matches):
# Game solved
response += '<h2>Congratulations '+user_name+'! You solved game: '+str(game_id)+'</h2>'
db.update_game_solved(game_id)
else:
# Not solved
response += '<br><a href="/resolve/#/resolve/movie_selected/">Try again!</a>'
response += '<br><a href="/give_up">Give Up</a>'
response += '<br><a href="/#/status">Status Page</a>'
return response
######################################################################################################################
# API #
######################################################################################################################
# Search movies by free text
@app.route('/api/v0/search/<query>/movie')
def imdb_api_search(query):
#result = {}
print 'Search :"'+query+'"'
movies = db.search(query,kind='movie')
if DEBUG:
print movies
print 'Total returned:',len(movies)
if len(movies) == 0:
result = "{ 'error': { 'status': 400, 'message': 'not a valid search' } }"
result = json.dumps( result, sort_keys=True, indent=4, separators=(',', ': ') )
else:
result = json.dumps( movies, sort_keys=True, indent=4, separators=(',', ': ') )
#result = json.dumps( movies )
if DEBUG:
print result
response = make_response( result )
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# API call (Secure) to update the movieID is GAMES extracting it from the URL and the game_id from he session
@app.route('/api/v0/secure/update/movieID/<movieID>')
def imdb_api_update_movieID(movieID):
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
#user_name = session['user_name']
game_id = int(session['game_id'])
movieID = int(movieID)
if DEBUG:
print "Updating game_id %i with movieID %i" % (game_id, movieID)
db.update_movieID(movieID,game_id)
#result = "{ 'success': { 'status': 100, 'message': 'movieID updated' } }"
result = '{ "success": 100 }'
response = make_response( result )
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# API call (Secure) to get the movieID for a game using game_id from SESSION
@app.route('/api/v0/secure/get/game_id/')
def imdb_api_get_game_id_from_session():
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
result = {}
game_id = session['game_id']
if DEBUG:
print "Getting movieID from game_id:",game_id,"using session[game_id]"
movieID = db.get_movieID_by_game_id(game_id)
if DEBUG:
print "movieID obtained", movieID
result['movieID'] = movieID
result = json.dumps( result, sort_keys=True, indent=4, separators=(',', ': ') )
if DEBUG:
print result
response = make_response( result )
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# API call (Secure) to get the movieID for a game using game_id FROM URL
@app.route('/api/v0/secure/get/game_id/<game_id>')
def imdb_api_get_game_id_from_URL(game_id):
if not 'user_name' in session:
if DEBUG:
print "Not logged in. Redirecting to /login"
return redirect("/login", code=302)
result = {}
if DEBUG:
print "Getting movieID from game_id:",game_id,"using URLparam."
movieID = db.get_movieID_by_game_id(game_id)
if DEBUG:
print "movieID obtained", movieID
result['movieID'] = movieID
result = json.dumps( result, sort_keys=True, indent=4, separators=(',', ': ') )
if DEBUG:
print result
response = make_response( result )
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# Get movie title and year by movieID
@app.route('/api/v0/get/title/<movieID>')
def imdb_api_get_title(movieID):
result = {}
movieID = int(movieID)
title, year = db.get_movie_title_by_movieID(movieID)
result['movieID'] = movieID
result['title'] = title
result['year'] = year
result = json.dumps( result, sort_keys=True, indent=4, separators=(',', ': ') )
if DEBUG:
print result
response = make_response( result )
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
if __name__ == '__main__':
app.run(debug=False,
host="127.0.0.1",
port=5000,
threaded=True,
#processes=10
)