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