-
Notifications
You must be signed in to change notification settings - Fork 0
/
serv.py
289 lines (244 loc) · 9.61 KB
/
serv.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
import flask
import flask_socketio
from flask_socketio import join_room, leave_room
from flask import request
import discoSounds as ds
import os
import json
import datetime
import requests
import facebook
import time
from song_update_service import *
import events, log
from schema import *
public_room = 912837
app = flask.Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL','postgresql://jcrzr:anchor99@localhost/postgres')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']= 0
db.init_app(app)
socket = flask_socketio.SocketIO(app)
# default route
thread_holder = SongThreadHolder(socket)
@app.route('/')
def start():
return flask.render_template('index.html')
# @socket.on(events.CONNECT)
# def on_connect():
# log.sock(events.CONNECT)
# adds a client to the public room
@socket.on('join room')
def on_join_room(data):
sessionid = request.sid
print("********************************************************joined room*************************************************************")
join_room(public_room)
# should be called when connected to get a list of songs for the room
# will take a 'genre' as an arguement, so this needs to be passed in
@socket.on('get songs')
def on_get_songs(data):
genre = data['genre']
songs = ds.getSongList(genre)
print("********************************************SONGLIST triggered******************************************************************")
socket.emit('song list', songs, room=public_room)
#expects floor_id, and song_id as arguments.
#updates songlist with song_picked added as the next song in the playlist to be played.
@socket.on('song picked')
def on_song_picked(data):
current_song = data['song']
thread_holder.find_thread(data['floor_id']).update_list(current_song['id'])
current_song['stream_url'] = stream_url_loc
socket.emit('song to play', current_song, room=floor_id)
"""this listener is expecting key:pair list (i.e json) with either fb_t for facebook token, or google_t for google token
if user exists, function will validate and pass back user information and auth status. else, it will add user reference to schema"""
@socket.on(events.LOGIN)
def on_login(data):
log.sock(events.LOGIN)
if 'fb_t' not in data:
# print('google_t: ')
# print(data)
response = requests.get(
'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' + data['google_t'])
json = response.json()
# print(json)
fname = json['given_name']
lname = json['family_name']
link = json['picture']
email = json['email']
mem_found = memberExists_by_email(email)
# print(mem_found)
if mem_found:
mem = getMemberObject_by_email(email)
log.emit(events.LOGIN_STATUS)
socket.emit(events.LOGIN_STATUS, userEmit(mem), room=request.sid)
else:
new_mem = registerMember(fname,lname,email,link)
log.emit(events.LOGIN_STATUS)
socket.emit(events.LOGIN_STATUS, userEmit(new_mem), room=request.sid)
#
if 'google_t' not in data:
graph = facebook.GraphAPI(access_token=data['fb_t'],version='2.7')
js = graph.get_object(id='me', fields='first_name,last_name,email,picture')
print("js:",js)
fname = js['first_name']
lname = js['last_name']
email = js['email']
print("***************************EMAIL************************")
link = js['picture']['data']['url']
mem_found = memberExists_by_email(email)
print(mem_found)
if mem_found:
mem = getMemberObject_by_email(email)
log.emit(events.LOGIN_STATUS)
socket.emit(events.LOGIN_STATUS, userEmit(mem), room=request.sid)
else:
new_mem = registerMember(fname,lname,email,link)
log.emit(events.LOGIN_STATUS)
socket.emit(events.LOGIN_STATUS, userEmit(new_mem), room=request.sid)
@socket.on(events.MESSAGE_SEND)
def on_new_message(data):
log.sock(
events.MESSAGE_SEND+"\n"+
log.get_json(data)
)
floor_id = data['floor']
member_id = data['from']
text = data['message']
add_message(floor_id, member_id, text)
log.emit(events.MESSAGE_LIST_UPDATE)
# print("floor messages", getFloorMessages(floor_id))
socket.emit(events.MESSAGE_LIST_UPDATE, {
'floor_messages': getFloorMessages(floor_id)}, room=floor_id)
#function will return floor list object after initializing the object
#the function should users associated with it. member_id and floorid
#floor_name and a floor_genre
# @app.route('/floors')
@socket.on(events.GET_FLOOR_LIST)
def on_get_floor_list(data):
log.sock(events.GET_FLOOR_LIST)
log.emit(events.FLOOR_LIST_UPDATE)
socket.emit(events.FLOOR_LIST_UPDATE, getPublicFloors())
#function assumes that the client sends
@socket.on('create floor')
def on_create(data):
print("data: ",json.dumps(data,indent=4))
if data['is_public'] == 1:
public = True
else:
public = False
print("******************join songlist message")
flag, new_floor = add_floor(data['floor_name'],data['member_id'],public,data['floor_genre'])
if flag == True:
new_floor.add_member(data['member_id'])
join_room(new_floor.floor_id)
genre = data['floor_genre']
songs = ds.getSongList(genre)
thread_holder.add_thread(new_floor.floor_name,new_floor.floor_id,songs)
time.sleep(2)
new_floor.set_songlist(thread_holder.find_thread(new_floor.floor_id).songlist)
updated_floor = getFloor(new_floor.floor_id)
socket.emit('floor created', {'floor':updated_floor.to_list()},room=new_floor.floor_id)
log.emit(events.FLOOR_LIST_UPDATE)
socket.emit(events.FLOOR_LIST_UPDATE, getPublicFloors())
else:
socket.emit('error',{'message':new_floor})
@socket.on(events.JOIN_FLOOR)
#join room, function expects data to be json array/objects
# expects keys 'floor_id', 'member_id, returns jsonarray to parse
def on_join_floor(data):
floor_id = data['floor_id']
member_id = data['member_id']
log.sock(events.JOIN_FLOOR+'\n'+
log.get_mapf(data, log.BOLD, log.CYAN)
)
join_room(floor_id)
floor_to_join = getFloor(floor_id)
floor_to_join.add_member(member_id)
# print(floor_to_join.to_list())
#need to check if floor exists before creating thread.
if thread_holder.find_thread(floor_id) is None:
#create a new songlist update thread
print("thread is none")
floor_to_join = getFloor(floor_id)
floor_list = floor_to_join.to_list()
new_thread = thread_holder.add_thread(floor_to_join.floor_name,floor_to_join.floor_id,floor_list['songlist'])
#refresh floor object
time.sleep(2)
floor_to_join.set_songlist(new_thread.songlist)
floor_to_join = getFloor(floor_id)
floor_list = floor_to_join.to_list()
socket.emit('floor joined', {'floor':floor_list}, room=request.sid)
# print("floor",json.dumps(floor_list[0:4],indent=4))
# print("***memlist update***")
# print(floor_to_join.to_list()['floor_members'])
socket.emit('member list update', {'floor members': floor_list['floor_members']},room=floor_to_join.floor_id)
else:
print("thread is active")
floor_to_join.set_songlist(thread_holder.find_thread(floor_to_join.floor_id).songlist)
#refresh floor object after new songlist has been updated
floor_to_join = getFloor(floor_id)
print("****floor songlist***")
floor_list = floor_to_join.to_list()
# print(json.dumps(floor_list['songlist'][0:3],indent=4))
socket.emit('floor joined', {'floor':floor_to_join.to_list()}, room=request.sid)
# print("***memlist update***")
# print(floor_list['floor_members'])
socket.emit('member list update', {'floor members': floor_list['floor_members']},room=floor_to_join.floor_id)
@socket.on(events.LEAVE_FLOOR)
def on_leave_floor(data):
log.sock(events.LEAVE_FLOOR)
current_floor = getFloor(data['floor_id'])
current_floor.rm_member(data['member_id'])
current_floor = getFloor(data['floor_id'])
leave_room(data['floor_id'])
# socket.emit('member left', {'floor':current_floor.to_list()}, room=data['floor_id'])
socket.emit('member list update', {'floor members': current_floor.to_list()['floor_members']},room=data['floor_id'])
if not current_floor.isActive():
current_floor.set_songlist(thread_holder.find_thread(current_floor.floor_id).songlist)
thread_holder.update_thread_status(current_floor.floor_id,current_floor.isActive())
def userEmit(member):
print(member.to_simple_list())
return {'authorized': 1,'email': member.member_email,'member_id':member.member_id,'user':member.to_simple_list()}
###################################################################################################################################
###################################################################################################################################
#############################PROFILE UPDATE HANDLERS###############################################################################
@socket.on('get floor profiles')
def get_floor_profiles(data):
floor = data['floor_id']
@socket.on(events.PROFILE_UPDATE)
def on_update_profile(data):
# dt = data[0]
print(data)
me = update_profile(**data)
print("updated user: ",me)
for t in thread_holder.threads:
fl = getFloor(t.floor_id)
fl_id_found = None
for m in fl.floor_members:
if m.member_id == data['member_id']:
print("found!")
fl_id_found = fl.floor_id
break
if fl_id_found is not None:
socket.emit('floor joined', {'floor':fl.to_list()}, room=request.sid)
fl_list = fl.to_list()
socket.emit('member list update', {'floor members': fl_list['floor_members']},room=fl.floor_id)
socket.emit(events.PROFILE_UPDATED,me)
@socket.on(events.PING)
def on_ping(data):
s_id = request.sid
socket.emit(events.PONG, "keep me alive",room= s_id)
#This event is for the privacy policy page
@app.route('/privacy')
def privacy():
return flask.render_template('privacy.html')
if __name__ == '__main__':
socket.run(
app,
port=int(os.getenv('PORT', '8080')),
host=os.getenv('IP', '0.0.0.0'),
debug=True)
# waht did you do yesterday?
# what did you do toda?
# what is the plan for today?
# roadblocks?
#on login: