-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
70 lines (56 loc) · 2.31 KB
/
server.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
# Create a list to store feedback
feedback_data = []
@app.route('/')
def home():
return render_template('index.html', feedback=feedback_data)
@app.route('/submit_feedback', methods=['POST'])
def submit_feedback():
feedback = request.json
feedback_data.append(feedback)
return jsonify({'message': 'Feedback submitted successfully'}), 200
if __name__ == '__main__':
app.run(debug=True) # Run the Flask application
=======
from flask import Flask, request, jsonify
from flask_cors import CORS
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import random
app = Flask(__name__)
CORS(app)
# Spotify API credentials
CLIENT_ID = 'e060c3e4e5f743248e88088593f1d5ca'
CLIENT_SECRET = 'ab703da153db477e885cc55cef61543f'
# Authenticate with Spotify API
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET))
# Function to get user's top tracks from a playlist
def get_user_playlist_tracks(username, playlist_id):
results = sp.user_playlist_tracks(username, playlist_id)
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
return tracks
# Function to recommend songs not in the playlist
def recommend_songs(tracks, k=5):
track_uris = [track['track']['uri'] for track in tracks if track['track']['uri'] is not None]
seed_tracks = random.sample(track_uris, min(len(track_uris), 5))
recommended_tracks = sp.recommendations(seed_tracks=seed_tracks, limit=k)
return [(track['name'], track['external_urls']['spotify']) for track in recommended_tracks['tracks']]
@app.route('/recommendations', methods=['GET'])
def get_recommendations():
username = request.args.get('username')
playlist_id = request.args.get('playlist_id')
tracks = get_user_playlist_tracks(username, playlist_id)
recommended_songs = recommend_songs(tracks, k=5)
return jsonify(recommended_songs=recommended_songs)
@app.route('/submit_feedback', methods=['POST'])
def submit_feedback():
data = request.get_json()
# Process feedback data
# For demonstration, just return success
return jsonify(message="Feedback submitted successfully")
if __name__ == '__main__':
app.run(debug=True)