This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
playlist_container.py
576 lines (467 loc) · 20.8 KB
/
playlist_container.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
from __future__ import unicode_literals
import collections
import logging
import pprint
import re
import spotify
from spotify import ffi, lib, serialized, utils
__all__ = [
'PlaylistContainer',
'PlaylistContainerEvent',
'PlaylistFolder',
'PlaylistPlaceholder',
'PlaylistType',
]
logger = logging.getLogger(__name__)
class PlaylistContainer(collections.MutableSequence, utils.EventEmitter):
"""A Spotify playlist container.
The playlist container can be accessed as a regular Python collection to
work with the playlists::
>>> import spotify
>>> session = spotify.Session()
# Login, etc.
>>> container = session.playlist_container
>>> container.is_loaded
False
>>> container.load()
[Playlist(u'spotify:user:jodal:playlist:6xkJysqhkj9uwufFbUb8sP'),
Playlist(u'spotify:user:jodal:playlist:0agJjPcOhHnstLIQunJHxo'),
PlaylistFolder(id=8027491506140518932L, name=u'Shared playlists',
type=<PlaylistType.START_FOLDER: 1>),
Playlist(u'spotify:user:p3.no:playlist:7DkMndS2KNVQuf2fOpMt10'),
PlaylistFolder(id=8027491506140518932L, name=u'',
type=<PlaylistType.END_FOLDER: 2>)]
>>> container[0]
Playlist(u'spotify:user:jodal:playlist:6xkJysqhkj9uwufFbUb8sP')
As you can see, a playlist container can contain a mix of
:class:`~spotify.Playlist` and :class:`~spotify.PlaylistFolder` objects.
The container supports operations that changes the container as well.
To add a playlist you can use :meth:`append` or :meth:`insert` with either
the name of a new playlist or an existing playlist object. For example::
>>> playlist = session.get_playlist(
... 'spotify:user:fiat500c:playlist:54k50VZdvtnIPt4d8RBCmZ')
>>> container.insert(3, playlist)
>>> container.append('New empty playlist')
To remove a playlist or folder you can use :meth:`remove_playlist`, or::
>>> del container[0]
To replace an existing playlist or folder with a new empty playlist with
the given name you can use :meth:`remove_playlist` and
:meth:`add_new_playlist`, or::
>>> container[0] = 'My other new empty playlist'
To replace an existing playlist or folder with an existing playlist you can
:use :meth:`remove_playlist` and :meth:`add_playlist`, or::
>>> container[0] = playlist
"""
@classmethod
@serialized
def _cached(cls, session, sp_playlistcontainer, add_ref=True):
"""
Get :class:`PlaylistContainer` instance for the given
``sp_playlistcontainer``. If it already exists, it is retrieved from
cache.
Internal method.
"""
if sp_playlistcontainer in session._cache:
return session._cache[sp_playlistcontainer]
playlist_container = PlaylistContainer(
session,
sp_playlistcontainer=sp_playlistcontainer, add_ref=add_ref)
session._cache[sp_playlistcontainer] = playlist_container
return playlist_container
def __init__(self, session, sp_playlistcontainer, add_ref=True):
super(PlaylistContainer, self).__init__()
self._session = session
if add_ref:
lib.sp_playlistcontainer_add_ref(sp_playlistcontainer)
self._sp_playlistcontainer = ffi.gc(
sp_playlistcontainer, lib.sp_playlistcontainer_release)
self._sp_playlistcontainer_callbacks = None
# Make sure we remove callbacks in __del__() using the same lib as we
# added callbacks with.
self._lib = lib
def __del__(self):
if not hasattr(self, '_lib'):
return
if getattr(self, '_sp_playlistcontainer_callbacks', None) is None:
return
self._lib.sp_playlistcontainer_remove_callbacks(
self._sp_playlistcontainer, self._sp_playlistcontainer_callbacks,
ffi.NULL)
def __repr__(self):
return 'PlaylistContainer(%s)' % pprint.pformat(list(self))
def __eq__(self, other):
if isinstance(other, self.__class__):
return self._sp_playlistcontainer == other._sp_playlistcontainer
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self._sp_playlistcontainer)
@property
def is_loaded(self):
"""Whether the playlist container's data is loaded."""
return bool(lib.sp_playlistcontainer_is_loaded(
self._sp_playlistcontainer))
def load(self, timeout=None):
"""Block until the playlist container's data is loaded.
After ``timeout`` seconds with no results :exc:`~spotify.Timeout` is
raised. If ``timeout`` is :class:`None` the default timeout is used.
The method returns ``self`` to allow for chaining of calls.
"""
return utils.load(self._session, self, timeout=timeout)
def __len__(self):
# Required by collections.Sequence
length = lib.sp_playlistcontainer_num_playlists(
self._sp_playlistcontainer)
if length == -1:
return 0
return length
@serialized
def __getitem__(self, key):
# Required by collections.Sequence
if isinstance(key, slice):
return list(self).__getitem__(key)
if not isinstance(key, int):
raise TypeError(
'list indices must be int or slice, not %s' %
key.__class__.__name__)
if key < 0:
key += self.__len__()
if not 0 <= key < self.__len__():
raise IndexError('list index out of range')
playlist_type = PlaylistType(lib.sp_playlistcontainer_playlist_type(
self._sp_playlistcontainer, key))
if playlist_type is PlaylistType.PLAYLIST:
sp_playlist = lib.sp_playlistcontainer_playlist(
self._sp_playlistcontainer, key)
return spotify.Playlist._cached(
self._session, sp_playlist, add_ref=True)
elif playlist_type in (
PlaylistType.START_FOLDER, PlaylistType.END_FOLDER):
return PlaylistFolder(
id=lib.sp_playlistcontainer_playlist_folder_id(
self._sp_playlistcontainer, key),
name=utils.get_with_fixed_buffer(
100,
lib.sp_playlistcontainer_playlist_folder_name,
self._sp_playlistcontainer, key),
type=playlist_type)
elif playlist_type is PlaylistType.PLACEHOLDER:
return PlaylistPlaceholder()
else:
raise spotify.Error('Unknown playlist type: %r' % playlist_type)
def __setitem__(self, key, value):
# Required by collections.MutableSequence
if not isinstance(key, (int, slice)):
raise TypeError(
'list indices must be int or slice, not %s' %
key.__class__.__name__)
if isinstance(key, slice):
if not isinstance(value, collections.Iterable):
raise TypeError('can only assign an iterable')
if isinstance(key, int):
if not 0 <= key < self.__len__():
raise IndexError('list index out of range')
key = slice(key, key + 1)
value = [value]
# In case playlist creation fails, we create before we remove any
# playlists.
for i, val in enumerate(value, key.start):
if isinstance(val, spotify.Playlist):
self.add_playlist(val, index=i)
else:
self.add_new_playlist(val, index=i)
# Adjust for the new playlist at index key.start.
key = slice(key.start + len(value), key.stop + len(value), key.step)
del self[key]
def __delitem__(self, key):
# Required by collections.MutableSequence
if isinstance(key, slice):
start, stop, step = key.indices(self.__len__())
indexes = range(start, stop, step)
for i in reversed(sorted(indexes)):
self.remove_playlist(i)
return
if not isinstance(key, int):
raise TypeError(
'list indices must be int or slice, not %s' %
key.__class__.__name__)
if not 0 <= key < self.__len__():
raise IndexError('list index out of range')
self.remove_playlist(key)
@serialized
def add_new_playlist(self, name, index=None):
"""Add an empty playlist with ``name`` at the given ``index``.
The playlist name must not be space-only or longer than 255 chars.
If the ``index`` isn't specified, the new playlist is added at the end
of the container.
Returns the new playlist.
"""
self._validate_name(name)
sp_playlist = lib.sp_playlistcontainer_add_new_playlist(
self._sp_playlistcontainer, utils.to_char(name))
if sp_playlist == ffi.NULL:
raise spotify.Error('Playlist creation failed')
playlist = spotify.Playlist._cached(
self._session, sp_playlist, add_ref=True)
if index is not None:
self.move_playlist(self.__len__() - 1, index)
return playlist
@serialized
def add_playlist(self, playlist, index=None):
"""Add an existing ``playlist`` to the playlist container at the given
``index``.
The playlist can either be a :class:`~spotify.Playlist`, or a
:class:`~spotify.Link` linking to a playlist.
If the ``index`` isn't specified, the playlist is added at the end of
the container.
Returns the added playlist, or :class:`None` if the playlist already
existed in the container. If the playlist already exists, it will not
be moved to the given ``index``.
"""
if isinstance(playlist, spotify.Link):
link = playlist
elif isinstance(playlist, spotify.Playlist):
link = playlist.link
else:
raise TypeError(
'Argument must be Link or Playlist, got %s' % type(playlist))
sp_playlist = lib.sp_playlistcontainer_add_playlist(
self._sp_playlistcontainer, link._sp_link)
if sp_playlist == ffi.NULL:
return None
playlist = spotify.Playlist._cached(
self._session, sp_playlist, add_ref=True)
if index is not None:
self.move_playlist(self.__len__() - 1, index)
return playlist
def add_folder(self, name, index=None):
"""Add a playlist folder with ``name`` at the given ``index``.
The playlist folder name must not be space-only or longer than 255
chars.
If the ``index`` isn't specified, the folder is added at the end of the
container.
"""
self._validate_name(name)
if index is None:
index = self.__len__()
spotify.Error.maybe_raise(lib.sp_playlistcontainer_add_folder(
self._sp_playlistcontainer, index, utils.to_char(name)))
def _validate_name(self, name):
if len(name) > 255:
raise ValueError('Playlist name cannot be longer than 255 chars')
if len(re.sub('\s+', '', name)) == 0:
raise ValueError('Playlist name cannot be space-only')
def remove_playlist(self, index, recursive=False):
"""Remove playlist at the given index from the container.
If the item at the given ``index`` is the start or the end of a
playlist folder, and the other end of the folder is found, it is also
removed. The folder content is kept, but is moved one level up the
folder hierarchy. If ``recursive`` is :class:`True`, the folder content
is removed as well.
Using ``del playlist_container[3]`` is equivalent to
``playlist_container.remove_playlist(3)``. Similarly, ``del
playlist_container[0:2]`` is equivalent to calling this method with
indexes ``1`` and ``0``.
"""
item = self[index]
if isinstance(item, PlaylistFolder):
indexes = self._find_folder_indexes(self, item.id, recursive)
else:
indexes = [index]
for i in reversed(sorted(indexes)):
spotify.Error.maybe_raise(
lib.sp_playlistcontainer_remove_playlist(
self._sp_playlistcontainer, i))
@staticmethod
def _find_folder_indexes(container, folder_id, recursive):
indexes = []
for i, item in enumerate(container):
if isinstance(item, PlaylistFolder) and item.id == folder_id:
indexes.append(i)
assert len(indexes) <= 2, (
'Found more than 2 items with the same playlist folder ID')
if recursive and len(indexes) == 2:
start, end = indexes
indexes = list(range(start, end + 1))
return indexes
def move_playlist(self, from_index, to_index, dry_run=False):
"""Move playlist at ``from_index`` to ``to_index``.
If ``dry_run`` is :class:`True` the move isn't actually done. It is
only checked if the move is possible.
"""
if from_index == to_index:
return
spotify.Error.maybe_raise(lib.sp_playlistcontainer_move_playlist(
self._sp_playlistcontainer, from_index, to_index, int(dry_run)))
@property
@serialized
def owner(self):
"""The :class:`User` object for the owner of the playlist container."""
return spotify.User(
self._session,
sp_user=lib.sp_playlistcontainer_owner(self._sp_playlistcontainer),
add_ref=True)
def get_unseen_tracks(self, playlist):
"""Get a list of unseen tracks in the given ``playlist``.
The list is a :class:`PlaylistUnseenTracks` instance.
The tracks will remain "unseen" until :meth:`clear_unseen_tracks` is
called on the playlist.
"""
return spotify.PlaylistUnseenTracks(
self._session, self._sp_playlistcontainer, playlist._sp_playlist)
def clear_unseen_tracks(self, playlist):
"""Clears unseen tracks from the given ``playlist``."""
result = lib.sp_playlistcontainer_clear_unseen_tracks(
self._sp_playlistcontainer, playlist._sp_playlist)
if result == -1:
raise spotify.Error('Failed clearing unseen tracks')
def insert(self, index, value):
# Required by collections.MutableSequence
self[index:index] = [value]
@serialized
def on(self, event, listener, *user_args):
if self._sp_playlistcontainer_callbacks is None:
self._sp_playlistcontainer_callbacks = (
_PlaylistContainerCallbacks.get_struct())
lib.sp_playlistcontainer_add_callbacks(
self._sp_playlistcontainer,
self._sp_playlistcontainer_callbacks, ffi.NULL)
if self not in self._session._emitters:
self._session._emitters.append(self)
super(PlaylistContainer, self).on(event, listener, *user_args)
on.__doc__ = utils.EventEmitter.on.__doc__
@serialized
def off(self, event=None, listener=None):
super(PlaylistContainer, self).off(event, listener)
if (self.num_listeners() == 0 and
self in self._session._emitters):
self._session._emitters.remove(self)
off.__doc__ = utils.EventEmitter.off.__doc__
class PlaylistContainerEvent(object):
"""Playlist container events.
Using :class:`PlaylistContainer` objects, you can register listener
functions to be called when various events occurs in the playlist
container. This class enumerates the available events and the arguments
your listener functions will be called with.
Example usage::
import spotify
def container_loaded(playlist_container):
print('Playlist container loaded')
session = spotify.Session()
# Login, etc...
session.playlist_container.on(
spotify.PlaylistContainerEvent.CONTAINER_LOADED, container_loaded)
All events will cause debug log statements to be emitted, even if no
listeners are registered. Thus, there is no need to register listener
functions just to log that they're called.
"""
PLAYLIST_ADDED = 'playlist_added'
"""Called when a playlist is added to the container.
:param playlist_container: the playlist container
:type playlist_container: :class:`PlaylistContainer`
:param playlist: the added playlist
:type playlist: :class:`Playlist`
:param index: the index the playlist was added at
:type index: int
"""
PLAYLIST_REMOVED = 'playlist_removed'
"""Called when a playlist is removed from the container.
:param playlist_container: the playlist container
:type playlist_container: :class:`PlaylistContainer`
:param playlist: the removed playlist
:type playlist: :class:`Playlist`
:param index: the index the playlist was removed from
:type index: int
"""
PLAYLIST_MOVED = 'playlist_moved'
"""Called when a playlist is moved in the container.
:param playlist_container: the playlist container
:type playlist_container: :class:`PlaylistContainer`
:param playlist: the moved playlist
:type playlist: :class:`Playlist`
:param old_index: the index the playlist was moved from
:type old_index: int
:param new_index: the index the playlist was moved to
:type new_index: int
"""
CONTAINER_LOADED = 'container_loaded'
"""Called when the playlist container is loaded.
:param playlist_container: the playlist container
:type playlist_container: :class:`PlaylistContainer`
"""
class _PlaylistContainerCallbacks(object):
"""Internal class."""
@classmethod
def get_struct(cls):
return ffi.new('sp_playlistcontainer_callbacks *', {
'playlist_added': cls.playlist_added,
'playlist_removed': cls.playlist_removed,
'playlist_moved': cls.playlist_moved,
'container_loaded': cls.container_loaded,
})
# XXX Avoid use of the spotify._session_instance global in the following
# callbacks.
@staticmethod
@ffi.callback(
'void(sp_playlistcontainer *pc, sp_playlist *playlist, int position, '
'void *userdata)')
def playlist_added(sp_playlistcontainer, sp_playlist, index, userdata):
logger.debug('Playlist added at index %d', index)
playlist_container = PlaylistContainer._cached(
spotify._session_instance, sp_playlistcontainer, add_ref=True)
playlist = spotify.Playlist._cached(
spotify._session_instance, sp_playlist, add_ref=True)
playlist_container.emit(
PlaylistContainerEvent.PLAYLIST_ADDED,
playlist_container, playlist, index)
@staticmethod
@ffi.callback(
'void(sp_playlistcontainer *pc, sp_playlist *playlist, int position, '
'void *userdata)')
def playlist_removed(
sp_playlistcontainer, sp_playlist, index, userdata):
logger.debug('Playlist removed at index %d', index)
playlist_container = PlaylistContainer._cached(
spotify._session_instance, sp_playlistcontainer, add_ref=True)
playlist = spotify.Playlist._cached(
spotify._session_instance, sp_playlist, add_ref=True)
playlist_container.emit(
PlaylistContainerEvent.PLAYLIST_REMOVED,
playlist_container, playlist, index)
@staticmethod
@ffi.callback(
'void(sp_playlistcontainer *pc, sp_playlist *playlist, int position, '
'int new_position, void *userdata)')
def playlist_moved(
sp_playlistcontainer, sp_playlist, old_index, new_index,
userdata):
logger.debug(
'Playlist moved from index %d to %d', old_index, new_index)
playlist_container = PlaylistContainer._cached(
spotify._session_instance, sp_playlistcontainer, add_ref=True)
playlist = spotify.Playlist._cached(
spotify._session_instance, sp_playlist, add_ref=True)
playlist_container.emit(
PlaylistContainerEvent.PLAYLIST_MOVED,
playlist_container, playlist, old_index, new_index)
@staticmethod
@ffi.callback(
'void(sp_playlistcontainer *pc, void *userdata)')
def container_loaded(sp_playlistcontainer, userdata):
logger.debug('Playlist container loaded')
playlist_container = PlaylistContainer._cached(
spotify._session_instance, sp_playlistcontainer, add_ref=True)
playlist_container.emit(
PlaylistContainerEvent.CONTAINER_LOADED, playlist_container)
class PlaylistFolder(collections.namedtuple(
'PlaylistFolder', ['id', 'name', 'type'])):
"""An object marking the start or end of a playlist folder."""
pass
class PlaylistPlaceholder(object):
"""An object marking an unknown entry in the playlist container."""
pass
@utils.make_enum('SP_PLAYLIST_TYPE_')
class PlaylistType(utils.IntEnum):
pass