class SpotifyAuthenticator: def __init__(self): self.scopes = 'user-read-private user-read-email playlist-read-private playlist-read-collaborative ' \ 'user-top-read user-library-read' self.sp_oauth = None self.sp = None self._init_oauth( "0.0.0.0" ) # init with stand in redirect_uri so update_token_info can be called def _init_oauth(self, redirect_uri): self.sp_oauth = SpotifyOAuth( scope=self.scopes, client_id=os.getenv("SPOTIFY_CLIENT_ID"), client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"), redirect_uri=redirect_uri) return self.sp_oauth.get_authorize_url() def initialize_auth(self, request): redirect_uri = request.build_absolute_uri('/') + 'login' return HttpResponseRedirect(self._init_oauth(redirect_uri)) def get_initial_token_info(self, initial_token): return self.sp_oauth.get_access_token(initial_token) def update_token_info(self, token_info): if self.sp_oauth.is_token_expired(token_info): return self.sp_oauth.refresh_access_token( token_info["refresh_token"]) return token_info def connect_user(self, token_info): self.sp = Spotify(token_info['access_token']) return self.sp.current_user()
def index(request): if not request.session.get('uuid'): request.session['uuid'] = str(uuid.uuid4()) auth_manager = SpotifyOAuth( scope='user-read-currently-playing', cache_path=session_cache_path(request.session), show_dialog=True) if request.method == 'GET': if request.GET.get("code"): # Step 3. Being redirected from Spotify auth page request.session['token_info'] = auth_manager.get_access_token( request.GET.get("code")) return redirect(index) if not auth_manager.get_cached_token(): auth_url = auth_manager.get_authorize_url() return HttpResponse(f'<h2><a href="{auth_url}">Sign in</a></h2>') if auth_manager.is_token_expired(request.session.get('token_info')): request.session['token_info'] = auth_manager.refresh_access_token( request.session.get('token_info')) spotify = Spotify(auth_manager=auth_manager) request.session['username'] = spotify.me()['id'] request.session['token'] = auth_manager.get_cached_token() return redirect(visview, request.session.get('username'))
def visview(request, username): if not request.session.get('username'): return redirect(index) if username != request.session.get('username'): return redirect(visview, request.session['username']) auth_manager = SpotifyOAuth(cache_path=session_cache_path(request.session)) if (not auth_manager.get_cached_token()) or auth_manager.is_token_expired(auth_manager.get_cached_token()): return redirect(index) context = {'username': username, 'sessionid': request.session.get('uuid')} return render(request, "visualizer/vis.html", context)
def get_spotipy_objs(): # Returns two list array with ouath and sp object letters = string.ascii_lowercase random_string = ''.join(random.choice(letters) for i in range(10)) oauth = SpotifyOAuth(client_id="ca077b4c1b6b4ea7a33ed0069ec3eecb", client_secret="2d2baf7aa1ff4c9792822aefac0ef7e5", redirect_uri="https://favorable-mark-297715.uc.r.appspot.com/form/", state = random_string, scope="user-read-recently-played user-modify-playback-state user-read-private", cache_path=None) token_dict = oauth.get_cached_token() token = token_dict['access_token'] refresh_token = token_dict['refresh_token'] if oauth.is_token_expired(token_dict): oauth.refresh_access_token(refresh_token) sp = spotipy.Spotify(auth_manager=oauth) current_user_id = sp.current_user()['id'] return [oauth, sp]
def authenticate(username): ''' authenticate with the spotify service ''' oauth_manager = SpotifyOAuth(username=username, scope=SCOPE) sp = spotipy.Spotify(oauth_manager=oauth_manager) user = sp.current_user() token_expired = oauth_manager.is_token_expired( oauth_manager.get_cached_token()) if token_expired: # refresh token logging.debug('Refreshing token for user {}'.format(user['id'])) oauth_manager.refresh_access_token( oauth_manager.get_cached_token()['refresh_token']) return oauth_manager, sp, user
class Muzik: def __init__(self, public_api=False): self.__create_cache_dir() self.ids = self.__read_cached_ids() if public_api: self.__sp = self.__connect_spotify() self.__sp_user = self.__connect_spotify_user() self.__user_id = self.__sp_user.me()["id"] def __create_cache_dir(self): """ Create cache dir at `CACHE_DIR` if doesn't already exists """ if not os.path.isdir(CACHE_DIR): os.mkdir(CACHE_DIR) def __read_cached_ids(self) -> pd.Series: """ Read the cached already fetched ids from the cache folder either returns the cached pd.Series, or empty series if no file there """ path = CACHE_DIR + ACH_IDS if os.path.exists(path): print(f"Reading data from cache file {path}") df = pd.read_pickle(path) else: df = pd.Series() print(f"Local library contains {len(df)} songs") return df def __read_credentials(self): """ Opens and return the content of `CRED_PATH_SPOTIFY` as a python dict """ with open(CRED_PATH_SPOTIFY, 'r') as handle: data = json.load(handle) return data def __connect_spotify_user(self): """ Connect to the API using the spotify user credentials needs more informations than the other method, but can access to personnal informations (including playlists :o) of the user """ data = self.__read_credentials() # generate a unique random number to prevent csrf state = hashlib.sha256(os.urandom(1024)).hexdigest() self.__user_credentials = SpotifyOAuth( **data, state=state, ) return self.__get_spotify_user( self.__user_credentials.get_access_token(as_dict=(False))) def __get_spotify_user(self, token): """ Returns a Spotify client authentified with the provided token """ return spotipy.Spotify(auth=token) def __connect_spotify(self): """ Connect to the public API of Spotify, useful to fetch songs ids since the API limite rate is higher here, however not really useful to create playlists and stuff """ data = self.__read_credentials() auth = {} auth["client_id"] = data["client_id"] auth["client_secret"] = data["client_secret"] return spotipy.Spotify(auth_manager=SpotifyClientCredentials(**auth)) def __refresh_token(self): """ Refreshes the tokenn if it has expired or not and updates the sp_user Spotify Interface with the new token """ cached = self.__user_credentials.get_cached_token() refreshed = self.__user_credentials.refresh_access_token( cached["refresh_token"]) self.__sp_user = self.__get_spotify_user(refreshed["access_token"]) def __update_token(self): """ Updates the token if it has expired !! may not work flawlessely (probably not in fact), hard to test since the token lasts for 1 hour haha """ # built-in function, does not work always good cached = self.__user_credentials.get_cached_token() if self.__user_credentials.is_token_expired(cached): print("Token expired, refreshing now") self.__refresh_token() # handmade function just in case the one above fails try: _ = self.__sp_user.me() except SpotifyException as e: if e.http_status == UNAUTHORIZED_ST_CODE: print("Token expired, refreshing now") self.__refresh_token() def __search_strings(self, row): """ Creates the search string for the Spotify API based on the information of the row Returns a list of multiple strings, to take into account if there is special characters (like "'") or multiple markets input: - row : pd.Series with genre, artists, songs,... output: - searches : list of tuples (search string, market) """ search = "" # artists artists = list(map(str.strip, row.artist.split(","))) if len(artists) > 1: sep = '" AND "' search += f"artist:\"{sep.join(artists)}\"" else: search += f"artist:\"{artists[0]}\"" # album if row.album != "N/A": search += f" album:\"{row.album}\"" # track name search += f" track:\"{row.song}\"" # dealing with "'"" # sometimes it will work with the "'" and sometimes not if "'" in search: searches_s = [search, search.replace("'", "")] else: searches_s = [search] searches = [] for market in MARKETS: for search in searches_s: searches.append((search, market)) return searches def __fetch_id(self, df): """ Fetches the Spotify songs id for each provided songs If it cannot find ids for a song, it will be set to None input: - df : a pd.DataFrame with a random index and the song specific columns (genre, artist, ...) """ # small hack to access the data from the index & the columns indexs = pd.MultiIndex.from_frame(df) songs = pd.DataFrame(data=df.values, index=indexs, columns=df.columns) ids = pd.Series(index=indexs, dtype=str, name="ids") bad_formats = [] # chosing the endpoint # by default try to take the public one, if doesn't exists # (public_api = False), use the private one try: endpoint = self.__sp except AttributeError: endpoint = self.__sp_user # format string padding used for the debug output str_format = int(math.log(len(songs), 10)) + 1 for idx, (_, content) in enumerate(songs.iterrows()): searches = self.__search_strings(content) bad_format = [] for search, market in searches: try: res = endpoint.search(search, market=market) track = res['tracks']['items'][0] except IndexError: bad_format.append((search, market)) else: # succeed to fetch an id break else: # did not managed to find an id with all the search strings # provided, set the id of the song to None bad_formats.append(bad_format) ids.iloc[idx] = None print(f"{Color.FAIL}" f"{idx + 1:<{str_format}}/{len(df)}" f"{Color.ENDC}" f" : {search} not in Spotify") continue album = track['album']['name'] name = track['name'] artist = track['artists'][0]['name'] id = track['id'] ids.iloc[idx] = id print(f"{Color.OKGREEN}" f"{idx + 1:<{str_format}}/{len(df)}" f"{Color.ENDC}" f" : {id} {name} {artist} {album}") return ids def __update_missing_list(self): """ Create a csv file containing every tracks that were not available on spotify """ missing = self.ids[self.ids.isnull()] missing.index.to_frame(index=False).to_csv(CACHE_DIR + MISSING_IDS) def __create_user_playlist(self): """ Creates a new playlist using PLAYLIST_NAME, PLAYLIST_DESC also pushes playlist cover PLAYLIST_COVER return: - playlist_id : string containing the id of the playlist """ # create the playlist with name, description, visibility print(f"Creating {PLAYLIST_NAME}...") ret = self.__sp_user.user_playlist_create(user=self.__user_id, name=PLAYLIST_NAME, public=True, description=PLAYLIST_DESC) playlist_id = ret["id"] # most important, upload the playlist image print(f"Uploading playlist cover from {PLAYLIST_COVER}") with open(PLAYLIST_COVER, "rb") as image_file: cover = base64.b64encode(image_file.read()) ret = self.__sp_user.playlist_upload_cover_image(playlist_id, cover) return playlist_id def __get_playlist_id(self): """ Returns the playlist id of PLAYLIST_NAME if the playlist doesn't exists yet, it will create it and return the id of the newly created playlist """ # check if the playlist already exists user_playlists = self.__sp_user.user_playlists(self.__user_id) playlist_id = None if len(user_playlists["items"]) > 0: for user_pl in user_playlists["items"]: if user_pl["name"] == PLAYLIST_NAME: playlist_id = user_pl["id"] break # at this point, if the playlist exists, the id is stored in # playlist_id, otherwise we have still a None value if playlist_id is None: print(f"Playlist {PLAYLIST_NAME} doesn't exists yet") playlist_id = self.__create_user_playlist() print(f"Using playlist {PLAYLIST_NAME} : {playlist_id}") return playlist_id def update(self, ach): """ updates the known list of ids with the newer version of the ach musik sheet input: - ach : raw sheet from google with multiindex """ self.__update_token() # turn the index to DataFrame objects new_songs = ach.index.to_frame().reset_index(drop=True) if self.ids.empty: # in case the cached list was empty, simply fetch the whole # list self.ids = self.__fetch_id(new_songs) else: old_songs = self.ids.index.to_frame().reset_index(drop=True) # get the list of the common values common_songs = new_songs.merge(old_songs, how='inner') # remove the songs that are not anymore in the cached df depr = pd.concat([common_songs, old_songs]).drop_duplicates(keep=False) to_remove = pd.MultiIndex.from_frame(depr) if len(to_remove) > 0: self.ids = self.ids.drop(to_remove) # adds the new songs from the ach sheet news = pd.concat([common_songs, new_songs]).drop_duplicates(keep=False) if len(news) > 0: new_ids = self.__fetch_id(news) self.ids = pd.concat([self.ids, new_ids]) else: print("Local list already updated") # save updated list in cache self.ids.to_pickle(CACHE_DIR + ACH_IDS) # also updates missing ID list self.__update_missing_list() def create_playlist(self, playlist): """ Create (or replace) a playlist containing all the songs provided in the playlists DataFrame input: - playlist : pd.DataFrame indexed by a MultiIndex with genre, artist, song, ... """ self.__update_token() # get the playlist id of PLAYLIST_NAME playlist_id = self.__get_playlist_id() # get the tracks tracks_all = self.ids[playlist.index] tracks_results = tracks_all.isnull().value_counts() print(f"Adding {tracks_results[False]} tracks," f" Missing {tracks_results[True]} tracks") tracks_id = tracks_all.dropna().values print(f"Inserting {len(tracks_id)} songs in the playlist...") # spotify api "only" handles 100 tracks by requests # so here we split the data batch_size = int(len(tracks_id) / MAX_TRACK_PER_REQUESTS) + 1 batches = np.array_split(tracks_id, batch_size) str_format = int(math.log(len(batches), 10)) + 1 print(f"{0:<{str_format}}/{len(batches)} batch inserting...") # the first call `replace_tracks` clear the playlist AND # adds the supplied tracks self.__sp_user.user_playlist_replace_tracks(self.__user_id, playlist_id=playlist_id, tracks=batches[0]) if len(batches) > 1: for idx, batch in enumerate(batches[1:]): print(f"{idx+2:<{str_format}}/{len(batches)}" " batch inserting...") # add the rest of the tracks self.__sp_user.user_playlist_add_tracks( self.__user_id, playlist_id=playlist_id, tracks=batch) print("Playlist done") def get_playlists(self): self.__update_token() return self.__sp_user.user_playlists(self.__user_id)
class PlaySpotify(MycroftSkill): def __init__(self): MycroftSkill.__init__(self) stream = open( '/opt/mycroft/skills/play-spotify-skill/spotipy.yaml', 'r') spotipyConfig = yaml.safe_load(stream) self.code = spotipyConfig.get('Code') scope = "user-read-playback-state user-modify-playback-state playlist-read-private playlist-read-collaborative" self.auth_manager = SpotifyOAuth(scope=scope, client_id=spotipyConfig.get( 'ClientID'), client_secret=spotipyConfig.get('ClientSecret'), redirect_uri='http://127.0.0.1/', open_browser=False, username='******') self.token_info = self.auth_manager.get_access_token( code=spotipyConfig.get('Code'), as_dict=True) self.sp = spotipy.Spotify(auth_manager=self.auth_manager) @intent_file_handler('play.artist.song.intent') def handle_play_artist_song(self, message): self.speak_dialog('play.artist.song.intent', { 'artist': message.data.get('artist'), 'song': message.data.get('song')}) @intent_file_handler('play.playlist.intent') def handle_play_playlist(self, message): self.log.info(("The playlist is {}").format( message.data.get('playlist'))) self.refreshToken() deviceId = self.getDevice() if deviceId == None: self.speak_dialog('play.device.notfound') return playlists = self.sp.current_user_playlists() while playlists: for _, playlist in enumerate(playlists['items']): name = playlist['name'].lower() if name == message.data.get('playlist'): self.playPlaylist(deviceId, playlist['uri'], playlist['tracks']['total']) self.speak_dialog('play.playlist', { 'playlist': message.data.get('playlist')}) return if playlists['next']: playlists = self.sp.next(playlists) else: playlists = None self.speak_dialog('play.playlist.notfound') @intent_file_handler('play.some.intent') def handle_play_some(self, message): self.log.info(("The genre is {}").format( message.data.get('type'))) self.refreshToken() deviceId = self.getDevice() if deviceId == None: self.speak_dialog('play.device.notfound') return genres = self.sp.recommendation_genre_seeds() for _, genre in enumerate(genres['genres']): if genre == message.data.get('type'): query = ('name:{}').format(genre) recomendations = self.sp.search(query, limit=50, type='playlist', market='ES') playlistList = recomendations['playlists'] self.log offset = random.randint(1, len(playlistList)) for i, playlist in enumerate(playlistList): if i==offset: self.playPlaylist(deviceId, playlist['uri'], playlist['tracks']['total']) self.speak_dialog('play.some', {'type': message.data.get('type')}) return self.speak_dialog('play.genre.notfound', { 'type': message.data.get('type')}) @intent_file_handler('play.stop.intent') def handle_play_stop(self, message): self.log.info("Stopping...") self.refreshToken() deviceId = self.getDevice() if deviceId == None: self.speak_dialog('play.device.notfound') return self.sp.pause_playback(deviceId) self.speak_dialog('play.stop') @intent_file_handler('play.artist.intent') def handle_play_artist(self, message): self.speak_dialog( 'play.artist', {'artist': message.data.get('artist')}) def getDevice(self): devices = self.sp.devices() for _, device in enumerate(devices['devices']): if device['name'] == 'Mycroft': return device['id'] return None def refreshToken(self): if self.auth_manager.is_token_expired(self.token_info): self.token_info = self.auth_manager.get_access_token( code=self.code, as_dict=True) def playPlaylist(self, deviceId, playlistUri, total): offset = random.randint(1, total) self.sp.start_playback(device_id=deviceId, context_uri=playlistUri, offset={'position': offset}) self.sp.shuffle(True, device_id=deviceId) self.sp.repeat(True, device_id=deviceId)