示例#1
0
def main():
    try:
        auth = Auth()
        token = auth.get_access_token()

        sp = Spotify(token)

        artist = input("Please enter your favourite artist's name: ").lstrip(
        ).rstrip().lower()

        # If an invalid input, print error message and exit
        if not artist:
            print('Please enter a valid artist name.')
            sys.exit(1)

        # Get the spotify ID of artist
        artist_id = sp.get_artist_id(artist)

        # Get user's Spotify ID
        user_id = os.getenv('SPOTIFY_USER_ID')

        # Do necessary error check. If artist exists in Spotify, then continue with data
        if artist_id:
            # Get artists albums
            albums = sp.get_albums(artist_id,
                                   limit=MAX_ALBUM_LIMIT_SET_BY_SPOTIFY)

            # If there are albums, print them
            if albums:
                print()
                print('*' * FIFTY_TIMES)
                print(f'{artist.title()} - ALBUMS')
                print('*' * FIFTY_TIMES)
                for album in albums:
                    print(album)
                print()

                # Get the top tracks of artists and if exists, print them
                top_tracks = sp.get_top_tracks(
                    artist_id, market=DEFAULT_MARKET_COUNTRY_CODE)
                if top_tracks:
                    print()
                    print('*' * FIFTY_TIMES)
                    print(
                        f'{artist.title()} - Top tracks in {DEFAULT_MARKET_COUNTRY_CODE}'
                    )
                    print('*' * FIFTY_TIMES)
                    for track in top_tracks:
                        print(track)
                    print()

                    # Get user's tracks (the tracks in the public playlists)
                    my_tracks = sp.get_users_playlists_tracks(user_id)

                    # Get the top tracks of artist which are in your collection.
                    for track in my_tracks:
                        for top_track in top_tracks:
                            if track in top_track:
                                print(
                                    f'You have {top_track} in your collection!'
                                )
                                break
                    print()
                else:
                    print(f'No top tracks for {artist} in {market}')
            else:
                print(f'No albums found for {artist.title()}')
        else:
            print(f'No albums found for {artist.title()}')

        # Get your play lists. Print them if any.
        playlists = sp.get_users_playlists(user_id)

        if playlists:
            print()
            print('*' * FIFTY_TIMES)
            print('My Play lists')
            print('*' * FIFTY_TIMES)
            for name in playlists:
                print(name)
            print()
        else:
            print("You don't have any public play lists.")

    except Exception as ex:
        print(ex)