def test_spinner(self, yaspin): type(yaspin.return_value).green = PropertyMock(return_value=yaspin) with spinner("foo") as sp: self.assertEqual(sp, yaspin.return_value) yaspin.return_value.start.assert_called_once_with() yaspin.return_value.stop.assert_called_once_with()
def fetch_tracks(*args): kwargs = dict(provider=Provider.lastfm) if args: kwargs["id"] = lambda x: x in args # So wrong, but yaspin doesn't support nested spinners LastService.get_tags() with spinner("Fetching track lists") as sp: for playlist in PlaylistManager.find(**kwargs): tracklist = LastService.get_tracks( type=playlist.type, **playlist.arguments ) track_ids: List[str] = [] for entry in tracklist: id = TrackManager.set( dict(artist=entry.artist.name, name=entry.name) ).id if id not in track_ids: track_ids.append(id) sp.write( "Playlist: {} - {} tracks".format(playlist.id, len(track_ids)) ) PlaylistManager.update(playlist, dict(tracks=track_ids))
def test_spinner_with_exception(self, yaspin, secho): type(yaspin.return_value).green = PropertyMock(return_value=yaspin) with spinner("foo"): raise Exception("Fatal") yaspin.return_value.start.assert_called_once_with() yaspin.return_value.stop.assert_called_once_with() secho.assert_called_once_with("Fatal")
def retrieve_tags(): page = 1 tags = [] # type: List[dict] with spinner("Fetching tags"): while len(tags) < 1000: tags.extend([ t.to_dict() for t in Tag.get_top_tags(limit=250, page=page) ]) page += 1 return tags
def fetch_tracks(): tracks = TrackManager.find(youtube_id=None) message = "Matching tracks to videos" with spinner(message) as sp: for track in tracks: sp.text = "{}: {} - {}".format(message, track.artist, track.name) youtube_id = YouService.search_track(track) TrackManager.update(track, dict(youtube_id=youtube_id)) total = len(tracks) if total > 0: sp.text = "Matched {} tracks to videos".format(magenta(total))
def push_playlists(): playlists = PlaylistManager.find(youtube_id=None) message = "Creating playlists" with spinner(message) as sp: for playlist in playlists: sp.text = "{}: {}".format(message, playlist.title) youtube_id = YouService.create_playlist(playlist) PlaylistManager.update(playlist, dict(youtube_id=youtube_id)) total = len(playlists) if total > 0: sp.text = "{0}: {1}/{1} ".format(message, total)
def push_tracks(): online_playlists = PlaylistManager.find(youtube_id=lambda x: x is not None) click.secho("Syncing playlists", bold=True) for playlist in online_playlists: add = items = remove = [] with spinner("Fetching playlist items: {}".format(playlist.title)): items = YouService.get_playlist_items(playlist) online = set([item.video_id for item in items]) offline = set([ track.youtube_id for track in TrackManager.find( youtube_id=lambda x: x is not None, id=lambda x: x in playlist.tracks, ) ]) add = offline - online remove = online - offline message = "Adding new playlist items" with spinner(message) as sp: for video_id in sorted(add): sp.text = "{}: {}".format(message, video_id) YouService.create_playlist_item(playlist, video_id) if len(add) > 0: sp.text = "{}: {}".format(message, len(add)) message = "Removing playlist items" with spinner(message) as sp: remove = [item for item in items if item.video_id in remove] for item in sorted(remove): sp.text = "{}: {}".format(message, video_id) YouService.remove_playlist_item(item) if len(remove) > 0: sp.text = "{}: {}".format(message, len(remove)) if len(add) or len(remove): PlaylistManager.update(playlist, dict(uploaded=timestamp()))
def fetch_playlists(): with spinner("Fetching playlists info") as sp: playlists = YouService.get_playlists() for playlist in playlists: if not PlaylistManager.exists(playlist): items = YouService.get_playlist_items(playlist) track_ids = [ TrackManager.set( dict( artist=item.artist, name=item.name, youtube_id=item.video_id, )).id for item in items ] playlist.tracks = track_ids PlaylistManager.set(playlist.asdict()) total = len(playlists) if total > 0: sp.text = "Fetched {} playlist(s) info".format(magenta(total))