def generate_spotify_access_tokens(self, spotify_code, connected_clients_ip): if connected_clients_ip is None or not isinstance( connected_clients_ip, str): raise ValueError( f"connected_clients_ip ({connected_clients_ip}) is not a good string" ) if spotify_code is None or not isinstance(spotify_code, str): raise ValueError( f"spotify_code ({spotify_code}) is not a good string") url = "https://accounts.spotify.com/api/token" headers = { 'Content-Type': "application/x-www-form-urlencoded", 'Authorization': get_env("SPOTIFY_CLIENT_BASIC") } params = { "code": spotify_code, "grant_type": "authorization_code", "redirect_uri": get_env("APP_URL") + get_env("SPOTIFY_REDIRECT_URI"), } # Make the request against Spotify token_data = self.generate_spotify_access_tokens_web_request( url, headers, params) return token_data
def _setup(self): conn = mariadb.connect( user=get_env('DATABASE_USER'), password=get_env('DATABASE_SECRET'), host=get_env('DATABASE_IP'), port=int(get_env('DATABASE_PORT')), database=get_env('DATABASE_SELECTED') ) conn.auto_reconnect = True self.database_connection = conn
def logout(): try: core.user_logout() except: return render_template('error.html') return redirect(get_env('APP_URL'), 302)
def get_callback_url(self, connected_clients_ip): if connected_clients_ip is None or not isinstance( connected_clients_ip, str): return False url = "https://accounts.spotify.com/authorize" # Get a random state to use random_state = self._generate_random_state() if not random_state: return False # Saved the state in the database to use for later state_saved = self._save_generated_random_state( random_state, connected_clients_ip) if not state_saved: return False # Fill out our HTTP parameters params = { "client_id": get_env("SPOTIFY_CLIENT_ID"), "response_type": "code", "redirect_uri": get_env("APP_URL") + get_env("SPOTIFY_REDIRECT_URI"), "scope": "user-read-email user-read-private user-read-currently-playing user-modify-playback-state user-read-playback-state", "state": random_state } headers = {'content-type': 'application/json'} # Generate the redirect URL redirect_url = self._get_callback_url_web_request_to_spotify( url, params, headers) if not redirect_url: return False return redirect_url
def get_chords_url_from_google_search_api(self, song_name, artist_name): google_api_key = get_env('GOOGLE_SEARCH_API_KEY') google_engine_id = get_env('GOOGLE_SEARCH_ENGINE_ID') num_of_results = 1 search_string = self.format_google_search_string(song_name, artist_name) print(f" log: searching for {search_string}") # return google_search(search_string) url = f"https://customsearch.googleapis.com/customsearch/v1?key={google_api_key}&cx={google_engine_id}&num={num_of_results}&q={search_string}" response = web_request_get(url=url) status = response.status_code if status == 200: response_json = response.json() if response_json['searchInformation']['totalResults'] != '0': chords_url = response_json['items'][0]['link'] if response.status_code != 200: print("unable to get song from Google, status code {0}".format(response.status_code)) chords_url = None return (status, chords_url)
def spotify_callback(): connected_clients_ip = request.remote_addr connected_clients_user_agent = request.user_agent if 'code' in request.args and 'state' in request.args: spotify_code = request.args.get('code') spotify_state = request.args.get('state') try: core.spotify_callback(connected_clients_ip, spotify_code, spotify_state) except: return render_template('error.html') return redirect(get_env('APP_URL'), 302)
def refresh_user_access_tokens(self, refresh_token): if refresh_token is None or not isinstance(refresh_token, str): raise ValueError( f"refresh_token ({refresh_token}) is not a good string") print("Refreshing user access tokens") url = "https://accounts.spotify.com/api/token" headers = { 'Content-Type': "application/x-www-form-urlencoded", 'Authorization': get_env("SPOTIFY_CLIENT_BASIC") } params = { "refresh_token": refresh_token, "grant_type": "refresh_token" } new_user_access_token = self.refresh_user_access_tokens_web_request( url, headers, params) return new_user_access_token
# from app import app from flask import Flask, request, redirect, render_template, Response from app.core.main_handler import Core from app.core.controllers.settings import get_env import requests import json import os # ********************************************* # app = Flask(__name__) app.config['SECRET_KEY'] = get_env('APP_SECRET') app.config['PERMANENT_SESSION_LIFETIME'] = int( get_env('USER_LOGIN_TOKEN_LIFETIME')) # ********************************************* # core = Core.Instance() @app.route('/') def home(): try: returning_user = core.returning_user() except: return render_template('error.html') if returning_user: user_details = core.get_user_details()