-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.py
288 lines (260 loc) · 7.41 KB
/
play.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
#!/usr/bin/python2
from copy import copy
from Queue import Queue, Empty
from math import ceil, log
from mfind import get_tags, fwalk
from threading import RLock, Semaphore, Condition, Event
from sys import argv, stdin, stderr, stdout, exit
from collections import namedtuple, defaultdict, OrderedDict
from codecs import getwriter, getreader
from codecs import open as codecs_open
from contextlib import closing
argv = [x.decode('utf8') for x in argv]
stdin = getreader('utf8')(stdin)
stdout, stderr = [getwriter('utf8')(x) for x in stdout, stderr]
class Playlist(object):
methods = OrderedDict()
@classmethod
def generate(cls, maxcount, *dirs):
raise NotImplementedError
@classmethod
def add_method(cls, name, typ):
cls.methods[name] = typ
# Method 1
class bPlaylist(Playlist):
beginskip = 2 ** 10
midskip = 2 ** 15
def __init__(self, track0, ntracks):
self.__queue = Queue()
self.__queue.put(track0)
self.__tracks = [track0]
self.__tlock = Semaphore()
self.__handle = None
self.__flock = Semaphore()
self.__ntracks = ntracks
bitstep = long(ceil(log(ntracks, 2)))
self.__bitmask = 0
for i in xrange(bitstep):
self.__bitmask = (self.__bitmask << 1) | 1
self.__bytestep = long(ceil(float(bitstep) / 8))
def __del__(self):
with self.__flock:
if not self.__handle is None:
self.__handle.close()
self.__handle = None
def __next_track(self):
try:
if not self.__handle is None:
self.__handle.close()
self.__handle = open(self.__queue.get(block = False))
self.__handle.read(self.beginskip)
except Empty:
self.__handle = None
raise StopIteration
def next(self, step = None):
bstep = self.__bytestep if step is None else step
with self.__flock:
if self.__handle is None:
self.__next_track()
s = self.__handle.read(bstep)
while len(s) < bstep:
self.__next_track()
s = self.__handle.read(bstep)
ret = reduce(lambda acc, x: (acc << 8) | x, [ord(c) for c in s], 0)
if step is None:
ret &= self.__bitmask
ret %= self.__ntracks
return ret
def __len__(self):
with self.__tlock:
return len(self.__tracks)
@property
def tracks(self):
with self.__tlock:
return copy(self.__tracks)
def append(self, track):
with self.__tlock:
self.__queue.put(track)
self.__tracks.append(track)
#####
Track = namedtuple('Track', ['fname', 'tags'])
@classmethod
def get_track_tags(cls, files):
for t in files:
try:
tags = tuple([(key, tuple(value)) for key, value in get_tags(t)])
except ValueError:
continue
yield cls.Track(t, tags)
@staticmethod
def build_track_table(intracks):
tracks = defaultdict(list)
ntracks = len(intracks)
for t in intracks:
tracks[hash(t.tags) % ntracks].append(t.fname)
return tracks
@classmethod
def generate(cls, maxcount, *dirs):
intracks = list(cls.get_track_tags(fwalk(*dirs)))
tracks = cls.build_track_table(intracks)
ntracks = len(tracks)
track0 = choice(intracks).fname
del intracks
pls = cls(track0, ntracks)
seen = set([track0])
print >>stderr, 'Iteration started.'
while len(pls) < maxcount:
i = pls.next()
if not i in tracks:
continue
t = tracks[i]
i = 0
if len(t) > 1:
i = pls.next(1) % len(t)
if not t[i] in seen:
seen.add(t[i])
pls.append(t[i])
return iter(pls.tracks)
Playlist.add_method('original', bPlaylist)
# Method 2
class Tree(object):
class Node(object):
__slots__ = '__parent', '__left', '__right', '__key', '__value'
def __init__(self, key, value, parent = None):
self.__parent = parent
self.__left, self.__right = None, None
self.__key, self.__value = key, value
@property
def parent(self):
return self.__parent
@parent.setter
def parent(self, value):
self.__parent = value
@property
def left(self):
return self.__left
@left.setter
def left(self, value):
self.__left = value
@property
def right(self):
return self.__right
@right.setter
def right(self, value):
self.__right = value
@property
def key(self):
return self.__key
@property
def value(self):
return self.__value
def __iter__(self):
if self.__left is not None:
for x in self.__left:
yield x
yield self
if self.__right is not None:
for x in self.__right:
yield x
def __repr__(self):
return '<Node %s=%s>' % (repr(self.__key), repr(self.__value))
EMPTY_TREE, NODE_APPROXIMATE, NODE_EXACT = xrange(3)
__slots__ = '__root',
def __init__(self):
self.__root = None
def __repr__(self):
if self.__root is None:
return '<Tree 0>'
else:
return '<Tree %s>' % repr(self.__root)
def __get_node(self, key):
if self.__root is None:
return self.EMPTY_TREE, None
node = self.__root
while True:
if node.key == key:
return self.NODE_EXACT, node
elif key < node.key:
if node.left is None:
return self.NODE_APPROXIMATE, node
node = node.left
else:
if node.right is None:
return self.NODE_APPROXIMATE, node
node = node.right
raise RuntimeError
def __setitem__(self, key, value):
match, parent = self.__get_node(key)
if match is self.EMPTY_TREE:
self.__root = self.Node(key, value, None)
elif match is self.NODE_EXACT:
raise KeyError('Key already exists: %s' % key)
else:
node = self.Node(key, value, parent)
if node.key < parent.key:
if parent.left is not None:
raise RuntimeError
parent.left = node
else:
if parent.right is not None:
raise RuntimeError
parent.right = node
def __iter__(self):
if self.__root is not None:
for node in self.__root:
yield node.key, node.value
def get(self, key, approximate = False):
match, node = self.__get_node(key)
if match is self.NODE_EXACT:
return node.value
elif match is self.NODE_APPROXIMATE and approximate:
return node.value
else:
raise KeyError('No such key: %s' % key)
class trPlaylist(Playlist):
@classmethod
def generate(cls, maxcount, *dirs):
playlist = []
tree = Tree()
for f in fwalk(*dirs):
try:
tags = tuple([(key, tuple(value)) for key, value in get_tags(f)])
except ValueError:
continue
h = hash(tags)
while True:
try:
tree[h] = f
break
except KeyError:
h = h + 1 if h > 0 else h - 1
if not len(playlist):
playlist.append(f)
for i in xrange(maxcount - 1):
with open(playlist[-1], 'rb') as f:
while True:
key = hash(f.read(2048))
g = tree.get(key, True)
if g not in playlist:
playlist.append(g)
break
return iter(playlist)
Playlist.add_method('tree', trPlaylist)
if __name__ == '__main__':
from argparse import ArgumentParser
from random import choice
parser = ArgumentParser(description = 'Generate a playlist')
parser.add_argument('playlist', help = 'Playlist to generate (- for stdout)', action = 'store')
parser.add_argument('directories', metavar = 'dir', help = 'Directories to search', action = 'store', nargs = '+')
parser.add_argument('-n', '--count', dest = 'count', type = int, help = 'Maximum number of tracks in generated playlist.', default = 0)
parser.add_argument('-m', '--method', dest = 'method', help = 'Playlist generation method', choices = Playlist.methods.keys(), default = Playlist.methods.keys()[0])
args = parser.parse_args(argv[1:])
tracks = list(Playlist.methods[args.method].generate(args.count, *args.directories))
print >>stderr, 'Writing playlist file.'
if args.playlist == '-':
for t in tracks:
print >>stdout, t
else:
with closing(codecs_open(args.playlist, 'w', 'utf8')) as f:
for t in tracks:
print >>f, t