def callback(): # Auth Step 4: Requests refresh and access tokens auth_token = request.args['code'] code_payload = { "grant_type": "authorization_code", "code": str(auth_token), "redirect_uri": REDIRECT_URI, 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, } post_request = requests.post(SPOTIFY_TOKEN_URL, data=code_payload) # Auth Step 5: Tokens are Returned to Application response_data = json.loads(post_request.text) access_token = response_data["access_token"] refresh_token = response_data["refresh_token"] token_type = response_data["token_type"] expires_in = response_data["expires_in"] # Auth Step 6: Use the access token to access Spotify API authorization_header = {"Authorization": "Bearer {}".format(access_token)} # Get profile data user_profile_api_endpoint = "{}/me".format(SPOTIFY_API_URL) profile_response = requests.get(user_profile_api_endpoint, headers=authorization_header) profile_data = json.loads(profile_response.text) user = User(spotify_id=profile_data['id'], name=profile_data['display_name'], jwt=access_token) user_check = User.query.filter_by(spotify_id=profile_data['id']).first() # Updates DB if not user_check: user.save() else: user_check.name = user.name user_check.spotify_id = user.spotify_id user_check.jwt = user.jwt user_check.save() user = user_check login_user(user) # Get user playlist data playlist_api_endpoint = "{}/playlists".format(profile_data["href"]) playlists_response = requests.get(playlist_api_endpoint, headers=authorization_header) playlist_data = json.loads(playlists_response.text) playlists = [] for item in playlist_data["items"]: playlists.append( Playlist(spotify_id=item['id'], name=item['name'], image=item['images'][0]['url']).to_json()) session['playlists'] = playlists return redirect(url_for('web.like'))
def create_playlist(self, name, description, add_random_torrents=False): playlist = Playlist(len(self.playlists) + 1, name, description) if add_random_torrents: picked_torrents = sample(self.torrents, randint(0, min(20, len(self.torrents)))) for torrent in picked_torrents: playlist.add_torrent(torrent) self.playlists.add(playlist)
def get_playlist(self, ctx: commands.Context, title: str = None): """Return a playlist with an id.""" if not ctx.author.id in self.user_playlists: raise Exception( "Utilisez 'playlist select [title]' pour sélectionner une playliste." ) playlist_id = self.user_playlists[ctx.author.id] if not playlist_id in self.playlists: raise Exception(f"La playliste d'id {playlist_id} n'existe pas.") return Playlist(self.playlists[playlist_id])
def select(id): playlist = None sql = "SELECT * FROM playlists WHERE id = %s" values = [id] result = run_sql(sql, values)[0] if result is not None: league_type = league_type_repo.select(result['league_type_id']) playlist = Playlist(league_type, result['round_no']) return playlist
def select_all(): playlists = [] sql = "SELECT * FROM playlists" results = run_sql(sql) for result in results: league_type = league_type_repo.select(result['league_type_id']) playlist = Playlist(league_type, result['round_no']) playlists.append(playlist) return playlists
def set_playlist(self, name, description, assets): self.kv.set_playlist(name, Playlist(name, description, assets))
def get_playlist_from_id(self, playlist_id: str): """Return a playlist given its id.""" if playlist_id in self.playlists: return Playlist(self.playlists[playlist_id]) else: raise Exception(f"La playliste d'id {playlist_id} n'existe pas.")