How to use spotipy - 10 common examples

To help you get started, we’ve selected a few spotipy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github plamere / spotipy / tests / authtests.py View on Github external
self.assertTrue(playlist['tracks']['total'] == 3)
        self.assertTrue(len(playlist['tracks']['items']) == 3)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        username = sys.argv[1]
        del sys.argv[1]

        scope = 'playlist-modify-public '
        scope += 'user-library-read '
        scope += 'user-follow-read '
        scope += 'user-library-modify '
        scope += 'user-read-private '
        scope += 'user-top-read'

        token = util.prompt_for_user_token(username, scope)
        spotify = spotipy.Spotify(auth=token)
        spotify.trace = False
        unittest.main()
    else:
        print("Usage: %s username" % (sys.argv[0],))
github plamere / spotipy / tests / authtests2.py View on Github external
bad_tracks = ['spotify:track:bad']
        input = self.four_tracks + bad_tracks
        results = spotify.audio_features(input)
        self.assertTrue(len(results) == len(input))
        for track in results[:-1]:
            if track != None:
                assert('speechiness' in track)
        self.assertTrue(results[-1] == None)

    def test_recommendations(self):
        results = spotify.recommendations(seed_tracks=self.four_tracks, min_danceability=0, max_loudness=0, target_popularity=50)
        self.assertTrue(len(results['tracks']) == 20)


if __name__ == '__main__':
    client_credentials_manager = SpotifyClientCredentials()
    spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
    spotify.trace = False
    unittest.main()
github plamere / spotipy / tests / test_oauth.py View on Github external
def test_get_authorize_url_passes_state_from_constructor(self):
        state = "STATE"
        oauth = SpotifyOAuth("CLID", "CLISEC", "REDIR", state)

        url = oauth.get_authorize_url()

        parsed_url = urllibparse.urlparse(url)
        parsed_qs = urllibparse.parse_qs(parsed_url.query)
        self.assertEqual(parsed_qs['state'][0], state)
github plamere / spotipy / tests / test_oauth.py View on Github external
def test_get_authorize_url_passes_state_from_func_call(self):
        state = "STATE"
        oauth = SpotifyOAuth("CLID", "CLISEC", "REDIR", "NOT STATE")

        url = oauth.get_authorize_url(state=state)

        parsed_url = urllibparse.urlparse(url)
        parsed_qs = urllibparse.parse_qs(parsed_url.query)
        self.assertEqual(parsed_qs['state'][0], state)
github plamere / spotipy / tests / authtests.py View on Github external
def test_track_bad_id(self):
        try:
            track = spotify.track(self.bad_id)
            self.assertTrue(False)
        except spotipy.SpotifyException:
            self.assertTrue(True)
github ritiek / spotify-downloader / test / test_spotify_tools.py View on Github external
def test_fake_token_generator(self, monkeypatch):
        spotify_tools.spotify = None
        monkeypatch.setattr(spotify_tools, "generate_token", lambda: 123123)

        with pytest.raises(spotipy.client.SpotifyException):
            spotify_tools.generate_metadata("ncs - spectre")
github felix-hilden / spotipy / tests / client / misc.py View on Github external
def setUp(self):
        self.client = Spotify(self.user_token)
github felix-hilden / spotipy / tests / enumerate.py View on Github external
def test_enum_str_is_name(self):
        e = SerialisableEnum('e', 'a b c')
        self.assertEqual(str(e.a), 'a')
github edujtm / diversify / diversify / session.py View on Github external
def _get_session(authenticate: bool = True) -> spotipy.Spotify:
    if not os.getenv('SPOTIPY_CLIENT_ID'):
        raise SystemExit(
            "Spotify application credentials are missing. Rename .env.example to .env and"
            " fill in the values"
        )
    if authenticate:
        token = utils.login_user()
    else:
        token = utils.cached_token(scope=SCOPE)

    if token:
        return spotipy.Spotify(auth=token)
    else:
        if authenticate:
            raise utils.DiversifyError(f"Unable to log in to your account")
        else:
            raise utils.DiversifyError("You are not logged in. Run [diversify login USERNAME] to log in.")
github keessonnema / Kezbot / kezbot / modules / get_spotify_url.py View on Github external
if not re.search(KeepWords, m.group(), re.I):
                        result = re.sub(re.escape(m.group()), '', result)
                result = re.sub(StringRegex, '', result).strip()
                result = ' '.join(result.split())
                new_list = list(filter(None, re.split(split, result)))

                first = new_list[0]
                sep = 'aka'
                sep2 = 'AKA'
                new_list[0] = first.split(sep, 1)[0]
                new_list[0] = first.split(sep2, 1)[0]

                spotify_token = util.prompt_for_user_token(Config.username, Config.scope)

                if spotify_token:
                    spot = spotipy.Spotify(auth=spotify_token)
                    artist = new_list[0]
                    track = new_list[1]

                    results = spot.search(q="artist:{} track:{}".format(artist, track, limit=1))

                    if results:
                        spottracks = results['tracks']['items']
                        if spottracks:
                            spotartist = spottracks[0]['artists'][0]['name']
                            spottitle = spottracks[0]['name']
                            spoturl = spottracks[0]['external_urls']['spotify']
                            update.effective_message.reply_text \
                                ("► {0} - {1} \n{2}".format(spotartist, spottitle, spoturl))
                        else:
                            results = spot.search(q="artist:{} track:{}"
                                                  .format(track, artist, limit=1))