Geek7 commited on
Commit
6eecc17
·
verified ·
1 Parent(s): 6b6ca30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -50
app.py CHANGED
@@ -1,53 +1,11 @@
1
- from flask import Flask, request, jsonify, redirect
2
- import requests
3
- import os
4
- from flask_cors import CORS
5
-
6
- app = Flask(__name__)
7
- CORS(app)
8
-
9
- # Access the secrets from environment variables in Hugging Face
10
- CLIENT_ID = os.getenv('v1') # Your Client ID
11
- CLIENT_SECRET = os.getenv('v2') # Your Client Secret
12
- REDIRECT_URI = 'http://127.0.0.1:5000/callback'
13
- SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
14
- SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
15
- SPOTIFY_API_URL = "https://api.spotify.com/v1"
16
-
17
 
18
- @app.route('/')
19
- def home():
20
- return "Welcome to the test Spotify API app!"
21
-
22
- # Get Spotify authorization URL
23
- @app.route('/login')
24
- def login():
25
- scopes = "user-read-private user-read-email"
26
- auth_url = f"{SPOTIFY_AUTH_URL}?response_type=code&client_id={CLIENT_ID}&scope={scopes}&redirect_uri={REDIRECT_URI}"
27
- return redirect(auth_url)
28
-
29
- # Callback to handle the access token
30
- @app.route('/callback')
31
- def callback():
32
- code = request.args.get('code')
33
- auth_response = requests.post(SPOTIFY_TOKEN_URL, data={
34
- "grant_type": "authorization_code",
35
- "code": code,
36
- "redirect_uri": REDIRECT_URI,
37
- "client_id": CLIENT_ID,
38
- "client_secret": CLIENT_SECRET,
39
- })
40
- response_data = auth_response.json()
41
- access_token = response_data.get("access_token")
42
- return jsonify({"access_token": access_token})
43
 
44
- # Fetch data from Spotify API
45
- @app.route('/api/podcasts')
46
- def get_podcasts():
47
- token = request.args.get('token') # Pass access_token as a query param
48
- headers = {"Authorization": f"Bearer {token}"}
49
- response = requests.get(f"{SPOTIFY_API_URL}/browse/categories/podcasts", headers=headers)
50
- return jsonify(response.json())
51
 
52
- if __name__ == '__main__':
53
- app.run(debug=False)
 
1
+ # app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import os
4
+ import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ if __name__ == "__main__":
7
+ # Run awake.py in the background
8
+ subprocess.Popen(["python", "wk.py"]) # Start awake.py
 
 
 
 
9
 
10
+ # Run the Flask app using Gunicorn
11
+ os.system("gunicorn -w 4 -b 0.0.0.0:5000 myapp:myapp") # 4 worker processes