def test_populate_user_creds(me_stub): spt = Spotify(populate_user_creds=False) # Offline test user = UserCreds() spt.user_creds = user spt._populate_user_creds(me_stub) assert getattr(spt.user_creds, "type", None) is None assert spt.user_creds.product == "premium"
def test_and_get_me_attr_attr_exists(): spt = Spotify() spt.user_creds = UserCreds() spt.user_creds.id = 'id1234' assert _set_and_get_me_attr_sync(spt, 'id') == 'id1234'
def test_and_get_me_attr_attr_exists(): spt = Spotify() spt.user_creds = UserCreds() spt.user_creds.id = "id1234" assert _set_and_get_me_attr_sync(spt, "id") == "id1234"
def test_authenticated_user_is_authorized(user_creds_from_env, client_creds_from_env): spotify = Spotify( client_creds=client_creds_from_env, user_creds=user_creds_from_env, ensure_user_auth=True, ) assert spotify.me()
def test_user_playlists(user_creds_from_env, client_creds_from_env): c = Spotify( client_creds=client_creds_from_env, user_creds=user_creds_from_env, ensure_user_auth=True, ) c.user_creds.user_id = None assert c.user_playlists()
def spotify_user_auth(): spotify = Spotify() user_creds = UserCreds() client_creds = ClientCreds() client_creds.load_from_env() user_creds.load_from_env() spotify.client_creds = client_creds spotify.user_creds = user_creds spotify._caller = spotify.user_creds yield spotify
def get_last_song(spotify: Spotify) -> typing.Optional[typing.Dict]: try: current_song = spotify.currently_playing() if current_song: return current_song["item"] else: last_tracks = spotify.recently_played_tracks(limit=1)["items"] return last_tracks[0]["track"] if last_tracks else None except ApiError: return None
async def test_user_is_active(user_creds_from_env, client_creds_from_env): ''' This will also work if you provide an empty client creds model. But when the access token eventually expires you'll need valid client creds to refresh it ''' spt = Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env) assert await spt.is_active is True
def test_user_is_rejected_with_bad_access_token(user_creds_from_env, client_creds_from_env): user_creds_from_env.access_token = 'BAD_ACCESS_TOKEN' with pytest.raises(AuthError): Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env, ensure_user_auth=True)
def test_initdata(client): cl = ClientCreds() spot = spotty_search.api.User(Spotify()) client.get('/auth/') spot.initdata()
async def test_user_is_authenticated_by_access_token(user_creds_from_env, client_creds_from_env): ''' This will also work if you provide an empty client creds model. But when the access token eventually expires you'll need valid client creds to refresh it ''' spt = Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env) await spt.populate_user_creds()
async def test_user_is_rejected_with_bad_access_token(user_creds_from_env, client_creds_from_env): user_creds_from_env.access_token = 'BAD_ACCESS_TOKEN' spt = Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env) assert spt._caller == spt.user_creds with pytest.raises(AuthError): await spt._check_authorization()
def test_client_instantiates_with_user_creds(): u = UserCreds() u.load_from_env() spt = Spotify(user_creds=u, ensure_user_auth=False, populate_user_creds=False) assert spt.user_creds.access_token is not None assert spt._caller == spt.user_creds
def get_spotify_client(hostname) -> Spotify: client = ClientCreds( client_id=os.getenv("SPOTIFY_CLIENT_KEY"), client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"), scopes=["user-read-currently-playing", "user-read-recently-played"], redirect_uri=f"http://{hostname}:4444", ) return Spotify(client_creds=client)
def test_user_is_authenticated_by_access_token(user_creds_from_env, client_creds_from_env): """ This will also work if you provide an empty client creds model. But when the access token eventually expires you'll need valid client creds to refresh it """ Spotify( client_creds=client_creds_from_env, user_creds=user_creds_from_env, ensure_user_auth=True, )
def test_client_instantiates_with_access_token(): u = UserCreds() u.load_from_env() access_token = u.access_token spt = Spotify(access_token=access_token, ensure_user_auth=False, populate_user_creds=False) assert spt.user_creds.access_token is not None assert spt.user_creds.refresh_token is None assert spt._caller == spt.user_creds assert not hasattr(spt, "access_token")
def test_oauth_uri_raises_deprecation_warning(): creds = ClientCreds(client_id='asdasdasdasdads', client_secret='asdasdasdasdasd', scopes=['asdasd', 'asdasd'], redirect_uri='asdasdasdasd') sync = Spotify(client_creds=creds) async_ = AsyncSpotify(client_creds=creds) with pytest.warns(DeprecationWarning): sync.oauth_uri with pytest.warns(DeprecationWarning): async_.oauth_uri
from pyfy import Spotify from pyfy import ClientCreds, Spotify # client_id ="2c0d0c49b20c4a2cbe346f42bb6dab74" client_secret ="811e8611fafc4683b415caae2814d98b" redirect_uri = 'http://localhost/' username = "******" scope = 'user-read-playback-state user-library-modify' client = ClientCreds(client_id=client_id, client_secret=client_secret) spt = Spotify(client_creds=client) print(spt.auth_uri(client_id=client_id,scopes=scope.split(" "),redirect_uri=redirect_uri,show_dialog=True)) authcode=input().strip() spt.build_user_creds(grant=authcode)
async def test_user_playlists(user_creds_from_env, client_creds_from_env): c = Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env) c.user_creds.user_id = None assert await c.user_playlists()
from pyfy import Spotify, ClientCreds, UserCreds from utils import config # Init pyfy spotify = Spotify() client_creds = ClientCreds( client_id=config["spotify"]["client_id"], client_secret=config["spotify"]["client_secret"], redirect_uri=config["spotify"]["client_redirect"], scopes=["user-read-recently-played", "user-read-playback-state"], ) spotify.client_creds = client_creds def get_credentials(user): return UserCreds(access_token=user.spotify_access_token, refresh_token=user.spotify_refresh_token)
def authorize(spotify: Spotify) -> None: print(spotify.auth_uri()) code = wait_for_code() spotify.build_user_creds(grant=code, set_user_creds=True)
import json from flask import Flask, request, redirect, g, render_template, make_response, session, abort, jsonify, url_for, Response import requests from pyfy import Spotify, ClientCreds, UserCreds, AuthError, ApiError import os import webbrowser from spt_keys import KEYS from main import mood import datetime import database import display import io spt = Spotify() client = ClientCreds() state = "123" app = Flask(__name__) @app.route("/authorize") def authorize(): export_keys() client.load_from_env() spt.client_creds = client if spt.is_oauth_ready: return redirect( 'https://accounts.spotify.com/authorize?redirect_uri=http://127.0.0.1:5000/callback/q&client_id=&response_type=code&scope=user-read-recently-played&show_dialog=false&state=123' ) else: return (
def spotify(): yield Spotify()
def spotify_client_auth(): spotify = Spotify() client_creds = ClientCreds() client_creds.load_from_env() spotify.client_creds = client_creds yield spotify
#!/usr/bin/python3.7 from flask import Flask, request, jsonify from pyfy import ClientCreds as ClientCreds, Spotify import requests, os, time, json, pprint client = ClientCreds(client_id='634a89ab0e2e4ec2b59dff7bfcbfca3d', client_secret='541bf8cfdced4f01896b3d4f7551ece9') spt = Spotify(client_creds=client) spt.authorize_client_creds() pp = pprint.PrettyPrinter(indent=4) app = Flask(__name__) @app.route('/api/v1/convert', methods=['POST']) def convert_csv(): playlist_id = str(request.data).split(":", 2)[2].strip("'") #print(playlist_id) results = [] num_offset = 1 response = spt.playlist_tracks( playlist_id, fields='items(track(name,artists(name),total))', offset=num_offset) pp.pprint(response) #while "\"next\" : null" not in response.values(): #pp.pprint(response) #num_offset = num_offset + 100 #response = spt.playlist_tracks(playlist_id, fields='items(track(name,artists(name)))', offset=num_offset)
def test_caller_defaults_to_user(): u = UserCreds(access_token="asdasdasd") c = Spotify(user_creds=u, populate_user_creds=False) assert c._caller == c.user_creds
async def test_authenticated_user_is_authorized(user_creds_from_env, client_creds_from_env): spotify = Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env) assert await spotify.me()
def test_client_raises_error_if_both_access_token_and_model(): u = UserCreds() u.load_from_env() with pytest.raises(ValueError): Spotify(user_creds=u, access_token=u.access_token)
def test_sync_client_instantiates_empty(): Spotify()
async def test_user_refresh_token(user_creds_from_env, client_creds_from_env): spotify = Spotify(client_creds=client_creds_from_env, user_creds=user_creds_from_env) await spotify.populate_user_creds() await spotify._refresh_token()