-
Notifications
You must be signed in to change notification settings - Fork 1
/
musiclib.py
378 lines (281 loc) · 11.4 KB
/
musiclib.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
'''
Music utilities, provides the core classes needed to implement a simple music server,
possibly including playlist handling.
Created on Apr 13, 2015
@author: bonino
'''
from mutagen import flac, mp3
from subprocess import Popen, PIPE
import os
#----- Classes -------------
class Track:
'''
A class representing a music file
'''
def __init__(self, path=None):
# the track id
self.id = None
# the file to which the track points (full path)
self.path = path
# extract the track description from file tags
self.data = self.metadata()
def metadata(self):
track_data = {}
# default name
track_data['title'] = self.path[:self.path.rfind('.')]
track_data['album'] = None
track_data['genre'] = None
track_data['artist'] = None
# detect the current file type
file_type = self.path[self.path.rfind('.'):]
# handle FLAC files
if file_type == ".flac":
metadata = flac.FLAC(self.path)
# print metadata
try:
track_data['title'] = metadata['title'][0]
track_data['album'] = metadata['album'][0]
track_data['genre'] = metadata['genre'][0]
track_data['artist'] = metadata['artist'][0]
except:
pass
# handle MP3 files
if file_type == ".mp3":
metadata = mp3.MP3(self.path)
# print metadata
try:
if(metadata.has_key('TIT2')):
track_data['title'] = metadata['TIT2'].text[0]
if(metadata.has_key('TALB')):
track_data['album'] = metadata['TALB'].text[0]
if(metadata.has_key('TCON')):
track_data['genre'] = metadata['TCON'].text[0]
if(metadata.has_key('TPE2')):
track_data['artist'] = metadata['TPE2'].text[0]
except:
pass
return track_data
def jsonifiable(self):
track = {}
track['id'] = self.id
track['path'] = self.path
track['metadata'] = self.data
return track
class TrackList:
'''
A class representing a "list" of music files
'''
def __init__(self):
# the list
self.tracks = []
# the last index
self.lastId = 0
def scan(self, root_folder):
'''
Walks the given directory to find mp3 and flac music files
'''
# the tracks in the given (root) folder...
tracks_in_folder = []
# track counter
i = 0
# iterate over subdirectories, following symlinks
for directory in os.walk(root_folder, followlinks=True):
# for each file in the current directory
for filename in directory[2]:
# check the file extension
if filename.endswith('.mp3') or filename.endswith('.flac'):
# set the track file path
current_track = Track(os.path.join(directory[0], filename))
# debug
print (current_track.path)
# set the track id, not unique:may change between different runs
current_track.id = i;
# append the current track
tracks_in_folder.append(current_track)
# increment the track id
i += 1
return tracks_in_folder
def add_tracks(self, tracks, auto_id=True):
'''
Adds the given (set) of tracks.
'''
for track in tracks:
# reset the track id
if(auto_id):
track.id = self.lastId
self.lastId += 1
# appedn the track
self.tracks.append(track)
def get_track(self, track_id):
# the track to return
track = None
# get the track info
found_tracks = [trackz for trackz in self.tracks if trackz.id == track_id]
# return the track info
if(len(found_tracks) > 0):
track = found_tracks[0]
return track
def search(self, filter_parameters):
'''
Searches for tracks having / containing the given value in the given tag
'''
# the list of matching tracks
matching_tracks = []
# check if parameters are available
if filter_parameters != None:
# iterate over all available tracks
for track in self.tracks:
try:
#implement AND
matching_params = 0
for param in filter_parameters:
# the track param
track_field = track.data[param]
# apply filter
if (track_field != None) and ((track_field.lower() == filter_parameters[param].lower()) or (track_field.lower().find(filter_parameters[param].lower()) > -1)):
matching_params = matching_params+1
if(matching_params == len(filter_parameters)) and (len([trackz for trackz in matching_tracks if trackz.id == track.id]) == 0):
# if the filter matches, add the track
matching_tracks.append(track)
except:
#skip silently
pass
# return the set of matching tracks
return matching_tracks
class Album:
def __init__(self, artist = None, title = None, tracks = None):
#the album id
self.id = 0
#the artist
self.artist = artist
#title
self.title = title
#tracks
self.track_list = TrackList()
def add_tracks(self,tracks):
# append the track
self.track_list.add_tracks(tracks, False)
def jsonifiable(self):
#prepare the album dictionary
album = {}
#the id
album['id'] = self.id
# the artist
album['artist'] = self.artist
# the title
album['title'] = self.title
# the list of tracks
album['tracks'] = [track.jsonifiable() for track in self.track_list.tracks]
return album
class AlbumList:
def __init__(self):
#the first free id
self.last_id = 0;
#the album array
self.albums = []
def from_tracklist(self, tracklist):
#check not None
if (tracklist!= None):
for track in tracklist:
#check if the album already exist
album = self.get_album_by_name(track.data['album'])
if(album == None):
#create the album
album = Album(title = track.data['album'], artist = track.data['artist'])
#set the album id
album.id = self.last_id
self.last_id = self.last_id+1
#add the album
self.albums.append(album)
#add the track
album.add_tracks({track})
def get_album_by_name(self, name):
#the actual album to return
album = None
#get the album having the given name
found_albums = [albumz for albumz in self.albums if albumz.title == name]
#if at least one album matches, get the first
if(len(found_albums)>0):
album = found_albums[0]
#return the album found
return album
def get_album(self, album_id):
#the album to return
album = None
#get the album having the given id
found_albums = [albumz for albumz in self.albums if albumz.id == album_id]
#if at least one album matches get the first
if(len(found_albums)>0):
album = found_albums[0]
#return the album found
return album
class Player:
def __init__(self):
# the tracklist
self.current_playlist = []
# the currently playing list
self.currently_playing = None
self.currently_playing_id = -1
# the player status
self.status = "stopped"
# build the player
self.player = Popen("mplayer -slave -quiet -nolirc -msglevel all=-1 -idle", stdin=PIPE, stdout=PIPE, shell=True)
def load_and_play(self, track):
# clear any playlist
self.current_playlist = []
# play the file
self.player.stdin.write("loadfile \"%s\"\n" % track.path)
# set the currently playing track
self.currently_playing = track
self.currently_playing_id = -1
# update the player status
self.status = "playing"
def next(self):
# skip to next track
self.player.stdin.write("pt_step 1\n")
# handle queue composition
if(len(self.current_playlist) > 0):
# the next id
next_id = self.currently_playing_id + 1
if(len(self.current_playlist) > next_id):
# update the current track
self.currently_playing = self.current_playlist[next_id]
self.currently_playing_id = next_id
else:
# stop playing
self.stop()
# reset playlist
self.currently_playing_id = 0
self.currently_playing = self.current_playlist[self.currently_playing_id]
# update the player status
self.status = "stopped"
def stop(self):
# stop playing
self.player.stdin.write("stop\n")
# update the player status
self.status = "stopped"
def exit(self):
# quit the player
self.player.stdin.write("quit\n")
def load_playlist(self, tracks):
# initialize the playlist track counter
i = 0
# initialize the play list
self.current_playlist = []
for track in tracks:
# add the track to the current playlist
self.current_playlist.append(track)
# start playing the first track
if(i == 0):
self.player.stdin.write("loadfile \"%s\"\n" % track.path)
# update the inner status
self.status = "playing"
self.currently_playing = track
self.currently_playing_id = 0
# update the player status
self.status = "playing"
else:
# enqueue
self.player.stdin.write("loadfile \"%s\" %s\n" % (track.path, i))
# increment track counter
i += 1