Example #1
0
def _authorization_code_request(auth_code):
    config = read_config()
    auth_key = get_auth_key(config.client_id, config.client_secret)
    headers = {'Authorization': f'Basic {auth_key}', }
    options = {
        'code': auth_code,
        'redirect_uri': 'http://localhost:3000/callback',
        'grant_type': 'authorization_code',
        'json': True
    }

    response = requests.post(
        config.access_token_url,
        headers=headers,
        data=options
    )
    content = json.loads(response.content.decode('utf-8'))

    if not response.ok:
        error_description = content.get('error_description')
        raise BadRequestError(error_description)

    access_token = content.get('access_token', None)
    token_type = content.get('token_type', None)
    expires_in = content.get('expires_in', None)
    refresh_token = content.get('refresh_token', None)
    scope = content.get('scope', None)

    return Authorization(access_token, token_type, expires_in, scope, refresh_token)
def _authorization_code_request(auth_code):
    config = read_config()

    auth_key = get_auth_key(config.client_id, config.client_secret)

    headers = {"Authorization": "Basic {}".format(auth_key), }

    options = {
        'code': auth_code,
        'redirect_uri': "http://localhost:3000/callback",
        'grant_type': 'authorization_code',  # This is important to getting authorization to change permissions
        "json": True
    }

    response = requests.post(
        config.access_token_url,
        headers=headers,
        data=options
    )

    content = json.loads(response.content.decode('utf-8'))

    if response.status_code ==400:
        error_description = content.get("error_description", "")
        raise BadRequestError("error_description")

    access_token =content.get('access_token', None)
    token_type = content.get("token_type", None)
    expires_in = content.get("expires_in", None)
    scope = content.get("scope", None)
    refresh_token = content.get("refresh_token", None)

    return Authorization(access_token, token_type, expires_in,
                         scope, refresh_token)
def callback():
    config = read_config()
    code = request.args.get("code", "")
    response = _authorization_code_request(code)

    with open(".pytify", mode='w', encoding="utf-8") as f:
        f.write(response.refresh_token)

    return "All set! You can close the browser window and stop the server."
Example #4
0
def callback():
    config = read_config()
    code = request.args.get('code', '')
    response = _authorization_code_request(config, code)

    file = open('.pytify', mode='w', encoding='utf-8')
    file.write(response.refresh_token)
    file.close()

    return 'All set! You can close the browser window and stop the server.'
def resume(device_name):
	_auth=authenticate(read_config())
	dev_id=""
	spdevices=list_devices(_auth)
	#print(spdevices)
	for dev in spdevices['devices']:
		if device_name == dev['name']:
			dev_id=dev['id']
	
	#Set device_name as player
	print("Set Player: "+device_name)
	playdev(dev_id,False,_auth)
Example #6
0
def home():
    config = read_config()

    params = {
        'client_id': config.client_id,
        'response_type': 'code',
        'redirect_uri': 'http://localhost:3000/callback',
        'scope': 'user-read-private user-modify-playback-state'
    }

    enc_params = urlencode(params)
    url = f'{config.auth_url}?{enc_params}'
    return render_template('index.html', link=url)
Example #7
0
def _search(criteria, auth, search_type):
    conf = read_config()

    if not criteria:
        raise AttributeError('Parameter `criteria` is required.')

    q_type = search_type.name.lower()
    url = f'{conf.base_url}/search?q={criteria}&type={q_type}'

    headers = {'Authorization': f'Bearer {auth.access_token}'}
    response = requests.get(url, headers=headers)

    return json.loads(response.text)
def home():
    config = read_config()

    params = {
        "client_id": config.client_id,
        "response_type": 'code',
        "redirect_uri": 'http://localhost:3000/callback',
        'scope': 'user-read-private user-modify-playback-state',
    }

    enc_params = urlencode(params)

    url = "{}?{}".format(config.auth_url, enc_params)
    return render_template('index.html', link=url)
Example #9
0
def _search(criteria, auth, search_type):

    conf = read_config()

    if not criteria:
        raise AttributeError('Parameter `criteria` is required')

    q_type = search_type.name.lower()
    url = conf.base_url + "/search?q=" + urllib.parse.quote_plus(
        f'{criteria}') + "&type=" + urllib.parse.quote_plus(f'{q_type}')

    print("SEARCH IS: ", url)

    headers = {'Authorization': f'Bearer {auth.access_token}'}
    response = requests.get(url, headers=headers)

    return json.loads(response.text)
Example #10
0
def _search(criteria, auth, search_type):
    logger = logging_wrapper(2, "logs/test.log")
    logger.info("Criteria is: {}".format(criteria))

    config = read_config()

    if not criteria:
        raise AttributeError("Parameter `criteria` is required")

    q_type = search_type.name.lower()
    url = quote(f'{config.base_url}/search?q={criteria}&type={q_type}',
                safe="/:?&=")
    logger.info("URL to request: {}".format(url))

    headers = {"Authorization": f'Bearer {auth.access_token}'}
    response = requests.get(url, headers=headers)

    # if response['error']:
    #     #     logger.warning("{}".format(response['Error']))

    return json.loads(response.text)
def play_uri(spotifyuri,device_name):

	print("SporitfyConnect")
	_auth=authenticate(read_config())
	
	#get device id from device name
	dev_id=""
	spdevices=list_devices(_auth)
	#print(spdevices)
	for dev in spdevices['devices']:
		if device_name == dev['name']:
			dev_id=dev['id']
	
	#Set device_name as player
	print("Set Player: "+device_name)
	playdev(dev_id,True,_auth)
	
	#Track or Album
	print("Track: "+spotifyuri)
	tracklist=[]
	if "album" in spotifyuri:
		print('is Album')
		split=spotifyuri.split(':')
		results=get_album_tracks(split[2],_auth)
		tracks=results['items']
		for track in tracks:
			tracklist.append(track['uri'])
	elif "track" in spotifyuri:
		print('is Track')
		tracklist.append(spotifyuri)
	else:
		print('Notsupportedjet!')
	
	#Play
	print("Start playing!")
	play(tracklist,_auth)
Example #12
0
 def __init__(self): 
     self._conf = read_config() 
     self._auth = authenticate(self._conf) 
Example #13
0
 def __init__(self):
     self._config = read_config()
     self._authentication = authenticate(self._config)