def test_login_user(webbrowser_mock, spotify_mock, HTTPServer_mock, _read_access_token_mock): _read_access_token_mock.side_effect = [None, "nowitworks"] spotify_mock.verify_access_token.return_value = False result = generalist.login_user() assert result == "nowitworks" webbrowser_mock.open.assert_called() HTTPServer_mock().serve_forever.assert_called()
def create_playlist(args): """Create a playlist with the tracks in the playlist.""" token = generalist.login_user() tracks = generalist.get_saved_tracks(token) tracks = list(filter(lambda x: args.genre in x["genres"], tracks)) playlist_name = args.name or f"Generalist: {args.genre}" track_ids = [track["id"] for track in tracks] generalist.create_playlist(token, playlist_name, track_ids) print(f"Created playlist: {playlist_name}")
def test_login_user_not_verified(webbrowser_mock, spotify_mock, HTTPServer_mock, _read_access_token_mock): _read_access_token_mock.return_value = "supersecure" spotify_mock.verify_access_token.return_value = False result = generalist.login_user() assert result == "supersecure" webbrowser_mock.open.assert_called() HTTPServer_mock().serve_forever.assert_called()
def list_tracks(args: argparse.Namespace): """Show a list of the saved tracks.""" token = generalist.login_user() tracks = generalist.get_saved_tracks(token) if args.alpha: tracks = utils.sort_track_list(tracks) for track in tracks: print("{artists} - {track} ({genres})".format( artists=", ".join(track["artists"]), track=track["name"], genres=", ".join(track["genres"]), ))
def list_artists(args: argparse.Namespace): """Show a list of the artists from the saved tracks.""" token = generalist.login_user() tracks = generalist.get_saved_tracks(token) artists = {} for track in tracks: for artist in track["artists"]: if artist not in artists: artists[artist] = 0 artists[artist] = artists[artist] + 1 artists = dict(sorted(artists.items(), key=lambda item: item[0])) for artist, amount in artists.items(): print(f"{artist} ({amount})")
def list_genre_tracks(args: argparse.Namespace): """List all the tracks in a genre.""" token = generalist.login_user() tracks = generalist.get_saved_tracks(token) tracks = list(filter(lambda x: args.genre in x["genres"], tracks)) if args.alpha: tracks = utils.sort_track_list(tracks) for track in tracks: print("{artists} - {track} ({genres})".format( artists=", ".join(track["artists"]), track=track["name"], genres=", ".join(track["genres"]), ))
def list_genres(args: argparse.Namespace): token = generalist.login_user() tracks = generalist.get_saved_tracks(token) genres = {} for track in tracks: for genre in track["genres"]: if genre not in genres: genres[genre] = 0 genres[genre] = genres[genre] + 1 if args.alpha: genres = dict(sorted(genres.items(), key=lambda item: item[0])) else: genres = dict( sorted(genres.items(), key=lambda item: item[1], reverse=True)) for genre, amount in genres.items(): print(f"{genre} ({amount})")