Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516121819202122232425262728293031323334353637383940414234544454647484950678952531011from flask import Flask, request, jsonify, redirectimport requestsimport osfrom flask_cors import CORS
app = Flask(__name__)CORS(app)
# Access the secrets from environment variables in Hugging FaceCLIENT_ID = os.getenv('v1') # Your Client IDCLIENT_SECRET = os.getenv('v2') # Your Client SecretREDIRECT_URI = 'http://127.0.0.1:5000/callback'SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"SPOTIFY_API_URL = "https://api.spotify.com/v1"
# app.py
@app.route('/')def home(): return "Welcome to the test Spotify API app!"
# Get Spotify authorization URL@app.route('/login')def login(): scopes = "user-read-private user-read-email" auth_url = f"{SPOTIFY_AUTH_URL}?response_type=code&client_id={CLIENT_ID}&scope={scopes}&redirect_uri={REDIRECT_URI}" return redirect(auth_url)
# Callback to handle the access token@app.route('/callback')def callback(): code = request.args.get('code') auth_response = requests.post(SPOTIFY_TOKEN_URL, data={ "grant_type": "authorization_code", "code": code, "redirect_uri": REDIRECT_URI, "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, }) response_data = auth_response.json() access_token = response_data.get("access_token") return jsonify({"access_token": access_token})import osimport subprocess
# Fetch data from Spotify API@app.route('/api/podcasts')def get_podcasts(): token = request.args.get('token') # Pass access_token as a query param headers = {"Authorization": f"Bearer {token}"} response = requests.get(f"{SPOTIFY_API_URL}/browse/categories/podcasts", headers=headers) return jsonify(response.json())if __name__ == "__main__": # Run awake.py in the background subprocess.Popen(["python", "wk.py"]) # Start awake.py
if __name__ == '__main__': app.run(debug=False) # Run the Flask app using Gunicorn os.system("gunicorn -w 4 -b 0.0.0.0:5000 myapp:myapp") # 4 worker processesapp.py+8-50