Spaces:
Running
Running
File size: 2,157 Bytes
0df20ca |
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 70 71 72 73 74 75 76 77 78 79 80 |
```python
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
# Mock data for demonstration
mock_playlist = [
{
"id": 1,
"title": "Midnight City",
"artist": "M83",
"duration": "4:03",
"thumbnail": "http://static.photos/music/200x200/1",
"downloaded": True
},
{
"id": 2,
"title": "Blinding Lights",
"artist": "The Weeknd",
"duration": "3:20",
"thumbnail": "http://static.photos/music/200x200/2",
"downloaded": True
}
]
mock_search_results = [
{
"id": 3,
"title": "Save Your Tears",
"artist": "The Weeknd",
"duration": "3:35",
"thumbnail": "http://static.photos/music/200x200/3",
"downloaded": False
},
{
"id": 4,
"title": "Starboy",
"artist": "The Weeknd ft. Daft Punk",
"duration": "3:50",
"thumbnail": "http://static.photos/music/200x200/4",
"downloaded": False
}
]
@app.route('/')
def index():
return render_template('player.html')
@app.route('/api/playlist')
def get_playlist():
return jsonify(mock_playlist)
@app.route('/api/search', methods=['POST'])
def search():
query = request.json.get('query', '')
# In a real app, you would search your database or API here
return jsonify(mock_search_results)
@app.route('/api/add_to_playlist', methods=['POST'])
def add_to_playlist():
song_id = request.json.get('id')
# Find the song in search results and add to playlist
for song in mock_search_results:
if song['id'] == song_id:
new_song = song.copy()
new_song['downloaded'] = False
mock_playlist.append(new_song)
return jsonify({"success": True})
return jsonify({"success": False}), 404
@app.route('/api/remove_from_playlist', methods=['POST'])
def remove_from_playlist():
song_id = request.json.get('id')
global mock_playlist
mock_playlist = [song for song in mock_playlist if song['id'] != song_id]
return jsonify({"success": True})
if __name__ == '__main__':
app.run(debug=True)
``` |