4
4
5
5
from mopidy import backend
6
6
7
- import spotify
8
-
9
7
from mopidy_spotify import translator , utils
10
8
11
9
10
+ _cache = {}
11
+
12
12
logger = logging .getLogger (__name__ )
13
13
14
14
@@ -23,25 +23,16 @@ def as_list(self):
23
23
return list (self ._get_flattened_playlist_refs ())
24
24
25
25
def _get_flattened_playlist_refs (self ):
26
- if self ._backend ._session is None :
26
+ if self ._backend ._web_client is None :
27
27
return
28
28
29
- if self ._backend ._session . playlist_container is None :
29
+ if self ._backend ._web_client . user_id is None :
30
30
return
31
31
32
- username = self ._backend ._session .user_name
33
- folders = []
34
-
35
- for sp_playlist in self ._backend ._session .playlist_container :
36
- if isinstance (sp_playlist , spotify .PlaylistFolder ):
37
- if sp_playlist .type is spotify .PlaylistType .START_FOLDER :
38
- folders .append (sp_playlist .name )
39
- elif sp_playlist .type is spotify .PlaylistType .END_FOLDER :
40
- folders .pop ()
41
- continue
42
-
32
+ web_client = self ._backend ._web_client
33
+ for web_playlist in web_client .get_user_playlists (_cache ):
43
34
playlist_ref = translator .to_playlist_ref (
44
- sp_playlist , folders = folders , username = username )
35
+ web_playlist , web_client . user_id )
45
36
if playlist_ref is not None :
46
37
yield playlist_ref
47
38
@@ -54,38 +45,15 @@ def lookup(self, uri):
54
45
return self ._get_playlist (uri )
55
46
56
47
def _get_playlist (self , uri , as_items = False ):
57
- try :
58
- sp_playlist = self ._backend ._session .get_playlist (uri )
59
- except spotify .Error as exc :
60
- logger .debug ('Failed to lookup Spotify URI %s: %s' , uri , exc )
61
- return
62
-
63
- if not sp_playlist .is_loaded :
64
- logger .debug (
65
- 'Waiting for Spotify playlist to load: %s' , sp_playlist )
66
- sp_playlist .load (self ._timeout )
67
-
68
- username = self ._backend ._session .user_name
69
- return translator .to_playlist (
70
- sp_playlist , username = username , bitrate = self ._backend ._bitrate ,
71
- as_items = as_items )
48
+ return playlist_lookup (
49
+ self ._backend ._web_client , uri ,
50
+ self ._backend ._bitrate , as_items )
72
51
73
52
def refresh (self ):
74
- pass # Not needed as long as we don't cache anything .
53
+ pass # TODO: Clear/invalidate all caches on refresh .
75
54
76
55
def create (self , name ):
77
- try :
78
- sp_playlist = (
79
- self ._backend ._session .playlist_container
80
- .add_new_playlist (name ))
81
- except ValueError as exc :
82
- logger .warning (
83
- 'Failed creating new Spotify playlist "%s": %s' , name , exc )
84
- except spotify .Error :
85
- logger .warning ('Failed creating new Spotify playlist "%s"' , name )
86
- else :
87
- username = self ._backend ._session .user_name
88
- return translator .to_playlist (sp_playlist , username = username )
56
+ pass # TODO
89
57
90
58
def delete (self , uri ):
91
59
pass # TODO
@@ -94,40 +62,27 @@ def save(self, playlist):
94
62
pass # TODO
95
63
96
64
97
- def on_container_loaded (sp_playlist_container ):
98
- # Called from the pyspotify event loop, and not in an actor context.
99
- logger .debug ('Spotify playlist container loaded' )
100
-
101
- # This event listener is also called after playlists are added, removed and
102
- # moved, so since Mopidy currently only supports the "playlists_loaded"
103
- # event this is the only place we need to trigger a Mopidy backend event.
104
- backend .BackendListener .send ('playlists_loaded' )
105
-
106
-
107
- def on_playlist_added (sp_playlist_container , sp_playlist , index ):
108
- # Called from the pyspotify event loop, and not in an actor context.
109
- logger .debug (
110
- 'Spotify playlist "%s" added to index %d' , sp_playlist .name , index )
111
-
112
- # XXX Should Mopidy support more fine grained playlist events which this
113
- # event can trigger?
65
+ def playlist_lookup (web_client , uri , bitrate , as_items = False ):
66
+ if web_client is None :
67
+ return
114
68
69
+ logger .info ('Fetching Spotify playlist "%s"' , uri )
70
+ web_playlist = web_client .get_playlist (uri , _cache )
115
71
116
- def on_playlist_removed (sp_playlist_container , sp_playlist , index ):
117
- # Called from the pyspotify event loop, and not in an actor context.
118
- logger .debug (
119
- 'Spotify playlist "%s" removed from index %d' , sp_playlist .name , index )
72
+ if web_playlist == {}:
73
+ logger .error ('Failed to lookup Spotify playlist URI %s' , uri )
74
+ return
120
75
121
- # XXX Should Mopidy support more fine grained playlist events which this
122
- # event can trigger?
76
+ return translator .to_playlist (
77
+ web_playlist , username = web_client .user_id , bitrate = bitrate ,
78
+ as_items = as_items )
123
79
124
80
125
- def on_playlist_moved (
126
- sp_playlist_container , sp_playlist , old_index , new_index ):
81
+ def on_playlists_loaded ():
127
82
# Called from the pyspotify event loop, and not in an actor context.
128
- logger .debug (
129
- 'Spotify playlist "%s" moved from index %d to %d' ,
130
- sp_playlist .name , old_index , new_index )
83
+ logger .debug ('Spotify playlists loaded' )
131
84
132
- # XXX Should Mopidy support more fine grained playlist events which this
133
- # event can trigger?
85
+ # This event listener is also called after playlists are added, removed and
86
+ # moved, so since Mopidy currently only supports the "playlists_loaded"
87
+ # event this is the only place we need to trigger a Mopidy backend event.
88
+ backend .BackendListener .send ('playlists_loaded' )
0 commit comments