-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack_queue.py
More file actions
112 lines (89 loc) · 3.19 KB
/
track_queue.py
File metadata and controls
112 lines (89 loc) · 3.19 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
from track import *
from spotipy_utils import *
from spotifyClient import *
class Track_Queue:
queue = []
#create a track queue from a spotify playlist with id playlist_id
def __init__(self,playlist_id):
self.playlist_id = playlist_id
tracklist = get_playlist(playlist_id)['tracks']
queue = []
for i, item in enumerate(tracklist['items']):
track = item['track']
#print(" %d %32.32s %s %s" % (i, track['artists'][0]['name'],track['name'], track['id']))
queue.append(Track(track['name'], track['artists'][0]['name'], track['album']['name'], track['id'], track['album']['images'][2]['url']))
self.queue = queue
self.sort()
def get_playlist_id(self):
return self.playlist_id
#queue sorting by vote_count funciton
def sort(self):
swap = True
queue = self.queue
#Bubble Sort
while swap :
swap = False
for i in range(1, len(queue)-1):
if queue[i].get_votes() < queue[i+1].get_votes() :
aux = queue[i]
queue[i] = queue[i+1]
queue[i+1] = aux
swap = True
def export_to_spotify(self):
queue = self.queue
#clear playlist
remove_all_from_playlist(self.playlist_id)
#recreate the playlist
for track in queue:
add_to_playlist(track.getId(),self.playlist_id)
def get_track_by_id(self, id):
queue = self.queue
for track in queue:
if track.getId()==id:
return track
#add a track object
def push(self, track, old_start):
self.queue.append(track)
self.sort()
queue=self.queue
new_index = len(queue)-1
for i in range (len(queue)):
if queue[i].getId()==track.getId():
new_index = i
break
print(old_start)
print('pppppppppppppppppppppppppppppp')
print(new_index)
# print(len(queue))
if new_index < len(queue)-1:
reorder_track(old_start, new_index, self.playlist_id)
else:
# add last in playlist
remove_from_playlist(queue[new_index].getId(), self.playlist_id)
add_to_playlist(queue[new_index].getId(), self.playlist_id)
#update spotify track
#self.export_to_spotify()
#delete all occurences of song with id track_id
#returns the removed track object
def pop(self,track_id):
pop_track = None
cuurent_ind = 0
queue = self.queue
for i in range (len(queue)):
if queue[i].getId()==track_id:
pop_track = queue[i]
self.queue.remove(queue[i])
return (i, pop_track)
return (0, None)
# print track.getId()
#update spotify track
#self.export_to_spotify()
def as_list(self):
return self.queue
def get_most_wanted(self):
queue=self.queue
i_max = 1
for i in range(1, len(queue)):
if queue[i].get_votes() > queue[i_max].get_votes():
i_max = i
return queue[i_max]