def data_client(user_token): """ Provides a client with a user token. """ sender = tk.RetryingSender(sender=tk.SyncSender()) yield tk.Spotify(user_token, sender=sender) sender.close()
def app_client(app_token): """ Provides a client with an application token. """ sender = tk.RetryingSender(sender=tk.SyncSender()) yield tk.Spotify(app_token, sender=sender) sender.close()
async def user_aclient(user_token): """ Provides an asynchronous client with a user token. """ sender = tk.RetryingSender(sender=tk.AsyncSender()) yield tk.Spotify(user_token, sender=sender) await sender.close()
async def app_aclient(app_token): """ Provides an asynchronous client with an application token. """ sender = tk.RetryingSender(sender=tk.AsyncSender()) yield tk.Spotify(app_token, sender=sender) await sender.close()
def connect(self): if self.cred is None: self.prep_cred() if self.spotify is None: if self.token is None: self.load_token() if self.token: print("Init spotify support") self.sender = tk.RetryingSender(retries=3) self.spotify = tk.Spotify(self.token, sender=self.sender)
def init_spotify(self): """ Initialize the main entity for the Spotify API. This includes Authorization. :return: Spotify object on which API methods can be called """ cred = tk.RefreshingCredentials(config_project.CLIENT_ID, config_project.CLIENT_SECRET) app_token = cred.request_client_token() sender = tk.RetryingSender(sender=tk.CachingSender()) spotify = tk.Spotify(app_token, sender=sender) return spotify
def connect(self): if not self.tauon.prefs.spotify_token or not self.tauon.prefs.spot_mode: return if len(self.tauon.prefs.spot_client) != 32: return if self.cred is None: self.prep_cred() if self.spotify is None: if self.token is None: self.load_token() if self.token: print("Init spotify support") self.sender = tk.RetryingSender(retries=3) self.spotify = tk.Spotify(self.token, sender=self.sender)
import os import tekore as tk SPOTIFY_CLIENT_ID = os.environ.get('SPOTIFY_CLIENT_ID') SPOTIFY_CLIENT_SECRET = os.environ.get('SPOTIFY_CLIENT_SECRET') SPOTIFY_REFRESH_TOKEN = os.environ.get('SPOTIFY_REFRESH_TOKEN') SPOTIFY_PLAYLIST_ID = os.environ.get('SPOTIFY_PLAYLIST_ID') TELEGRAM_CHANNEL = os.environ.get('TELEGRAM_CHANNEL') DATABASE_URL = os.environ.get('DATABASE_URL') SERVER_PORT = int(os.environ.get('PORT', 5000)) SERVER_ADDRESS = 'https://' + os.environ.get('APP_NAME') + '.herokuapp.com' TELETHON_API_ID = os.environ.get('TELETHON_API_ID') TELETHON_API_HASH = os.environ.get('TELETHON_API_HASH') TELETHON_SESSION_WEB = os.environ.get('TELETHON_SESSION_WEB') TELETHON_SESSION_WORKER = os.environ.get('TELETHON_SESSION_WORKER') DEEZER_ARL_TOKEN = os.environ.get('DEEZER_ARL_TOKEN') UPDATE_BIOS = True if os.environ.get('UPDAE_BIOS', 'true').lower() == 'true' else False CHECK_CHANNEL_DELETED = (True if os.environ.get( 'CHECK_CHANNEL_DELETED', 'true').lower() == 'true' else False) CHECK_LOCAL_PLAYBACK = (True if os.environ.get( 'CHECK_LOCAL_PLAYBACK', 'true').lower() == 'true' else False) USING_WEB_SERVER = (True if os.environ.get('USING_WEB_SERVER', 'true').lower() == 'true' else False) if USING_WEB_SERVER is False: CHECK_LOCAL_PLAYBACK = False # spotify client cred = tk.RefreshingCredentials(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET) token = cred.refresh_user_token(SPOTIFY_REFRESH_TOKEN) spotify = tk.Spotify(token, sender=tk.RetryingSender())
async def preferences_from_platform( self, token: str, platform: str) -> Optional[Preferences]: """Processes user's data from Spotify/Genius to generate preferences Processes activity data from Spotify/Genius to generate user's favorite artists and genres. In case of insufficient data, it returns None. Args: token (str): Token used to log into platform. platform (str): Platform to get data from Returns: Optional[Preferences]: Generated preferences if sufficient data is avaialbe, else None. """ genres = [] artists = [] if platform == "genius": user_genius = lg.Genius(token) account = user_genius.account()["user"] pyongs = user_genius.user_pyongs(account["id"]) pyonged_songs = [] for contribution in pyongs["contribution_groups"]: pyong = contribution["contributions"][0] if pyong["pyongable_type"] == "song": api_path = pyong["pyongable"]["api_path"] song_id = int(api_path[api_path.rfind("/") + 1:]) pyonged_songs.append(song_id) public_genius = lg.PublicAPI(timeout=10) for song_id in pyonged_songs: song = public_genius.song(song_id)["song"] artists.append(song["primary_artist"]["name"]) for tag in song["tags"]: for genre in self.genres: if genre in tag["name"].lower(): genres.append(genre) else: user_spotify = tk.Spotify(token, sender=tk.RetryingSender()) top_tracks = await user_spotify.current_user_top_tracks( "short_term") top_artists = await user_spotify.current_user_top_artists(limit=5) user_spotify.close() # Add track genres to genres list params = { "method": "Track.getTopTags", "api_key": LASTFM_API_KEY, "format": "json", } lastfm_api_root = "http://ws.audioscrobbler.com/2.0/" async with httpx.AsyncClient() as client: for track in top_tracks.items: params.update({ "artist": track.artists[0], "track": track.name }) res = await client.get(lastfm_api_root, params=params) track_genres = res.json() if "toptags" in track_genres: for tag in track_genres["toptags"]["tag"]: for genre in self.genres: if genre in tag["name"].lower(): genres.append(genre) artists = [artist.name for artist in top_artists.items] # get count of genres and only keep genres with a >=30% occurance unique_elements, counts_elements = np.unique(genres, return_counts=True) counts_elements = counts_elements.astype(float) counts_elements /= counts_elements.sum() user_genres = [] found_artists = [] genres, percentage = np.asarray( (unique_elements, counts_elements)).tolist() for i, genre in enumerate(genres): if float(percentage[i]) > 0.3: user_genres.append(genre) # find user artists in recommender artists if user_genres: for artist in artists: found_artist = self._artists[self._artists.name == artist].name.values if found_artist.size > 0: found_artists.append(found_artist[0]) return (Preferences(genres=user_genres, artists=found_artists) if genres else None)