-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBallotServer.py
172 lines (145 loc) · 5.28 KB
/
BallotServer.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
from PlaylistItem import PlaylistItem, JsonEncoder
from MPDClientWrapper import MPDClientWrapper
import mpd
import json
import threading
class BallotServer:
mpdHandle = None
votedPlaylist = None
""" Returns the playlist position of a file if found, else -1 """
def getIndexForFile(self, file):
pl = self.votedPlaylist
if file == None:
return -1
for i in range(0, len(pl)):
if pl[i].file == file:
return i
return -1
def mergeVotePlaylistIntodMpdPlaylist(self):
mpdPl = self.mpdHandle.playlistid()
pl = list()
for plItem in mpdPl:
artist = plItem.get('artist', "Unkown Artist")
title = plItem.get('title', plItem['file'])
file = plItem['file']
votes = 0
operatorPos = None
found = False
vitem = None
mpdId = int(plItem['id'])
if self.votedPlaylist:
for vitem in self.votedPlaylist:
if vitem.mpdId == mpdId:
found = True
break
# This element was in the list
if found:
votes = vitem.votes
operatorPos = vitem.playlistPosSetByOperator
pl.append(PlaylistItem(mpdId, votes, operatorPos, artist, title, file))
return pl
def updatePlaylist(self):
self.listLock.acquire()
self.votedPlaylist = self.mergeVotePlaylistIntodMpdPlaylist()
self.listLock.release()
'''
Create new playlist, incorporating operator changes
for item in mpdPl:
found = False
vitem = None
for lowerItem in mpdPl[mpdPl.index(item):]:
if vitem.votes < lowerItem.votes:
# Check whether it was inserted by the operator
'''
'''
Moves the given item from its current position to the one behind another
song that has equally or more votes.
If it does so, it returns the new position. If the position remains
unchanged, -1 is returned
If it does so, it returns the new position.
e.g.: (True, 2)
If the position remains unchanged, -1 is returned
(True, -1)
'''
def moveFromBottomAccordingToVotes(self, plItem):
pl = self.votedPlaylist
idx = pl.index(plItem)
aboveListReversed = pl[1:idx]
aboveListReversed.reverse()
if len(aboveListReversed) <= 1:
return (True, -1)
lessVotes = aboveListReversed[0]
for itemAbove in aboveListReversed:
if itemAbove.votes >= plItem.votes:
break
else:
lessVotes = itemAbove
idx2 = pl.index(lessVotes)
pl.insert(idx2, pl.pop(idx))
self.mpdHandle.moveid(plItem.mpdId, idx2)
print ("moving track from {0} to playlist position {1}".format(idx, idx2))
return (True, idx2)
'''
Moves an item in the playlist according to its votes.
If it does so, it returns the new position. If the position remains
unchanged, -1 is returned
If it does so, it returns the new position.
e.g.: (True, 2)
If the position remains unchanged, -1 is returned
(True, -1)
'''
def voteForMpdId(self, mpdId):
voteSuccess = (False, -1)
if self.votedPlaylist == None:
self.updatePlaylist()
pl = self.votedPlaylist
for track in pl:
if track.mpdId == mpdId:
track.votes += 1
voteSuccess = self.moveFromBottomAccordingToVotes(track)
break
return voteSuccess
def getLsInfo(self, path):
return self.mpdHandle.lsinfo(path)
def getPlaylist(self):
return self.votedPlaylist
def getPlaylistAsJson(self):
return json.dumps(self.votedPlaylist, cls=JsonEncoder)
#return json.dumps(self.votedPlaylist.__dict__)
def getLibrary(self):
return self.mpdHandle.listallinfo()
def getLibraryAsJson(self):
return json.dumps(self.getLibrary(), cls=JsonEncoder)
""" return value: (bool, int) = (SongQueued, PlaylistPosition) """
def queueSong(self, path):
# maybe it's already queued
index = self.getIndexForFile(path)
if index != -1:
return (False, index + 1)
# try to add it and get the index afterwards
try:
self.mpdHandle.add(path)
self.updatePlaylist()
except mpd.CommandError:
(False, -1)
return (True, self.getIndexForFile(path) + 1)
def searchFile(self, query):
return self.mpdHandle.search("file", query)
def setupTimer(self):
self.updatePlaylist()
if not self.timerStop:
self.timer = threading.Timer(5, self.setupTimer)
self.timer.start()
def __init__(self, host, port):
# Connect to mpd
print ("Connecting to MPD on {0}:{1}".format(host, port))
self.mpdHandle = MPDClientWrapper(host, port)
self.mpdHandle.consume(1)
self.listLock = threading.Lock()
print ("Updating playlist".format())
self.updatePlaylist()
self.timerStop = False
self.setupTimer()
def close(self):
self.timerStop = True
self.timer.cancel()