/
pocket_juke_endpoints.py
396 lines (317 loc) · 15.5 KB
/
pocket_juke_endpoints.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
import endpoints
import os
import urllib
import operator
import endpoints
from operator import itemgetter
from protorpc import messages
from google.appengine.api import users
from protorpc import message_types
from protorpc import remote
from google.appengine.api import oauth
from operator import itemgetter
from google.appengine.ext import ndb
WEB_CLIENT_ID = '490877078433-t9spjdkqmkqe8c3c3jbgrgot9lrhgu86.apps.googleusercontent.com'
ANDROID_CLIENT_ID = 'replace this with your Android client ID'
IOS_CLIENT_ID = 'replace this with your iOS client ID'
ANDROID_AUDIENCE = WEB_CLIENT_ID
package = 'pocket_juke'
#thsi represents the entitiy for a user class
#NDB data type user
class User(ndb.Model):
"""Sub Model representing the user"""
user_id = ndb.StringProperty(indexed=True)
party_key = ndb.StringProperty(indexed=True)
#NDB data type party
class Party_(ndb.Model):
"""main model to represent a party"""
party_creator = ndb.KeyProperty(User,indexed=True)
name = ndb.StringProperty(indexed=True)
code = ndb.StringProperty(indexed=False)
attending = ndb.IntegerProperty(indexed=False)
#NDB datatype for a song
class Song(ndb.Model):
track_id = ndb.StringProperty(indexed=True)
user_suggest = ndb.KeyProperty(indexed=True)
party_key = ndb.KeyProperty(Party_,indexed=True)
played = ndb.BooleanProperty()
#NDB datatype for an activity
class Activity(ndb.Model):
song = ndb.KeyProperty(Song,indexed=True)
party_key = ndb.KeyProperty(Party_,indexed=True)
user_vote = ndb.KeyProperty(User,indexed=True)
time_stamp = ndb.DateTimeProperty(auto_now_add=True)
#this represents the entity for housing the party name and code
class Party_class(messages.Message):
"""Greeting that stores a message."""
name = messages.StringField(1)
code = messages.StringField(2)
#this represents the entity for housing the Activity_Class
class Activity_class(messages.Message):
track_id = messages.StringField(1)
class Song_class(messages.Message):
track_id = messages.StringField(2)
#this holsd the entity that is used for the response for successful/not succesful additcion of a party
class add_response(messages.Message):
response = messages.StringField(1,required=True)
#this hold the list of parties that will be passed back to the user when the query for a name of a party is successful
class Party_list(messages.Message):
"""Collection of Parties."""
Parties = messages.MessageField(Party_class,1,repeated=True)
class Party_info(messages.Message):
Activity_list = messages.MessageField(Activity_class,1,repeated=True)
attending = messages.IntegerField(2)
party_name = messages.StringField(3)
"""Pocket Juke API using Google App Engine Cloud Endpoints"""
@endpoints.api(name='pocketjuke', version='v1',
allowed_client_ids=[WEB_CLIENT_ID, ANDROID_CLIENT_ID,
IOS_CLIENT_ID, endpoints.API_EXPLORER_CLIENT_ID],
audiences=[ANDROID_AUDIENCE],
scopes=[endpoints.EMAIL_SCOPE])
class PocketJukeAPI(remote.Service):
"""Pocket Juke API v1."""
@endpoints.method(add_response,Party_info,
path='authed_partyInfo',http_method='GET',
name='pocketjuke.getpartyinfoAuthed')
def get_partyInfo(self,request):
party_query = Party_.query(Party_.name == request.response)
active_user = endpoints.get_current_user();
playlist_queue = []
if party_query.get():
#request_party = party_query.get()
#user = User.query(User.user_id == active_user.user_id())
#if party_query.get(keys_only=True) == user.party_key:
activity_query = Activity.query(Activity.party_key == party_query.get(keys_only=True)).order(Activity.song,Activity.time_stamp)
if activity_query.get():
#we need to get all of the activities so we can build the queue
#we dont need to sort the response, because the response is sorted already
#set up our constant values
#One of them is the maximum time between votes to be considered consecutive
max_time_apart = 5 #this is in seconds
#this one is for the time frame before the end of the current song
max_before_start = 2000 #this is in miliseconds
#this is the threshold minimum number of consecutive songs
min_consect_songs = 3
#variable to keep track of the total number of consecutive songs
consect_songs = 0
#for the consecutive songs the buff is .1 %
consect_buff = 1.001
#buff for a standard vote
vote_buff = 1.0005
#for the before the end of the current song buff its .5%
before_buff = 1.005
queue = []
pre_entry = None
def close_time(date1,date2):
if(date1.year == date2.year):
if(date1.month == date2.month):
if(date1.day == date2.day):
if(date1.hour == date2.hour):
if(date1.minute == date2.minute):
if(date1.second - date2.second < 5):
return True
return False
for entry in activity_query:
if not any(d['track_id'] == entry.song.get().track_id for d in queue):
consect_songs = 0
new_activity = Activity_class(track_id = entry.song.get().track_id)
song_pos = {
'weight' : 1,
'track_id': new_activity.track_id,
}
queue.append(song_pos)
pre_entry = entry
else:
pos = map(itemgetter('track_id'),queue).index(entry.song.get().track_id)
if consect_songs > min_consect_songs:
weight = queue[pos]['weight']
queue[pos]["weight"] = weight * consect_buff
else:
weight = queue[pos]['weight']
queue[pos]["weight"] = weight * vote_buff
if close_time(pre_entry.time_stamp,entry.time_stamp):
consect_songs += 1
else:
consect_songs = 0
#will add this back later
#if entry.date.seconds() - 500000 < max_before_start:
# queue[pos]['weight'] *= before_buff
pre_entry = entry
sorted_queue = sorted(queue, key=operator.itemgetter('weight'),reverse=True)
#add the marked song that is in the active position
song = Song.query(ndb.AND(Song.party_key == party_query.get(keys_only=True),Song.played == True))
if song.get():
active_song = song.get()
playlist_queue.append(Activity_class(track_id = active_song.track_id));
else:
active_song = Activity_class(track_id = '0')
for activity in sorted_queue:
if not activity['track_id'] == active_song.track_id:
playlist_queue.append(Activity_class(track_id = activity['track_id']))
party = party_query.get()
return Party_info(Activity_list = playlist_queue,attending = party.attending)
else:
party = party_query.get()
playlist_queue.append(Activity_class(track_id='1'))
return Party_info(Activity_list = playlist_queue,attending = party.attending)
else:
playlist_queue.append(Activity_class(track_id='2'))
return Party_info(Activity_list = playlist_queue)
@endpoints.method(Party_class,add_response,
path='authed_addParty',http_method='PUT',
name='pocketjuke.addPartyAuthed')
def add_Party(self,request):
party_query = Party_.query(Party_.name == request.name)
if not party_query.get():
current_user = endpoints.get_current_user()
user_query = User.query(User.user_id == current_user.user_id())
if user_query:
user = user_query.get()
new_party = Party_(name = request.name,party_creator = user_query.get(keys_only=True),code = request.code,attending =+1)
user.party_key = new_party.put()
user.put()
return add_response(response='added party')
else:
return add_response(response="unable to add party")
else:
return add_response(response="party already exsists")
@endpoints.method(message_types.VoidMessage,add_response,
path='authed_addUser',http_method='POST',
name='pocketjuke.addUserAuthed')
def add_User(self,request):
active_user = endpoints.get_current_user()
user_query = User.query(User.user_id == active_user.user_id())
if user_query.get():
return add_response(response = 'User already in the database')
else:
new_user = User(user_id = active_user.user_id(),party_key = None)
new_user.put()
return add_response(response="user added to the database")
QUANTITY = endpoints.ResourceContainer(
Party_class,offset=messages.IntegerField(4, variant=messages.Variant.INT32))
@endpoints.method(QUANTITY,Party_list,
path='authed_getParty',http_method='GET',
name='pocketjuke.getPartysAuthed')
def get_parties(self,request):
current_user = endpoints.get_current_user()
keywords = []
party_list = []
keywords.append(request.name)
party_query = Party_.query(Party_.name.IN(keywords))
if party_query.get():
if request.offset > 10:
for party in party_query.fetch(10,offset=request.offset):
party_list.append(Party_class(name= request.party_name))
return Party_list(Parties = party_list)
#if we are only pulling the first ten Parties
else:
for party in party_query.fetch(10):
party_list.append(Party_class(name= party.name))
count = len(party_list)
return Party_list(Parties = party_list)
else:
party_list = []
party_list.append(Party_class(name = 'None', pass_code = 'None'))
count = 0
return Party_list(Parties = party_list)
@endpoints.method(Song_class,add_response,
path='authed_play',http_method='POST',
name='pocketjuke.playAuthed')
def play_song(self,request):
"""Set Play song Flag"""
current_user = endpoints.get_current_user()
user_query = User.query(User.user_id == current_user.user_id())
user = user_query.get()
song_query = Song.query(ndb.AND(Song.track_id == request.track_id,Song.party_key == user.party_key))
if song_query.get():
song = song_query.get()
if not song.played:
song.played = True;
song.put()
#delete all the activities for that song in that party
activity_query = Activity.query(Activity.song == song.key,Activity.party_key == user.party_key).fetch(keys_only=True)
ndb.delete_multi(activity_query)
return add_response(response = 'Song has been updated')
else:
return add_response(response = 'song not found')
@endpoints.method(Song_class,add_response,
path='authed_stop',http_method='POST',
name='pocketjuke.stopAuthed')
def stop_song(self,request):
"""Unmarks the palying song so it can be voted on"""
current_user = endpoints.get_current_user()
user_query = User.query(User.user_id == current_user.user_id())
user = user_query.get()
song_query = Song.query(ndb.AND(Song.track_id == request.track_id,Song.party_key == user.party_key))
if song_query.get():
song = song_query.get()
if song.played:
song.played = False;
song.put()
return add_response(response = 'Song has been updated')
else:
return add_response(response = 'song not found')
@endpoints.method(message_types.VoidMessage,add_response,
path='auted_leaveParty',http_method='GET',
name='pocketjuke.leavePartyAuthed')
def leave_Party(self,request):
current_user = endpoints.get_current_user()
active_user = User.query(User.user_id == current_user.user_id())
user = active_user.get()
party = user.party_key.get()
party.attending = party.attending - 1
party.put()
user.party_key = None
user.put()
return add_response(response="Removed from the party")
@endpoints.method(Party_class,add_response,
path='authed_joinParty',http_method='POST',
name='pocketjuke.joinPartyAuthed')
def join_Party(self,response):
current_user = endpoints.get_current_user()
keywords = []
keywords.append(response.name)
party_query = Party_.query(Party_.name.IN(keywords))
if party_query.get():
user_query = User.query(User.user_id == current_user.user_id())
user = user_query.get()
party = party_query.get()
#party.attending = party.attending + 1
#party_query = Party_.query(Party_.name.IN(keywords))
user.party_key = party.put()
user.put()
return add_response(response="Joined party")
else:
return add_response(response="Unable to Join Party")
@endpoints.method(Song_class,add_response,
path='authed_addSong',http_method='POST',
name='pocketjuke.addSongAuthed')
def add_song(self,request):
current_user = endpoints.get_current_user()
user_query = User.query(User.user_id == current_user.user_id())
user = user_query.get()
party_query = user.party_key.get()
song_query = Song.query(ndb.AND(Song.track_id == request.track_id,Song.party_key == user.party_key))
if not song_query.get():
new_song = Song(track_id = request.track_id, user_suggest = user_query.get(keys_only=True),party_key = user.party_key,played = False)
new_song.put()
return add_response(response='Added song as a suggestion')
else:
new_activity = Activity(song=song_query.get(keys_only=True),party_key=user.party_key,user_vote=user_query.get(keys_only=True))
new_activity.put()
return add_response(response='Song already added as suggestion will consider this as a vote')
@endpoints.method(Song_class,add_response,
path='authed_voteSong',http_method="POST",
name='pocketjuke.voteSongAuthed')
def vote_song(self,request):
current_user = endpoints.get_current_user()
user_query = User.query(User.user_id == current_user.user_id())
user = user_query.get()
song_query = Song.query(ndb.AND(Song.track_id == request.track_id,Song.party_key == user.party_key))
if song_query.get():
new_activity = Activity(song=song_query.get(keys_only=True),party_key = user.party_key,user_vote=user_query.get(keys_only=True))
new_activity.put()
return add_response(response='Added vote to activity list')
else:
return add_response(response = 'Could not added vote to activity list')
APPLICATION = endpoints.api_server([PocketJukeAPI])