-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroom.py
More file actions
227 lines (197 loc) · 6.94 KB
/
room.py
File metadata and controls
227 lines (197 loc) · 6.94 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
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
from flask import request
class Room:
def __init__(self, sp_oauth, Recs=False):
scope = "user-modify-playback-state user-read-currently-playing user-read-playback-state"
client_id='8f8a8980909d4e10a27e9e4e7ac702f5'
client_secret='d42dc7c04b4e43a19c852336484304a4'
redirect_uri='http://localhost:8888'
self.sp_oauth = sp_oauth
self.local_queue = []
self.just_added = None
self.len = 0
self.current_song = None
self.dummy = None
self.Recs = Recs
self.users = 1
self.lastKicked = False
self.getCurrentlyPlaying()
def getSp(self):
token_info = self.sp_oauth.get_cached_token()
access_token = token_info['access_token']
return spotipy.Spotify(auth=access_token)
def getCurrentlyPlaying(self):
sp = self.getSp()
data = sp.current_user_playing_track()
if data:
temp = data['item']
self.current_song = {'name':temp['name'], 'id':temp['id'], 'artist':temp['artists'][0]['name']}
if data:
name, artist, album_art = self.get_track_data(data['item']['id'])
progress_ms = data['progress_ms']/1000
duration = data['item']['duration_ms']/1000
self.current_song['art'] = album_art
return name, artist, album_art, progress_ms, duration
return
def get_track_data(self,trackid):
sp = self.getSp()
try:
data = sp.track(trackid)
name = data['name']
artist = data['artists'][0]['name']
album_art = data['album']['images'][0]['url']
return name, artist, album_art
except:
return
def getSong(self, query, limit = 3): #takes in query, outputs top 3 relevant serches in an array
sp = self.getSp()
B = sp.search(query, limit = limit, type = 'track')
l = len(B['tracks']['items'])
A = B['tracks']['items'][0:l]
stripped = []
for a in A:
id = a['id']
art = sp.track(id)['album']['images'][0]['url']
stripped.append({'id':a['id'] , 'name':a['name'], 'artist':a['artists'][0]['name'], 'art':art})
return stripped
def stripRecs(self, B):
l = len(B['tracks'])
A = B['tracks'][0:l]
stripped = []
for a in A:
stripped.append({'id':a['id'] , 'name':a['name'], 'artist':a['artists'][0]['name']})
return stripped[0:3]
def isEmpty(self):
return len(self.local_queue) == 0
def addtoQueue(self, song):
song['upvotes'] = 1
song['downvotes'] = 0
song['votes'] = 1
if not self.isEmpty():
if(self.local_queue[-1]['votes'] >0):
self.local_queue.append(song)
else:
left,right=0,self.len
while (left < right):
mid = left + (right - left)//2
if self.local_queue[mid]['votes'] <= 1:
right = mid
else:
left = mid + 1
self.local_queue.insert(left, song)
else:
self.local_queue.append(song)
self.len+=1
# if self.len > 2 and self.Recs: #RECOMMENDATION STUFF
# return self.stripRecs(self.sp.recommendations(seed_tracks = self.getCurrentQueueIds()[0:min(3,self.len)]))
return None
def popfromQueue(self): #Want to Ping client after we use this so they know to get the current song
sp = self.getSp()
song = self.local_queue.pop(0)
sp.add_to_queue(song['id'])
# self.just_added = song['id']
self.dummy = song
self.len-=1
def getCurrentQueue(self):
return self.local_queue
def getCurrentQueueIds(self):
ids = []
for a in self.local_queue:
ids.append(a['id'])
return ids
def getCurrentSong(self):
return self.current_song
def nextSong(self): #Top of the Queue
if(self.len == 0):
return None
return self.local_queue[0]
def addVote(self, songid, dir): #Keep sorted directly after adding votes, PING CLIENT IF RETURNS 1
Q = self.local_queue
x = 0
for x in range(self.len):
if(Q[x]['id'] == songid):
Q[x]['upvotes']+=dir
Q[x]['votes']+=dir
break;
y = x-1
while(y >= -1):
if y == -1 or Q[x]['votes'] <= Q[y]['votes']:
Q.insert(y+1,Q.pop(x))
break;
y-=1
if y != x-1:
return 1
return -1
def removeVote(self, songid, dir): #Keep sorted directly after removing votes, PING CLIENT IF RETURNS 1
Q = self.local_queue
x = 0
for x in range(len(Q)):
if(Q[x]['id'] == songid):
Q[x]['downvotes']+=dir
Q[x]['votes']+=dir
break;
y = x+1
while(y <= len(Q)):
if y == len(Q) or Q[x]['votes'] >= Q[y]['votes']:
Q.insert(y-1,Q.pop(x))
break;
y+=1
if(Q[x]['votes'] < self.cutoff()):
self.kickLast() #Figure out pings later luLULULULULULUL
if y != x+1:
return 1
return -1
def updateVote(self,songid, dir):
if(dir < 0):
return self.removeVote(songid, dir) #HERE PING CLIENT??
else:
return self.addVote(songid) #HERE PING CLIENT??
def someFunction(self):
data = self.getCurrentlyPlaying()
if data:
if (data[4] - data[3] < 20 and self.just_added == self.dummy):
self.popfromQueue()
elif(data[3] < 10 and self.just_added != self.dummy):
self.just_added = self.dummy
else:
return
elif not self.len == 0:
self.popfromQueue()
def testFunc(self):
for i in range(10):
self.someFunction()
time.sleep(5)
def skipSong(self):
sp = self.getSp()
sp.next_track()
def pause(self):
sp = self.getSp()
sp.pause_playback()
def play(self, device = None):
sp = self.getSp()
sp.start_playback(device)
def getDevice(self):
sp = self.getSp()
return sp.devices()
def clearQueue(self):
self.len = 0
self.local_queue = []
self.just_added = None
def pushQueue(self):
for x in range(self.len):
self.popfromQueue()
def addUser(self):
self.users += 1
def cutoff(self):
return -self.users/2
def kickLast(self): #Also Ping? How to do this?
return
# self.local_queue.pop()
# self.len-=1
def sendDanAll(self):
data = {}
data['queue'] = self.local_queue
data['song'] = self.current_song
return data