File size: 1,830 Bytes
c813777
 
cce730e
c813777
ccc2556
 
c813777
715d5c0
c813777
e27c3fe
 
f12f326
c813777
 
 
ccc2556
c813777
ccc2556
 
89696bb
 
 
c813777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfe4016
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from flask import Flask, request, jsonify, redirect, render_template
import requests
import os
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# Access the secrets from environment variables in Hugging Face
CLIENT_ID =  os.environ.get("v1")  # Your Client ID
CLIENT_SECRET = os.environ.get("v2")  # Your Client Secret
REDIRECT_URI = 'https://geek7-spotifypod.hf.space/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"

# Home route
@app.route('/')
def home():
    return "Welcome to the Image Background Remover!"



# 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})

# 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__':
    app.run(host='0.0.0.0', port=7860)