def app_env(): """ Retrieves application credentials from the environment. """ cred = tk.config_from_environment() if any(i is None for i in cred): skip_or_fail(KeyError, 'No application credentials!') return cred
def queue_song(db: Session, song_url: str, spotify_username: str): if spotify_username: conf = tk.config_from_environment() cred = tk.Credentials(*conf) spotify = tk.Spotify() spotify_data = crud.spotify.get(db_session=db, id=spotify_username) token = cred.refresh_user_token(spotify_data.refresh_token) with spotify.token_as(token): uri = "spotify:track:" + song_url.split("/").pop(-1) spotify.playback_queue_add(uri) else: print("ERROR: NO USERNAME - " + str(spotify_username))
def test_environment_read_modified_names(self): import os import tekore as tk tk.client_id_var = 'client_id' tk.client_secret_var = 'client_secret' tk.redirect_uri_var = 'redirect_uri' os.environ[tk.client_id_var] = 'id' os.environ[tk.client_secret_var] = 'secret' os.environ[tk.redirect_uri_var] = 'uri' conf = config_from_environment() assert conf == ('id', 'secret', 'uri')
def add_songs_for_playlist(db, playlist_id: int, playlist_link: str): client_id, client_secret, redirect_uri = tk.config_from_environment() token = tk.request_client_token(client_id, client_secret) spotify = tk.Spotify(token) playlist = spotify.playlist( playlist_id=playlist_link.split("/").pop(-1).split("?").pop(0)) # ask spotify for tracks for item in playlist.tracks.items: if item.track: name = item.track.name + " - " + item.track.artists.pop().name link = item.track.href duration = item.track.duration_ms crud.song.create_with_playlist(db, obj_in=SongCreate(name=name, link=link, duration=duration), playlist_id=playlist_id)
def get_left_playtime(db: Session, spotify_username: str) -> int: if spotify_username: conf = tk.config_from_environment() cred = tk.Credentials(*conf) spotify = tk.Spotify() spotify_data = crud.spotify.get(db_session=db, id=spotify_username) token = cred.refresh_user_token(spotify_data.refresh_token) with spotify.token_as(token): current = spotify.playback_currently_playing() if current: return current.item.duration_ms - current.progress_ms else: return 0 else: return 0
import os from urllib.parse import urlencode from sys import argv import json import requests import crud from datetime import datetime app = Flask(__name__) app.secret_key = 'dev' app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] = True SPOTIFY_KEY = os.environ['SPOTIFY_KEY'] conf = tk.config_from_environment() client_id, client_secret, redirect_uri = conf cred = tk.Credentials(*conf) spotify = tk.Spotify() users = {} @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def show_homepage(path): """Show the homepage.""" if 'user_id' in session: user_id = session.get('user_id') user_dict = crud.get_user_by_id(user_id)
def test_environment_user_refresh_returned(self): _, _, _, _ = config_from_environment(return_refresh=True)
import tekore as tk import os from flask import Flask, Response, session, jsonify from functools import wraps spotify_conf = tk.config_from_environment() spotfy_cred = tk.Credentials(*spotify_conf) spotify = tk.Spotify() user_to_token = {} app = Flask(__name__) # Decorate to ensure user is logged in to spotify def spotify_user_logged_in(f): @wraps(f) def wrap(*args, **kwargs): user = session.get('spotify_user', None) if user is None: # There is no logged in user --> Send to login page return jsonify({ 'redirect': spotfy_cred.user_authorisation_url( scope=tk.scope.playlist_read_private) }) else: user_token: tk.Token = user_to_token.get(user, None) if user_token.is_expiring: user_token = user_token.refresh_token(user_token) user_to_token[user] = user_token return f(*args, **kwargs)