def select_artist(ids, func, *args, **kwargs): """Select an artist from the list then run func(artist_data, *args, **kwargs) Where artist_data is the dictionary as returned by api.get_artist_info.""" artists = [] def f(): """Finish up.""" if len(artists) == 1: artist = artists[0] else: dlg = wx.SingleChoiceDialog(backend.panel, 'Choose an artist', 'Artists', [a.title for a in artists]) if dlg.ShowModal() == wx.ID_OK: artist = artists[dlg.GetSelection()] else: artist = None dlg.Destroy() func(artist, *args, **kwargs) def load_artists(): if not ids: wx.CallAfter(f) return True id = ids.pop(0) artists.append(GoogleArtist.from_dict(api.get_artist_info(id))) add_job('Load Artists', load_artists)
def on_show(self, event): """Start adding tracks.""" event.Skip() if self.tracks_data and not self.processing_tracks: self.processing = True add_job('Load tracks for %s' % self.backend.name, self.add_track, run_every=load_speed)
def get_stream(self): """Check this track has been downloaded first.""" assert self.id is not None name = '%s.mp3' % self.id state = backend.get_download_state(name) if state is DownloadStates.downloaded: return FileStream(file=backend.get_full_path(name)) else: url = api.get_stream_url(self.id) if state is DownloadStates.none: add_job('Download track %r' % self, lambda: backend.download_file(url, name)) return URLStream(url.encode())
def display_album(event): """Display the album of the current result.""" res = backend.panel.get_result() if res is None: return wx.Bell() if isinstance(res, GoogleAlbum): id = res.id elif isinstance(res, GoogleTrack): id = res.album_id else: return backend.frame.on_error('You must run this command on an album.') if id is None: return backend.frame.on_error('No album ID found.') add_job('Display the album for %r' % res, partial(load_album, id))
def __init__(self, backend, *args, **kwargs): self.backend = backend super(PlaylistsPanel, self).__init__(*args, **kwargs) self.SetSizerType('vertical') self.refresh_playlists = wx.Button(self, label='Refresh &Playlists') self.refresh_library = wx.Button(self, label='Refresh &Library') self.refresh_promoted_songs = wx.Button( self, label='Refresh &Promoted Songs') self.refresh_promoted_songs.Bind( wx.EVT_BUTTON, lambda event: add_job('Refresh Promoted Songs', build_promoted_songs)) self.refresh_playlists.Bind( wx.EVT_BUTTON, lambda event: add_job('Refresh Playlists', build_playlists)) self.refresh_library.Bind( wx.EVT_BUTTON, lambda event: add_job('Refresh Library', build_library))
def build_playlists(): """Get playlists from Google and add them to playlists_root.""" logger.info('Loading playlists...') if not authenticated: try_login() return True logger.info('Loading playlists...') playlists_data = sorted( api.get_all_user_playlist_contents(), key=lambda entry: entry.get('name', 'Untitled Playlist')) num_playlists = len(playlists_data) logger.info('Loaded playlists: %d.', num_playlists) def add_playlists(): """Add playlists.""" playlists.clear() if not playlists_data: return True playlist = playlists_data.pop(0) wx.CallAfter(add_playlist, playlist) add_job('Add Playlists', add_playlists, run_every=load_speed) return True
def try_login(): global authenticated if authenticated: logger.info('Already logged in.') return # Already logged in. authenticated = api.login(config.login['username'], config.login['password'], api.FROM_MAC_ADDRESS) if not authenticated: logger.info('Failed login.') raise RuntimeError( 'Login failed. Check your username and password and try again.') logger.info('Login successful.') add_job('Populate Google Playlists', build_playlists) add_job('Populate Google Library', build_library) add_job('Populate Promoted Songs', build_promoted_songs)
def on_init(backend): add_job('Set Stream Title', set_stream_title, run_every=1.0)
def activate(self): """Load the contents of this album via the jobs framework..""" add_job('Load album tracks for %s' % self.title, partial(load_album, self.id))